-
Notifications
You must be signed in to change notification settings - Fork 8
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
bugfix for issue #11 (skip xml entities in filter method) #12
Changes from 1 commit
fe49d5b
0b231be
9d4f368
065261b
fa95787
6af5b6a
4ca1dbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,8 @@ | |||||
import java.io.UnsupportedEncodingException; | ||||||
import java.net.URLEncoder; | ||||||
import java.nio.charset.Charset; | ||||||
import java.util.regex.Matcher; | ||||||
import java.util.regex.Pattern; | ||||||
|
||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
|
@@ -35,6 +37,9 @@ | |||||
* $ | ||||||
*/ | ||||||
public class ResponseUtils { | ||||||
|
||||||
protected static Pattern XML_ENTITY_PATTERN = Pattern.compile("&(?:[a-z\\d]+|#\\d+|#x[a-f\\d]+);"); | ||||||
|
||||||
// ------------------------------------------------------- Static Variables | ||||||
|
||||||
/** | ||||||
|
@@ -82,8 +87,11 @@ public static String filter(String value) { | |||||
break; | ||||||
|
||||||
case '&': | ||||||
filtered = "&"; | ||||||
|
||||||
if ( isStartOfXmlEntity(value,i) ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete blanks after
Suggested change
|
||||||
filtered = "&"; // leave unchanged | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When unchanged, it's not needed to set |
||||||
} else { | ||||||
filtered = "&"; | ||||||
} | ||||||
break; | ||||||
|
||||||
case '"': | ||||||
|
@@ -120,6 +128,21 @@ public static String filter(String value) { | |||||
} | ||||||
|
||||||
/** | ||||||
* Checks, if a given string contains a XML entity starting at a specified position | ||||||
* @param str the string value to check | ||||||
* @param startpos the index where the entity is expected to start | ||||||
* @return <code>true</code> if a XML entity was found, <code>false</code> otherwise | ||||||
*/ | ||||||
private static boolean isStartOfXmlEntity(String str, int startpos) { | ||||||
Matcher matcher = XML_ENTITY_PATTERN.matcher(str.substring(startpos)); | ||||||
if ( matcher.find() && matcher.start() == 0 ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return true; | ||||||
} else { | ||||||
return false; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* URLencodes a string assuming the character encoding is UTF-8. | ||||||
* | ||||||
* @param url | ||||||
|
@@ -162,4 +185,5 @@ public static String encodeURL(String url, String enc) { | |||||
return str; | ||||||
|
||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* 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.struts.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.apache.struts.mock.TestMockBase; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link ResponseUtils}. | ||
* | ||
* @version $Rev$ $Date$ | ||
*/ | ||
public class TestResponseUtils extends TestMockBase { | ||
// ----------------------------------------------------- Instance Variables | ||
// ----------------------------------------------------- Setup and Teardown | ||
@BeforeEach | ||
public void setUp() { | ||
super.setUp(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
super.tearDown(); | ||
} | ||
|
||
// ------------------------------------------------------- Individual Tests | ||
// ---------------------------------------------------------- filter() | ||
@Test | ||
public void testFilter() { | ||
assertEquals("123&456", ResponseUtils.filter("123&456")); | ||
assertEquals("123&456", ResponseUtils.filter("123&456")); | ||
assertEquals("123{456", ResponseUtils.filter("123{456")); | ||
assertEquals("123&#12a;456", ResponseUtils.filter("123a;456")); | ||
assertEquals("123Ī456", ResponseUtils.filter("123Ī456")); | ||
assertEquals("123&#x12ah;456", ResponseUtils.filter("123Īh;456")); | ||
assertEquals("123<>"'456", ResponseUtils.filter("123<>\"'456")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test-case " |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the test-case " |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also the suggested test-cases.