Skip to content

Commit 12ec43e

Browse files
adamthom-amznsrchase
authored andcommitted
fix(codegen): escape regex literals in path segments
We use regex to extract labeled path values, and literal path segments can contain unescaped regex literals that can both blow up deserialization and present ReDoS risks. While it's unlikely we will see these paths in practice, we should still escape special regex characters.
1 parent af5f4ee commit 12ec43e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
7575
import software.amazon.smithy.utils.ListUtils;
7676
import software.amazon.smithy.utils.OptionalUtils;
77+
import software.amazon.smithy.utils.SetUtils;
7778
import software.amazon.smithy.utils.SmithyUnstableApi;
7879

7980
/**
@@ -83,6 +84,8 @@
8384
public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator {
8485

8586
private static final Logger LOGGER = Logger.getLogger(HttpBindingProtocolGenerator.class.getName());
87+
private static final Set<Character> REGEX_CHARS = SetUtils.of('.', '*', '+', '?', '^', '$', '{', '}', '(',
88+
')', '|', '[', ']', '\\');
8689

8790
private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
8891
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
@@ -1921,7 +1924,14 @@ private void readPath(
19211924
}
19221925
pathRegexBuilder.append(")");
19231926
} else {
1924-
pathRegexBuilder.append(segment.getContent());
1927+
segment.getContent()
1928+
.chars()
1929+
.forEach(c -> {
1930+
if (REGEX_CHARS.contains((char) c)) {
1931+
pathRegexBuilder.append('\\');
1932+
}
1933+
pathRegexBuilder.append((char) c);
1934+
});
19251935
}
19261936
}
19271937
writer.write("const pathRegex = new RegExp($S);", pathRegexBuilder.toString());

0 commit comments

Comments
 (0)