Skip to content

Commit 7a7df66

Browse files
committed
Update JavaScriptUtils
Add escaping for <, >, and PS/LS line terminators Issue: SPR-9983
1 parent 63bff1f commit 7a7df66

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/JavaScriptUtils.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,21 +21,21 @@
2121
* Escapes based on the JavaScript 1.5 recommendation.
2222
*
2323
* <p>Reference:
24-
* <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#String_Literals">
25-
* Core JavaScript 1.5 Guide
26-
* </a>
24+
* <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#String_literals">
25+
* JavaScript Guide</a> on Mozilla Developer Network.
2726
*
2827
* @author Juergen Hoeller
2928
* @author Rob Harrop
29+
* @author Rossen Stoyanchev
3030
* @since 1.1.1
3131
*/
3232
public class JavaScriptUtils {
3333

3434
/**
35-
* Turn special characters into escaped characters conforming to JavaScript.
36-
* Handles complete character set defined in HTML 4.01 recommendation.
35+
* Turn JavaScript special characters into escaped characters.
36+
*
3737
* @param input the input string
38-
* @return the escaped string
38+
* @return the string with escaped characters
3939
*/
4040
public static String javaScriptEscape(String input) {
4141
if (input == null) {
@@ -73,6 +73,27 @@ else if (c == '\r') {
7373
else if (c == '\f') {
7474
filtered.append("\\f");
7575
}
76+
else if (c == '\b') {
77+
filtered.append("\\b");
78+
}
79+
// No '\v' in Java, use octal value for VT ascii char
80+
else if (c == '\013') {
81+
filtered.append("\\v");
82+
}
83+
else if (c == '<') {
84+
filtered.append("\\u003C");
85+
}
86+
else if (c == '>') {
87+
filtered.append("\\u003E");
88+
}
89+
// Unicode for PS (line terminator in ECMA-262)
90+
else if (c == '\u2028') {
91+
filtered.append("\\u2028");
92+
}
93+
// Unicode for LS (line terminator in ECMA-262)
94+
else if (c == '\u2029') {
95+
filtered.append("\\u2029");
96+
}
7697
else {
7798
filtered.append(c);
7899
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2004-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.util;
18+
19+
import static org.junit.Assert.*;
20+
21+
import java.io.UnsupportedEncodingException;
22+
23+
import org.junit.Test;
24+
25+
/**
26+
* Test fixture for {@link JavaScriptUtils}.
27+
*
28+
* @author Rossen Stoyanchev
29+
*/
30+
public class JavaScriptUtilsTests {
31+
32+
@Test
33+
public void escape() {
34+
StringBuilder sb = new StringBuilder();
35+
sb.append('"');
36+
sb.append("'");
37+
sb.append("\\");
38+
sb.append("/");
39+
sb.append("\t");
40+
sb.append("\n");
41+
sb.append("\r");
42+
sb.append("\f");
43+
sb.append("\b");
44+
sb.append("\013");
45+
assertEquals("\\\"\\'\\\\\\/\\t\\n\\n\\f\\b\\v", JavaScriptUtils.javaScriptEscape(sb.toString()));
46+
}
47+
48+
// SPR-9983
49+
50+
@Test
51+
public void escapePsLsLineTerminators() {
52+
StringBuilder sb = new StringBuilder();
53+
sb.append('\u2028');
54+
sb.append('\u2029');
55+
String result = JavaScriptUtils.javaScriptEscape(sb.toString());
56+
57+
assertEquals("\\u2028\\u2029", result);
58+
}
59+
60+
// SPR-9983
61+
62+
@Test
63+
public void escapeLessThanGreaterThanSigns() throws UnsupportedEncodingException {
64+
assertEquals("\\u003C\\u003E", JavaScriptUtils.javaScriptEscape("<>"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)