Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEXT-XXX] Support ECMAScript \x unescaping in StringEscapeUtils #589

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions src/main/java/org/apache/commons/text/StringEscapeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.text.translate.CharSequenceTranslator;
import org.apache.commons.text.translate.CsvTranslators;
import org.apache.commons.text.translate.EntityArrays;
import org.apache.commons.text.translate.HexUnescaper;
import org.apache.commons.text.translate.JavaUnicodeEscaper;
import org.apache.commons.text.translate.LookupTranslator;
import org.apache.commons.text.translate.NumericEntityEscaper;
Expand Down Expand Up @@ -375,36 +376,62 @@ public int translate(final CharSequence input, final int index, final Writer wri
}

/**
* Translator object for unescaping escaped Java.
*
* While {@link #unescapeJava(String)} is the expected method of use, this
* object allows the Java unescaping functionality to be used
* as the foundation for a custom translator.
* Translator for octal escapes.
*/
private static final CharSequenceTranslator OCTAL_JAVA_TRANSLATOR = new OctalUnescaper();

/**
* Translator for Unicode escapes.
*/
private static final CharSequenceTranslator UNICODE_JAVA_TRANSLATOR = new UnicodeUnescaper();

/**
* Translator for Java control characters.
*/
public static final CharSequenceTranslator UNESCAPE_JAVA;
private static final CharSequenceTranslator CTRL_CHARS_JAVA_TRANSLATOR = new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE);

/**
* Translator for Java escapes that aren't control characters.
*/
private static final CharSequenceTranslator UNESCAPE_JAVA_TRANSLATOR;

static {
final Map<CharSequence, CharSequence> unescapeJavaMap = new HashMap<>();
unescapeJavaMap.put("\\\\", "\\");
unescapeJavaMap.put("\\\"", "\"");
unescapeJavaMap.put("\\'", "'");
unescapeJavaMap.put("\\", StringUtils.EMPTY);
UNESCAPE_JAVA = new AggregateTranslator(
new OctalUnescaper(), // .between('\1', '\377'),
new UnicodeUnescaper(),
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE),
new LookupTranslator(Collections.unmodifiableMap(unescapeJavaMap))
);
UNESCAPE_JAVA_TRANSLATOR = new LookupTranslator(Collections.unmodifiableMap(unescapeJavaMap));
}

/**
* Translator object for unescaping escaped Java.
*
* While {@link #unescapeJava(String)} is the expected method of use, this
* object allows the Java unescaping functionality to be used
* as the foundation for a custom translator.
*/
public static final CharSequenceTranslator UNESCAPE_JAVA = new AggregateTranslator(
OCTAL_JAVA_TRANSLATOR,
UNICODE_JAVA_TRANSLATOR,
CTRL_CHARS_JAVA_TRANSLATOR,
UNESCAPE_JAVA_TRANSLATOR
);

/**
* Translator object for unescaping escaped EcmaScript.
*
* While {@link #unescapeEcmaScript(String)} is the expected method of use, this
* object allows the EcmaScript unescaping functionality to be used
* as the foundation for a custom translator.
*/
public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = UNESCAPE_JAVA;
public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = new AggregateTranslator(
new HexUnescaper(),
OCTAL_JAVA_TRANSLATOR,
UNICODE_JAVA_TRANSLATOR,
CTRL_CHARS_JAVA_TRANSLATOR,
UNESCAPE_JAVA_TRANSLATOR
);

/**
* Translator object for unescaping escaped Json.
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/apache/commons/text/translate/HexUnescaper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text.translate;

import java.io.IOException;
import java.io.Writer;

/**
* Translates escaped ASCII values of the form \\x\[0-9A-Fa-f][0-9A-Fa-f] back to ASCII.
*/
public class HexUnescaper extends CharSequenceTranslator {

/**
* {@inheritDoc}
*/
@Override
public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
final int prefixLength = 2; // "\\x".length()
final int escapeLength = 4; // "\\xHH".length()
if (input.charAt(index) == '\\' && index + 1 < input.length() && input.charAt(index + 1) == 'x') {
if (index + escapeLength <= input.length()) {
// Get 2 hex digits
final CharSequence hex = input.subSequence(index + prefixLength, index + escapeLength);

try {
final int value = Integer.parseInt(hex.toString(), 16);
writer.write((char) value);
} catch (final NumberFormatException nfe) {
throw new IllegalArgumentException("Unable to parse ASCII value: " + hex, nfe);
}
return escapeLength;
}
throw new IllegalArgumentException("Less than 2 hex digits in ASCII value: '"
+ input.subSequence(index, input.length())
+ "' due to end of CharSequence");
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ public void testUnescapeEcmaScript() {
assertEquals("<script src=\"build/main.bundle.js\"></script>", StringEscapeUtils.unescapeEcmaScript("<script src=\"build/main.bundle.js\"></script>"));
assertEquals("<script src=\"build/main.bundle.js\"></script>>",
StringEscapeUtils.unescapeEcmaScript("<script src=\"build/main.bundle.js\"></script>>"));
assertEquals("a=b", StringEscapeUtils.unescapeEcmaScript("a\\x3Db"));
}

@Test
Expand Down