|
7 | 7 |
|
8 | 8 | package org.w3c.css.servlet;
|
9 | 9 |
|
| 10 | +import org.apache.commons.validator.routines.EmailValidator; |
10 | 11 | import org.w3c.css.css.CssParser;
|
11 | 12 | import org.w3c.css.css.DocumentParser;
|
12 | 13 | import org.w3c.css.css.StyleReport;
|
|
17 | 18 | import org.w3c.css.error.ErrorReport;
|
18 | 19 | import org.w3c.css.error.ErrorReportFactory;
|
19 | 20 | import org.w3c.css.index.IndexGenerator;
|
| 21 | +import org.w3c.css.parser.CssError; |
| 22 | +import org.w3c.css.parser.Errors; |
20 | 23 | import org.w3c.css.util.ApplContext;
|
21 | 24 | import org.w3c.css.util.Codecs;
|
22 | 25 | import org.w3c.css.util.CssVersion;
|
23 | 26 | import org.w3c.css.util.FakeFile;
|
24 | 27 | import org.w3c.css.util.HTTPURL;
|
| 28 | +import org.w3c.css.util.InvalidParamException; |
25 | 29 | import org.w3c.css.util.NVPair;
|
26 | 30 | import org.w3c.css.util.Utf8Properties;
|
27 | 31 | import org.w3c.css.util.Util;
|
@@ -367,65 +371,76 @@ public void doGet(HttpServletRequest req, HttpServletResponse res)
|
367 | 371 | // " (" + req.getRemoteAddr() + ") at " + (new Date()) );
|
368 | 372 |
|
369 | 373 | if (uri != null) {
|
370 |
| - // HTML document |
371 |
| - try { |
372 |
| - uri = HTTPURL.getURL(uri).toString(); // needed to be sure |
373 |
| - // that it is a valid |
374 |
| - // url |
375 |
| - uri = uri.replaceAll(" ", "%20"); |
376 |
| - if (Util.checkURI(uri)) { |
377 |
| - DocumentParser URLparser = new DocumentParser(ac, uri); |
378 |
| - handleRequest(ac, res, uri, URLparser.getStyleSheet(), output, |
379 |
| - warningLevel, errorReport); |
380 |
| - } else { |
381 |
| - res.setHeader("Rejected", "Requested URI Forbidden by Rule"); |
382 |
| - handleError(res, ac, output, "Forbidden", new IOException( |
383 |
| - "URI Forbidden by rule"), false); |
384 |
| - } |
385 |
| - } catch (ProtocolException pex) { |
386 |
| - if (Util.onDebug) { |
387 |
| - pex.printStackTrace(); |
| 374 | + // check for scammers |
| 375 | + EmailValidator ev = EmailValidator.getInstance(); |
| 376 | + if (ev.isValid(uri)) { |
| 377 | + handleScam(ac, uri, res, output, warningLevel, errorReport); |
| 378 | + } else { |
| 379 | + // HTML document |
| 380 | + try { |
| 381 | + uri = HTTPURL.getURL(uri).toString(); // needed to be sure |
| 382 | + // that it is a valid |
| 383 | + // url |
| 384 | + uri = uri.replaceAll(" ", "%20"); |
| 385 | + if (Util.checkURI(uri)) { |
| 386 | + DocumentParser URLparser = new DocumentParser(ac, uri); |
| 387 | + handleRequest(ac, res, uri, URLparser.getStyleSheet(), output, |
| 388 | + warningLevel, errorReport); |
| 389 | + } else { |
| 390 | + res.setHeader("Rejected", "Requested URI Forbidden by Rule"); |
| 391 | + handleError(res, ac, output, "Forbidden", new IOException( |
| 392 | + "URI Forbidden by rule"), false); |
| 393 | + } |
| 394 | + } catch (ProtocolException pex) { |
| 395 | + if (Util.onDebug) { |
| 396 | + pex.printStackTrace(); |
| 397 | + } |
| 398 | + res.setHeader("WWW-Authenticate", pex.getMessage()); |
| 399 | + res.sendError(HttpServletResponse.SC_UNAUTHORIZED); |
| 400 | + } catch (Exception e) { |
| 401 | + handleError(res, ac, output, uri, e, true); |
388 | 402 | }
|
389 |
| - res.setHeader("WWW-Authenticate", pex.getMessage()); |
390 |
| - res.sendError(HttpServletResponse.SC_UNAUTHORIZED); |
391 |
| - } catch (Exception e) { |
392 |
| - handleError(res, ac, output, uri, e, true); |
393 | 403 | }
|
394 | 404 | } else if (text != null) {
|
395 | 405 | String fileName = "TextArea";
|
396 | 406 | Util.verbose("- " + fileName + " Data -");
|
397 | 407 | Util.verbose(text);
|
398 | 408 | Util.verbose("- End of " + fileName + " Data");
|
399 |
| - InputStream is = new ByteArrayInputStream(text.getBytes()); |
400 |
| - fileName = "file://localhost/" + fileName; |
| 409 | + EmailValidator ev = EmailValidator.getInstance(); |
| 410 | + if (ev.isValid(text)) { |
| 411 | + handleScam(ac, text, res, output, warningLevel, errorReport); |
| 412 | + } else { |
| 413 | + InputStream is = new ByteArrayInputStream(text.getBytes()); |
| 414 | + fileName = "file://localhost/" + fileName; |
401 | 415 |
|
402 |
| - try { |
| 416 | + try { |
403 | 417 |
|
404 |
| - if ("css".equals(type) || ("none".equals(type) && isCSS(text))) { |
405 |
| - // if CSS: |
406 |
| - parser = new StyleSheetParser(); |
407 |
| - parser.parseStyleElement(ac, is, null, usermedium, |
408 |
| - new URL(fileName), 0); |
| 418 | + if ("css".equals(type) || ("none".equals(type) && isCSS(text))) { |
| 419 | + // if CSS: |
| 420 | + parser = new StyleSheetParser(); |
| 421 | + parser.parseStyleElement(ac, is, null, usermedium, |
| 422 | + new URL(fileName), 0); |
409 | 423 |
|
410 |
| - handleRequest(ac, res, fileName, parser |
411 |
| - .getStyleSheet(), output, warningLevel, errorReport); |
412 |
| - } else { |
413 |
| - // else, trying HTML |
| 424 | + handleRequest(ac, res, fileName, parser.getStyleSheet(), |
| 425 | + output, warningLevel, errorReport); |
| 426 | + } else { |
| 427 | + // else, trying HTML |
414 | 428 | // HTMLParserStyleSheetHandler handler = new HTMLParserStyleSheetHandler(null, ac);
|
415 |
| - TagSoupStyleSheetHandler handler = new TagSoupStyleSheetHandler(null, ac); |
416 |
| - handler.parse(is, fileName); |
417 |
| - |
418 |
| - handleRequest(ac, res, fileName, handler.getStyleSheet(), output, |
419 |
| - warningLevel, errorReport); |
420 |
| - } |
421 |
| - } catch (ProtocolException pex) { |
422 |
| - if (Util.onDebug) { |
423 |
| - pex.printStackTrace(); |
| 429 | + TagSoupStyleSheetHandler handler = new TagSoupStyleSheetHandler(null, ac); |
| 430 | + handler.parse(is, fileName); |
| 431 | + |
| 432 | + handleRequest(ac, res, fileName, handler.getStyleSheet(), output, |
| 433 | + warningLevel, errorReport); |
| 434 | + } |
| 435 | + } catch (ProtocolException pex) { |
| 436 | + if (Util.onDebug) { |
| 437 | + pex.printStackTrace(); |
| 438 | + } |
| 439 | + res.setHeader("WWW-Authenticate", pex.getMessage()); |
| 440 | + res.sendError(HttpServletResponse.SC_UNAUTHORIZED); |
| 441 | + } catch (Exception e) { |
| 442 | + handleError(res, ac, output, fileName, e, false); |
424 | 443 | }
|
425 |
| - res.setHeader("WWW-Authenticate", pex.getMessage()); |
426 |
| - res.sendError(HttpServletResponse.SC_UNAUTHORIZED); |
427 |
| - } catch (Exception e) { |
428 |
| - handleError(res, ac, output, fileName, e, false); |
429 | 444 | }
|
430 | 445 | }
|
431 | 446 | Util.verbose("CssValidator: Request terminated.\n");
|
@@ -667,6 +682,12 @@ public void doPost(HttpServletRequest req, HttpServletResponse res)
|
667 | 682 | fileName = file.getName();
|
668 | 683 | Util.verbose("File : " + fileName);
|
669 | 684 | } else {
|
| 685 | + // check POSTED text for possible scam |
| 686 | + EmailValidator ev = EmailValidator.getInstance(); |
| 687 | + if (ev.isValid(text)) { |
| 688 | + handleScam(ac, text, res, output, warningLevel, errorReport); |
| 689 | + return; |
| 690 | + } |
670 | 691 | ac.setFakeText(text);
|
671 | 692 | fileName = "TextArea";
|
672 | 693 | Util.verbose("- " + fileName + " Data -");
|
@@ -724,6 +745,27 @@ public void doPost(HttpServletRequest req, HttpServletResponse res)
|
724 | 745 | Util.verbose("CssValidator: Request terminated.\n");
|
725 | 746 | }
|
726 | 747 |
|
| 748 | + private void handleScam(ApplContext ac, String uri, HttpServletResponse res, String output, |
| 749 | + int warningLevel, boolean errorReport) |
| 750 | + throws IOException { |
| 751 | + // so it is an email and not a URL, do something clever. |
| 752 | + String fileName = "email"; |
| 753 | + InputStream is = new ByteArrayInputStream("".getBytes()); |
| 754 | + fileName = "file://" + fileName; |
| 755 | + try { |
| 756 | + TagSoupStyleSheetHandler handler = new TagSoupStyleSheetHandler(null, ac); |
| 757 | + handler.parse(is, fileName); |
| 758 | + // add a warning |
| 759 | + Errors e = new Errors(); |
| 760 | + e.addError(new CssError(new InvalidParamException("email", uri, ac))); |
| 761 | + handler.getStyleSheet().addErrors(e); |
| 762 | + handleRequest(ac, res, fileName, handler.getStyleSheet(), output, |
| 763 | + warningLevel, errorReport); |
| 764 | + } catch (Exception e) { |
| 765 | + handleError(res, ac, output, fileName, e, false); |
| 766 | + } |
| 767 | + } |
| 768 | + |
727 | 769 | private void handleRequest(ApplContext ac, HttpServletResponse res,
|
728 | 770 | String title, StyleSheet styleSheet,
|
729 | 771 | String output, int warningLevel,
|
|
0 commit comments