Skip to content

Commit

Permalink
Address issue #1222: verify that classifier and outputFormat are vali…
Browse files Browse the repository at this point in the history
…d values before returning them in headers. Should sanitize malicious output
  • Loading branch information
AngledLuffa committed Nov 26, 2021
1 parent 85e305b commit 5ee097d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/edu/stanford/nlp/ie/ner/webapp/NERServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void init() throws ServletException {
log(classifier);
}

ners = Generics.newHashMap();
ners = new HashMap<>();
for (String classifier : classifiers) {
CRFClassifier model = null;
CRFClassifier<CoreMap> model = null;
String filename = "/WEB-INF/data/models/" + classifier;
InputStream is = getServletConfig().getServletContext().getResourceAsStream(filename);

Expand Down Expand Up @@ -154,15 +154,23 @@ private void addResults(HttpServletRequest request,
classifier = this.defaultClassifier;
}

response.addHeader("classifier", classifier);
response.addHeader("outputFormat", outputFormat);
response.addHeader("preserveSpacing", String.valueOf(preserveSpacing));
CRFClassifier<CoreMap> nerModel = ners.get(classifier);
// check that we weren't asked for a classifier that doesn't exist
if (nerModel == null) {
out.print(StringEscapeUtils.escapeHtml4("Unknown model " + classifier));
return;
}

if (outputFormat.equals("highlighted")) {
outputHighlighting(out, ners.get(classifier), input);
outputHighlighting(out, nerModel, input);
} else {
out.print(StringEscapeUtils.escapeHtml4(ners.get(classifier).classifyToString(input, outputFormat, preserveSpacing)));
out.print(StringEscapeUtils.escapeHtml4(nerModel.classifyToString(input, outputFormat, preserveSpacing)));
}

response.addHeader("classifier", classifier);
// a non-existent outputFormat would have just thrown an exception
response.addHeader("outputFormat", outputFormat);
response.addHeader("preserveSpacing", String.valueOf(preserveSpacing));
}

private static void outputHighlighting(PrintWriter out,
Expand Down

0 comments on commit 5ee097d

Please sign in to comment.