Skip to content

Commit d4f31e3

Browse files
authored
Merge pull request #28 from anonyein/origin
Origin
2 parents 76ebc29 + db7a072 commit d4f31e3

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/main/java/org/jsoup/helper/Re2jRegex.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static Regex compile(String regex) {
1818
return new Re2jRegex(com.google.re2j.Pattern.compile(regex));
1919
} catch (RuntimeException e) {
2020
throw new ValidationException("Pattern syntax error: " + e.getMessage());
21+
} catch (OutOfMemoryError | StackOverflowError e) { // defensive check on regex to normalize exception
22+
throw new ValidationException("Pattern complexity error: " + e.getMessage());
2123
}
2224
}
2325

src/main/java/org/jsoup/select/Evaluator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ public AttributeKeyPair(String key, String value) {
426426
this.key = normalize(key);
427427
boolean quoted = value.startsWith("'") && value.endsWith("'")
428428
|| value.startsWith("\"") && value.endsWith("\"");
429-
if (quoted)
429+
if (quoted) {
430+
Validate.isTrue(value.length() > 1, "Quoted value must have content");
430431
value = value.substring(1, value.length() - 1);
432+
}
431433

432434
this.value = lowerCase(value); // case-insensitive match
433435
}

src/main/java/org/jsoup/select/QueryParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ private Evaluator byAttribute() {
346346
private Evaluator evaluatorForAttribute(TokenQueue cq) {
347347
String key = cq.consumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val)
348348
Validate.notEmpty(key);
349+
Validate.isFalse(key.equals("abs:"), "Absolute attribute key must have a name");
349350
cq.consumeWhitespace();
350351
final Evaluator eval;
351352

src/test/java/org/jsoup/select/SelectorTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,4 +1768,30 @@ public void testAncestorChain() {
17681768
assertSelectedIds(doc.select("div[data*='']"), "1", "2", "3");
17691769
}
17701770

1771+
@Test void parseExceptionOnEmptyAbsKey() {
1772+
// was previously firing at match time, not eval time
1773+
String q = "[abs:!=]";
1774+
boolean threw = false;
1775+
try {
1776+
Evaluator e = Selector.evaluatorOf(q);
1777+
} catch (Selector.SelectorParseException ex) {
1778+
threw = true;
1779+
assertEquals("Absolute attribute key must have a name", ex.getMessage());
1780+
}
1781+
assertTrue(threw);
1782+
}
1783+
1784+
@Test void parseExceptionOnEmptyKeyVal() {
1785+
// was previously firing at match time, not eval time
1786+
String q = "[\"=\"]";
1787+
boolean threw = false;
1788+
try {
1789+
Evaluator e = Selector.evaluatorOf(q);
1790+
} catch (Selector.SelectorParseException ex) {
1791+
threw = true;
1792+
assertEquals("Quoted value must have content", ex.getMessage());
1793+
}
1794+
assertTrue(threw);
1795+
}
1796+
17711797
}

0 commit comments

Comments
 (0)