Skip to content

Commit

Permalink
Merge pull request #419 from nahsra/bang-comments
Browse files Browse the repository at this point in the history
Bang comments
  • Loading branch information
davewichers authored Jan 15, 2024
2 parents 8032983 + 0b1a534 commit aa88f4a
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 68 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<project.java.target>1.8</project.java.target>
<version.findsecbugs>1.12.0</version.findsecbugs>
<version.slf4j>2.0.11</version.slf4j>
<version.spotbugs.maven>4.8.2.0</version.spotbugs.maven>
<version.spotbugs.maven>4.8.3.0</version.spotbugs.maven>
<version.spotbugs>4.8.3</version.spotbugs>
</properties>

Expand All @@ -94,7 +94,7 @@
<dependency>
<groupId>org.htmlunit</groupId>
<artifactId>neko-htmlunit</artifactId>
<version>3.9.0</version>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
Expand Down Expand Up @@ -439,7 +439,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/owasp/validator/css/CssHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public Collection<String> getErrorMessages() {
*
* @see org.w3c.css.sac.DocumentHandler#comment(java.lang.String)
*/
@Override
public void comment(String text) throws CSSException {
errorMessages.add(
ErrorMessageUtil.getMessage(
Expand All @@ -184,6 +185,7 @@ public void comment(String text) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#ignorableAtRule(java.lang.String)
*/
@Override
public void ignorableAtRule(String atRule) throws CSSException {
// this method is called when the parser hits an unrecognized @-rule. Like the page/media/font
// declarations, this is CSS2+ stuff
Expand Down Expand Up @@ -211,6 +213,7 @@ public void ignorableAtRule(String atRule) throws CSSException {
* @see org.w3c.css.sac.DocumentHandler#importStyle(java.lang.String,
* org.w3c.css.sac.SACMediaList, java.lang.String)
*/
@Override
public void importStyle(String uri, SACMediaList media, String defaultNamespaceURI)
throws CSSException {

Expand Down Expand Up @@ -292,6 +295,7 @@ public void importStyle(String uri, SACMediaList media, String defaultNamespaceU
* @see org.w3c.css.sac.DocumentHandler#namespaceDeclaration(java.lang.String,
* java.lang.String)
*/
@Override
public void namespaceDeclaration(String prefix, String uri) throws CSSException {
// CSS3 - Namespace declaration - ignore for now
}
Expand All @@ -301,6 +305,7 @@ public void namespaceDeclaration(String prefix, String uri) throws CSSException
*
* @see org.w3c.css.sac.DocumentHandler#startDocument(org.w3c.css.sac.InputSource)
*/
@Override
public void startDocument(InputSource arg0) throws CSSException {
// no-op
}
Expand All @@ -310,6 +315,7 @@ public void startDocument(InputSource arg0) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#endDocument(org.w3c.css.sac.InputSource)
*/
@Override
public void endDocument(InputSource source) throws CSSException {
// no-op
}
Expand All @@ -319,6 +325,7 @@ public void endDocument(InputSource source) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#startFontFace()
*/
@Override
public void startFontFace() throws CSSException {
// CSS2 Font Face declaration - ignore this for now
}
Expand All @@ -328,6 +335,7 @@ public void startFontFace() throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#endFontFace()
*/
@Override
public void endFontFace() throws CSSException {
// CSS2 Font Face declaration - ignore this for now
}
Expand All @@ -337,6 +345,7 @@ public void endFontFace() throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#startMedia(org.w3c.css.sac.SACMediaList)
*/
@Override
public void startMedia(SACMediaList media) throws CSSException {
// CSS2 Media declaration - ignore this for now
}
Expand All @@ -346,6 +355,7 @@ public void startMedia(SACMediaList media) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#endMedia(org.w3c.css.sac.SACMediaList)
*/
@Override
public void endMedia(SACMediaList media) throws CSSException {
// CSS2 Media declaration - ignore this for now
}
Expand All @@ -356,6 +366,7 @@ public void endMedia(SACMediaList media) throws CSSException {
* @see org.w3c.css.sac.DocumentHandler#startPage(java.lang.String,
* java.lang.String)
*/
@Override
public void startPage(String name, String pseudoPage) throws CSSException {
// CSS2 Page declaration - ignore this for now
}
Expand All @@ -366,6 +377,7 @@ public void startPage(String name, String pseudoPage) throws CSSException {
* @see org.w3c.css.sac.DocumentHandler#endPage(java.lang.String,
* java.lang.String)
*/
@Override
public void endPage(String name, String pseudoPage) throws CSSException {
// CSS2 Page declaration - ignore this for now
}
Expand All @@ -375,6 +387,7 @@ public void endPage(String name, String pseudoPage) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#startSelector(org.w3c.css.sac.SelectorList)
*/
@Override
public void startSelector(SelectorList selectors) throws CSSException {

// keep track of number of valid selectors from this rule
Expand Down Expand Up @@ -455,6 +468,7 @@ public void startSelector(SelectorList selectors) throws CSSException {
*
* @see org.w3c.css.sac.DocumentHandler#endSelector(org.w3c.css.sac.SelectorList)
*/
@Override
public void endSelector(SelectorList selectors) throws CSSException {
// if we are in a state within a selector, close brace
if (selectorOpen) {
Expand All @@ -472,6 +486,7 @@ public void endSelector(SelectorList selectors) throws CSSException {
* @see org.w3c.css.sac.DocumentHandler#property(java.lang.String,
* org.w3c.css.sac.LexicalUnit, boolean)
*/
@Override
public void property(String name, LexicalUnit value, boolean important) throws CSSException {
// only bother validating and building if we are either inline or within a selector tag

Expand Down
106 changes: 54 additions & 52 deletions src/main/java/org/owasp/validator/css/CssParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,63 +35,65 @@

public class CssParser extends org.apache.batik.css.parser.Parser {

/**
* This implementation is a workaround to solve leading dash errors on property names.
* @see <code>https://issues.apache.org/jira/browse/BATIK-1112</code>
* @param inSheet Specifies if the style to parse is inside a sheet or the sheet itself.
* @throws CSSException Thrown if there are parsing errors in CSS
*/
protected void parseStyleDeclaration(final boolean inSheet) throws CSSException {
boolean leadingDash = false;
for (;;) {
switch (current) {
case LexicalUnits.EOF:
if (inSheet) {
throw createCSSParseException("eof");
}
return;
case LexicalUnits.RIGHT_CURLY_BRACE:
if (!inSheet) {
throw createCSSParseException("eof.expected");
}
nextIgnoreSpaces();
return;
case LexicalUnits.SEMI_COLON:
nextIgnoreSpaces();
continue;
case LexicalUnits.MINUS:
leadingDash = true;
next();
break;
default:
throw createCSSParseException("identifier");
case LexicalUnits.IDENTIFIER:
}
/**
* This implementation is a workaround to solve leading dash errors on property names.
*
* @see <code>https://issues.apache.org/jira/browse/BATIK-1112</code>
* @param inSheet Specifies if the style to parse is inside a sheet or the sheet itself.
* @throws CSSException Thrown if there are parsing errors in CSS
*/
@Override
protected void parseStyleDeclaration(final boolean inSheet) throws CSSException {
boolean leadingDash = false;
for (; ; ) {
switch (current) {
case LexicalUnits.EOF:
if (inSheet) {
throw createCSSParseException("eof");
}
return;
case LexicalUnits.RIGHT_CURLY_BRACE:
if (!inSheet) {
throw createCSSParseException("eof.expected");
}
nextIgnoreSpaces();
return;
case LexicalUnits.SEMI_COLON:
nextIgnoreSpaces();
continue;
case LexicalUnits.MINUS:
leadingDash = true;
next();
break;
default:
throw createCSSParseException("identifier");
case LexicalUnits.IDENTIFIER:
}

final String name = (leadingDash ? "-" : "") + scanner.getStringValue();
leadingDash = false;
final String name = (leadingDash ? "-" : "") + scanner.getStringValue();
leadingDash = false;

if (nextIgnoreSpaces() != LexicalUnits.COLON) {
throw createCSSParseException("colon");
}
nextIgnoreSpaces();
if (nextIgnoreSpaces() != LexicalUnits.COLON) {
throw createCSSParseException("colon");
}
nextIgnoreSpaces();

LexicalUnit exp = null;
LexicalUnit exp = null;

try {
exp = parseExpression(false);
} catch (final CSSParseException e) {
reportError(e);
}
try {
exp = parseExpression(false);
} catch (final CSSParseException e) {
reportError(e);
}

if (exp != null) {
boolean important = false;
if (current == LexicalUnits.IMPORTANT_SYMBOL) {
important = true;
nextIgnoreSpaces();
}
documentHandler.property(name, exp, important);
}
if (exp != null) {
boolean important = false;
if (current == LexicalUnits.IMPORTANT_SYMBOL) {
important = true;
nextIgnoreSpaces();
}
documentHandler.property(name, exp, important);
}
}
}
}
1 change: 1 addition & 0 deletions src/main/java/org/owasp/validator/html/CleanResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public CleanResults(
this(
startOfScan,
new Callable<String>() {
@Override
public String call() throws Exception {
return cleanHTML;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/owasp/validator/html/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -1070,19 +1070,23 @@ private static Iterable<Element> getByTagName(Element parent, String tagName) {

final NodeList nodes = parent.getElementsByTagName(tagName);
return new Iterable<Element>() {
@Override
public Iterator<Element> iterator() {
return new Iterator<Element>() {
int pos = 0;
int len = nodes.getLength();

@Override
public boolean hasNext() {
return pos < len;
}

@Override
public Element next() {
return (Element) nodes.item(pos++);
}

@Override
public void remove() {
throw new UnsupportedOperationException("Cant remove");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/owasp/validator/html/model/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public String getRegularExpression() {
Collections.sort(
values,
new Comparator<Attribute>() {
@Override
public int compare(Attribute o1, Attribute o2) {
return o1.getName().compareTo(o2.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public ASHTMLSerializer(Writer w, OutputFormat format, InternalPolicy policy) {
this.encodeAllPossibleEntities = policy.isEntityEncodeIntlCharacters();
}

@Override
protected String getEntityRef(int charToPrint) {
if (encodeAllPossibleEntities || Constants.big5CharsToEncode.indexOf(charToPrint) != -1)
return super.getEntityRef(charToPrint);
return null;
}

@Override
public void endElementIO(String namespaceURI, String localName, String rawName)
throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public CleanResults scan(String html) throws ScanException {

Callable<String> cleanHtml =
new Callable<String>() {
@Override
public String call() throws Exception {
return trimmed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public CleanResults scan(String html, Policy policy) throws ScanException {
final String tainted = html;
Callable<String> cleanCallable =
new Callable<String>() {
@Override
public String call() throws Exception {
return trim(tainted, out.toString());
}
Expand Down
Loading

0 comments on commit aa88f4a

Please sign in to comment.