Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codegen): escape regex literals in path segments #477

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.OptionalUtils;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
Expand All @@ -83,6 +84,8 @@
public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator {

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

private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
Expand Down Expand Up @@ -1921,7 +1924,14 @@ private void readPath(
}
pathRegexBuilder.append(")");
} else {
pathRegexBuilder.append(segment.getContent());
segment.getContent()
.chars()
.forEach(c -> {
if (REGEX_CHARS.contains((char) c)) {
pathRegexBuilder.append('\\');
}
pathRegexBuilder.append((char) c);
});
}
}
writer.write("const pathRegex = new RegExp($S);", pathRegexBuilder.toString());
Expand Down