Skip to content

Commit d558d95

Browse files
manningStanford NLP
authored andcommitted
Fix the next sutime XSS
1 parent 85ef586 commit d558d95

2 files changed

Lines changed: 28 additions & 30 deletions

File tree

src/edu/stanford/nlp/time/suservlet/SUTimePipeline.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
package edu.stanford.nlp.time.suservlet;
2-
import edu.stanford.nlp.util.logging.Redwood;
1+
package edu.stanford.nlp.time.suservlet;
32

43
import java.io.BufferedReader;
54
import java.io.IOException;
@@ -14,6 +13,7 @@
1413
import edu.stanford.nlp.time.HeidelTimeAnnotator;
1514
import edu.stanford.nlp.time.TimeAnnotator;
1615
import edu.stanford.nlp.time.TimeAnnotations;
16+
import edu.stanford.nlp.util.logging.Redwood;
1717

1818
public class SUTimePipeline {
1919

@@ -26,7 +26,7 @@ public SUTimePipeline() {
2626
}
2727

2828
public SUTimePipeline(Properties props) {
29-
// By default, we want to tokenize the text, split it into
29+
// By default, we want to tokenize the text, split it into
3030
// sentences, and then put it through the sutime annotator.
3131
// We also want to pos tag it and put it through the number and
3232
// qen annotators.
@@ -36,7 +36,7 @@ public SUTimePipeline(Properties props) {
3636
// This should be inexpensive.
3737

3838
if (props.getProperty("annotators") == null) {
39-
props.setProperty("annotators",
39+
props.setProperty("annotators",
4040
"tokenize, ssplit, pos");
4141
// "tokenize, ssplit, pos, number, qen");
4242
}
@@ -85,7 +85,7 @@ public Annotation process(String sentence, String dateString, Annotator timeAnno
8585
return anno;
8686
}
8787

88-
static public void main(String[] args) throws IOException {
88+
public static void main(String[] args) throws IOException {
8989
SUTimePipeline pipeline = new SUTimePipeline();
9090
Annotator timeAnnotator = pipeline.getTimeAnnotator("sutime", new Properties());
9191
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
@@ -96,4 +96,5 @@ static public void main(String[] args) throws IOException {
9696
System.out.print("> ");
9797
}
9898
}
99+
99100
}

src/edu/stanford/nlp/time/suservlet/SUTimeServlet.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
import edu.stanford.nlp.time.Timex;
1919
import edu.stanford.nlp.util.CoreMap;
2020

21+
import edu.stanford.nlp.util.StringUtils;
2122
import org.apache.commons.lang3.StringEscapeUtils;
2223

23-
public class SUTimeServlet extends HttpServlet
24-
{
25-
SUTimePipeline pipeline = null;
24+
public class SUTimeServlet extends HttpServlet {
2625

27-
public void init()
28-
throws ServletException
29-
{
26+
private SUTimePipeline pipeline; // = null;
27+
28+
@Override
29+
public void init() throws ServletException {
3030
String dataDir = getServletContext().getRealPath("/WEB-INF/data");
3131
String taggerFilename = dataDir + "/english-left3words-distsim.tagger";
3232
Properties pipelineProps = new Properties();
@@ -37,18 +37,17 @@ public void init()
3737
}
3838

3939
public static boolean parseBoolean(String value) {
40-
if (value == null || value.equals("")) {
40+
if (StringUtils.isNullOrEmpty(value)) {
4141
return false;
4242
}
4343
if (value.equalsIgnoreCase("on")) {
4444
return true;
4545
}
46-
return Boolean.valueOf(value);
46+
return Boolean.parseBoolean(value);
4747
}
4848

4949
public void doGet(HttpServletRequest request, HttpServletResponse response)
50-
throws ServletException, IOException
51-
{
50+
throws ServletException, IOException {
5251
if (request.getCharacterEncoding() == null) {
5352
request.setCharacterEncoding("utf-8");
5453
}
@@ -61,9 +60,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
6160
include(request, response);
6261
}
6362

63+
@Override
6464
public void doPost(HttpServletRequest request, HttpServletResponse response)
65-
throws ServletException, IOException
66-
{
65+
throws ServletException, IOException {
6766
doGet(request, response);
6867
}
6968

@@ -79,8 +78,7 @@ private String getRuleFilepaths(String... files) {
7978
return sb.toString();
8079
}
8180

82-
private Properties getTimeAnnotatorProperties(HttpServletRequest request)
83-
{
81+
private Properties getTimeAnnotatorProperties(HttpServletRequest request) {
8482
// Parses request and set up properties for time annotators
8583
boolean markTimeRanges =
8684
parseBoolean(request.getParameter("markTimeRanges"));
@@ -93,9 +91,8 @@ private Properties getTimeAnnotatorProperties(HttpServletRequest request)
9391
String heuristicLevel = request.getParameter("relativeHeuristicLevel");
9492
Options.RelativeHeuristicLevel relativeHeuristicLevel =
9593
Options.RelativeHeuristicLevel.NONE;
96-
if (heuristicLevel != null && !heuristicLevel.equals("")) {
97-
relativeHeuristicLevel =
98-
Options.RelativeHeuristicLevel.valueOf(heuristicLevel);
94+
if ( ! StringUtils.isNullOrEmpty(heuristicLevel)) {
95+
relativeHeuristicLevel = Options.RelativeHeuristicLevel.valueOf(heuristicLevel);
9996
}
10097
String ruleFile = null;
10198
if (readRules) {
@@ -132,7 +129,7 @@ private Properties getTimeAnnotatorProperties(HttpServletRequest request)
132129
return props;
133130
}
134131

135-
private void displayAnnotation(PrintWriter out, String query, Annotation anno, boolean includeOffsets) {
132+
private static void displayAnnotation(PrintWriter out, String query, Annotation anno, boolean includeOffsets) {
136133
List<CoreMap> timexAnns = anno.get(TimeAnnotations.TimexAnnotations.class);
137134
List<String> pieces = new ArrayList<>();
138135
List<Boolean> tagged = new ArrayList<>();
@@ -214,24 +211,23 @@ private void displayAnnotation(PrintWriter out, String query, Annotation anno, b
214211

215212
private void addResults(HttpServletRequest request,
216213
HttpServletResponse response)
217-
throws IOException
218-
{
214+
throws IOException {
219215
// if we can't handle UTF-8, need to do something like this...
220216
//String originalQuery = request.getParameter("q");
221217
//String query = WebappUtil.convertString(originalQuery);
222-
218+
223219
String query = request.getParameter("q");
224220
String dateString = request.getParameter("d");
225221
// TODO: this always returns true...
226-
boolean dateError = !pipeline.isDateOkay(dateString);
222+
boolean dateError = ! pipeline.isDateOkay(dateString);
227223
boolean includeOffsets = parseBoolean(request.getParameter("includeOffsets"));
228224
PrintWriter out = response.getWriter();
229225
if (dateError) {
230-
out.println("<br><br>Warning: unparseable date " +
226+
out.println("<br><br>Warning: unparseable date " +
231227
StringEscapeUtils.escapeHtml4(dateString));
232228
}
233229

234-
if (query != null && !query.equals("")) {
230+
if ( ! StringUtils.isNullOrEmpty(query)) {
235231
Properties props = getTimeAnnotatorProperties(request);
236232
String annotatorType = request.getParameter("annotator");
237233
if (annotatorType == null) {
@@ -243,7 +239,7 @@ private void addResults(HttpServletRequest request,
243239
out.println("<h3>Annotated Text</h3> <em>(tagged using " + annotatorType + "</em>)");
244240
displayAnnotation(out, query, anno, includeOffsets);
245241
} else {
246-
out.println("<br><br>Error creating annotator for " + annotatorType);
242+
out.println("<br><br>Error creating annotator for " + StringEscapeUtils.escapeHtml4(annotatorType));
247243
}
248244

249245

@@ -252,4 +248,5 @@ private void addResults(HttpServletRequest request,
252248
}
253249

254250
private static final long serialVersionUID = 1L;
251+
255252
}

0 commit comments

Comments
 (0)