From 05455c7b68ac15a29a1e21ed1e798e133d205c2e Mon Sep 17 00:00:00 2001 From: srowen Date: Sat, 9 Jul 2011 09:23:23 +0000 Subject: [PATCH] Issue 897 tighten URI definition git-svn-id: https://zxing.googlecode.com/svn/trunk@1851 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/result/URIResultParser.java | 22 +++++++++++++------ .../result/URIParsedResultTestCase.java | 9 ++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/src/com/google/zxing/client/result/URIResultParser.java b/core/src/com/google/zxing/client/result/URIResultParser.java index 6d72dc62d..c8dcc5239 100644 --- a/core/src/com/google/zxing/client/result/URIResultParser.java +++ b/core/src/com/google/zxing/client/result/URIResultParser.java @@ -49,17 +49,25 @@ final class URIResultParser extends ResultParser { * need to know when a string is obviously not a URI. */ static boolean isBasicallyValidURI(String uri) { - if (uri == null || uri.indexOf(' ') >= 0 || uri.indexOf('\n') >= 0) { + if (uri == null) { return false; } + int period = -1; + int colon = -1; + int length = uri.length(); + for (int i = length - 1; i >= 0; i--) { + char c = uri.charAt(i); + if (c <= ' ') { // covers space, newline, and more + return false; + } else if (c == '.') { + period = i; + } else if (c == ':') { + colon = i; + } + } // Look for period in a domain but followed by at least a two-char TLD // Forget strings that don't have a valid-looking protocol - int period = uri.indexOf('.'); - if (period >= uri.length() - 2) { - return false; - } - int colon = uri.indexOf(':'); - if (period < 0 && colon < 0) { + if (period >= uri.length() - 2 || (period < 0 && colon < 0)) { return false; } if (colon >= 0) { diff --git a/core/test/src/com/google/zxing/client/result/URIParsedResultTestCase.java b/core/test/src/com/google/zxing/client/result/URIParsedResultTestCase.java index d1c607fd4..59e472c0b 100644 --- a/core/test/src/com/google/zxing/client/result/URIParsedResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/URIParsedResultTestCase.java @@ -65,6 +65,15 @@ public final class URIParsedResultTestCase extends Assert { assertEquals(text, result.getDisplayResult()); } + @Test + public void testGarbage2() { + String text = "DEA\u0003\u0019M\u0006\u0000\bå\u0000‡HO\u0000X$\u0001\u0000\u001Fwfc\u0007!þ“˜\u0013\u0013¾Z{ùÎÝڗZ§¨+y_zbñk\u00117¸\u000E†Ü\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000£.ux"; + Result fakeResult = new Result(text, null, null, BarcodeFormat.QR_CODE); + ParsedResult result = ResultParser.parseResult(fakeResult); + assertSame(ParsedResultType.TEXT, result.getType()); + assertEquals(text, result.getDisplayResult()); + } + @Test public void testIsPossiblyMalicious() { doTestIsPossiblyMalicious("http://google.com", false);