Skip to content

Commit

Permalink
Test unicode smuggling
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Dec 24, 2023
1 parent e145449 commit 18cb80e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 4 additions & 3 deletions mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.google.mu.safesql;

import static com.google.common.base.CharMatcher.anyOf;
import static com.google.common.base.CharMatcher.ascii;
import static com.google.common.base.CharMatcher.is;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -19,7 +21,6 @@
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.CompileTimeConstant;
import com.google.errorprone.annotations.Immutable;
import com.google.mu.util.CharPredicate;
import com.google.mu.util.StringFormat;
import com.google.mu.util.Substring;

Expand Down Expand Up @@ -233,7 +234,7 @@ private static String quotedBy(char quoteChar, Substring.Match placeholder, Obje
quoteChar,
placeholder,
quoteChar);
return first(CharPredicate.is('\\').or(quoteChar).or('\n').or('\r').or(ascii().negate()::matches))
return first(anyOf("\\\n\r").or(is(quoteChar)).or(ascii().negate())::matches)
.repeatedly()
.replaceAllFrom(
value.toString(),
Expand All @@ -257,7 +258,7 @@ private static String backquoted(Substring.Match placeholder, Object value) {
"placeholder value for `%s` (%s) contains illegal character",
placeholder,
name);
return Substring.first(CharMatcher.ascii().negate()::matches)
return first(CharMatcher.ascii().negate()::matches)
.repeatedly()
.replaceAllFrom(name, uni -> toUnicodeHex(uni.charAt(0)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,13 @@ public void trustedSqlStringShouldNotBeDoubleQuoted() {

@Test
public void unicodeSmugglingInStringLiteralNotEffective() {
String input = "ʻ OR TRUE OR ʼʼ=ʼ";
SafeQuery query = template("'{id}'").with(input);
SafeQuery query = template("'{id}'").with("ʻ OR TRUE OR ʼʼ=ʼ");
assertThat(query.toString()).isEqualTo("'\\u02BB" + " OR TRUE OR \\u02BC\\u02BC=\\u02BC'");
}

@Test
public void unicodeSmugglingInIdentifierNotEffective() {
String input = "ʻ OR TRUE OR ʼʼ=ʼ";
SafeQuery query = template("`{tbl}`").with(input);
SafeQuery query = template("`{tbl}`").with("ʻ OR TRUE OR ʼʼ=ʼ");
assertThat(query.toString()).isEqualTo("`\\u02BB" + " OR TRUE OR \\u02BC\\u02BC=\\u02BC`");
}

Expand Down

0 comments on commit 18cb80e

Please sign in to comment.