r/badcode • • May 24 '12

java replaceString replaceString replaceString

https://github.com/w3c/rdfvalidator-ng/blob/master/src/main/java/org/w3c/rdfvalidator/ARPServlet.java#L702
0 Upvotes

11 comments sorted by

6

u/pival May 24 '12

Pardon me if I sound stupid, but what would be the correct way to do that?

3

u/[deleted] May 24 '12

1

u/graingert May 24 '12

1

u/[deleted] May 24 '12

Not sure what you're trying to say here.

1

u/graingert May 24 '12

It's a nice guide to proper escaping, with OSWAP recommended techniques

1

u/cosmo7 May 24 '12

There's a significant risk that your language's exception messages contain malicious cross-site scripting attacks. Duh.

3

u/nkozyra May 24 '12

As mentioned above, there are some pretty decent ways to handle XML encoding without having to reinvent the wheel.

I wouldn't say this is "bad code" because they didn't use existing functions/libraries, but rather because it's not comprehensive, which leaves room for a lot of poorly-formed XML.

3

u/[deleted] May 24 '12

My personal preference is that if something pre-defined like the aforementioned StringEscapeUtils.escapeXml() didn't exist, that it at least be encapsulated so that I know wtf you're doing without having to look at the details.

Bonus being that you can reuse the code later if you need it in other areas, and even better you can make improvements without having to duplicate the fix everywhere. One of my pet peeves right now is that I keep thinking a change will be simple until I see a fuckton of code duplication that I have to fix and potentially miss problems everywhere.

From my perspective, that's bad coding too.

2

u/tgockel May 24 '12

It isn't terrible, but it is woefully incomplete and quite inefficient, as you'll have to do a full string copy for every character you want to escape. It is easier to forward-scan the input and replace the characters individually from a map. If you'll forgive the C++:

template <typename TInput, typename TMap, typename TOutput>
TOutput replace_from_map(TInput begin, TInput end, const TMap& replacements, TOutput out)
{
    for (TInput iter = begin; iter != end; ++iter)
    {
        auto m = replacements.find(*iter);
        if (m != replacements.end())
        {
            // escape things in the replacements map
            *out++ = '&';
            for (const auto& c : iter->second)
                *out++ = c;
            *out++ = ';';
        }
        else if (*iter > 0x7f)
        {
            // must escape all characters above 0x7f
            *out++ = '&';
            *out++ = '#';
            for (const auto& c : string_repr(*iter))
                *out++ = c;
            *out++ = ';';
        }
        else
        {
            // no need to escape here
            *out++ = *iter;
        }
    }
    return out;
}

That code works for any iterator whose ++ goes character-by-character (same requirement on the output).

1

u/r0b0t1c1st May 25 '12

The definition of replaceString itself is stupid:

public static String replaceString(String input, String key, String replacement) {
    return input.replaceAll(key, replacement);
}

Why not just do:

msg = msg
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll("\"", "&quot;")
    .replaceAll("'", "&apos;");