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

Improve sanitization of broken comments. #382

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,20 @@ public int read(char[] cbuf, int off, int len) throws IOException {
}

if (cbuf[startElem + 1] == '!') {
boolean illegal = (
// Comment?
(cbuf[startElem + 2] == '-' && cbuf[startElem + 3] != '-')
// Doctype?
|| ((cbuf[startElem + 2] == 'D' || cbuf[startElem + 2] == 'd')
&& (endElem - startElem) < 12)
// CDATA?
|| (cbuf[startElem + 2] == '[' && (endElem - startElem) < 10));
if (illegal) {
boolean isComment = isLegalComment(cbuf, startElem, endElem);
boolean isDoctype =
(cbuf[startElem + 2] == 'D' || cbuf[startElem + 2] == 'd')
&& ((endElem - startElem) >= 12);
boolean isCdata = (cbuf[startElem + 2] == '[') && ((endElem - startElem) >= 10);
if (!isComment && !isDoctype && !isCdata) {
cbuf[startElem] = '_';
cbuf[endElem] = '-';
cbuf[endElem] = '_';
schmika marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
}
}

if (cbuf[startElem + 1] == '!' && cbuf[startElem + 2] == '-'
&& cbuf[startElem + 3] == '-') {
// Comment, nothing to do
continue;
}
} else if (cbuf[startElem + 1] == '!' && cbuf[startElem + 2] == '-' && cbuf[startElem + 3] == '-') {
// Comment, nothing to do
if (isLegalComment(cbuf, startElem, endElem)) {
continue;
}

Expand Down Expand Up @@ -363,6 +355,13 @@ private static boolean isLegalEntity(char[] cbuf, int startIdx, int endIdx) {
return true;
}

private static boolean isLegalComment(char[] cbuf, int startIdx, int endIdx) {
if (cbuf[startIdx + 1] != '!' || cbuf[startIdx + 2] != '-' || cbuf[startIdx + 3] != '-') {
return false;
}
return cbuf[endIdx - 2] == '-' && cbuf[endIdx - 1] == '-';
}

@Override
public Optional<String> getSource() {
if (this.input instanceof SourceAwareReader) {
Expand Down
29 changes: 28 additions & 1 deletion src/test/java/com/github/dbmdz/solrocr/solr/HocrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ public void testBrokenEntities() throws IOException {
"contains(.//lst[@name='87372']//arr[@name='snippets']/lst/str[@name='text']/text(), 'dt_HiFi-i?cBßflpedx1ttonI-iii;_;ikW')");
}


public void testBrokenPIs() throws IOException {
Path ocrPath = Paths.get("src/test/resources/data/hocr_broken_pis.html");
assertU(
Expand Down Expand Up @@ -589,6 +588,34 @@ public void testBrokenPIs() throws IOException {
"contains(.//lst[@name='87373']//arr[@name='snippets']/lst/str[@name='text']/text(), '_?«»«?_i_5t»_?».')");
}

public void testBrokenComment() throws IOException {
Path ocrPath = Paths.get("src/test/resources/data/hocr_broken_comment.html");
assertU(
adoc(
"ocr_text_stored",
new String(Files.readAllBytes(ocrPath), StandardCharsets.UTF_8),
"id",
"87374"));
assertU(commit());
SolrQueryRequest req =
xmlQ(
"q",
"eiiigekaufs",
"hl.snippets",
"4096",
"hl.weightMatches",
"true",
"hl.ocr.contextSize",
"4",
"df",
"ocr_text_stored",
"hl.ocr.fl",
"ocr_text_stored");
assertQ(
req,
"contains(.//lst[@name='87374']//arr[@name='snippets']/lst/str[@name='text']/text(), \"!'_!--,,,_\")");
}

public void testHlQParam() {
SolrQueryRequest req = xmlQ("q", "ocr_text:\"nathanael brush\"", "hl.q", "nathanael");
assertQ(
Expand Down
Loading