Issue 897 tighten URI definition

git-svn-id: https://zxing.googlecode.com/svn/trunk@1851 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-07-09 09:23:23 +00:00
parent 91b55eb5e5
commit 05455c7b68
2 changed files with 24 additions and 7 deletions

View file

@ -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) {

View file

@ -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);