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

bugfix for issue #11 (skip xml entities in filter method) #12

Merged
merged 7 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 26 additions & 2 deletions core/src/main/java/org/apache/struts/util/ResponseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +37,9 @@
* $
*/
public class ResponseUtils {

protected static Pattern XML_ENTITY_PATTERN = Pattern.compile("&(?:[a-z\\d]+|#\\d+|#x[a-f\\d]+);");
Copy link
Member

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.

Suggested change
protected static Pattern XML_ENTITY_PATTERN = Pattern.compile("&(?:[a-z\\d]+|#\\d+|#x[a-f\\d]+);");
protected static Pattern XML_ENTITY_PATTERN = Pattern.compile("&(?:[a-zA-Z][a-zA-Z\\d]*|#\\d+|#x[a-fA-F\\d]+);");


// ------------------------------------------------------- Static Variables

/**
Expand Down Expand Up @@ -82,8 +87,11 @@ public static String filter(String value) {
break;

case '&':
filtered = "&";

if ( isStartOfXmlEntity(value,i) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete blanks after ( and before ), thanks.

Suggested change
if ( isStartOfXmlEntity(value,i) ) {
if (isStartOfXmlEntity(value,i)) {

filtered = "&"; // leave unchanged
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When unchanged, it's not needed to set filtered.

} else {
filtered = "&";
}
break;

case '"':
Expand Down Expand Up @@ -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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( matcher.find() && matcher.start() == 0 ) {
return matcher.find() && matcher.start() == 0;

return true;
} else {
return false;
}
}

/**
* URLencodes a string assuming the character encoding is UTF-8.
*
* @param url
Expand Down Expand Up @@ -162,4 +185,5 @@ public static String encodeURL(String url, String enc) {
return str;

}

}
60 changes: 60 additions & 0 deletions core/src/test/java/org/apache/struts/util/TestResponseUtils.java
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&amp;456", ResponseUtils.filter("123&456"));
assertEquals("123&amp;456", ResponseUtils.filter("123&amp;456"));
assertEquals("123&#123;456", ResponseUtils.filter("123&#123;456"));
assertEquals("123&amp;#12a;456", ResponseUtils.filter("123&#12a;456"));
assertEquals("123&#x12a;456", ResponseUtils.filter("123&#x12a;456"));
assertEquals("123&amp;#x12ah;456", ResponseUtils.filter("123&#x12ah;456"));
assertEquals("123&lt;&gt;&quot;&#39;456", ResponseUtils.filter("123<>\"'456"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test-case "123&456;789" should return "123&amp;456;789" because the "NameStartChar" is not allowed to begin with an number. See https://www.w3.org/TR/xml/#NT-Name

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test-case "123&Uuml;456" will not work. Could you also add this case please.

}