Skip to content

Commit 5056bab

Browse files
adamthom-amznsrchase
authored andcommitted
Switch to re2 for pattern validation
Standard RegExp is vulnerable to ReDoS, which means SSDK users have to be careful when crafting patterns constraints. re2 does not have this problem, at the expense of not supporting lookahead or backreferences.
1 parent 3bbd4de commit 5056bab

File tree

4 files changed

+336
-13
lines changed

4 files changed

+336
-13
lines changed

smithy-typescript-ssdk-libs/server-common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"@aws-sdk/protocol-http": "3.34.0",
2323
"@aws-sdk/smithy-client": "3.34.0",
2424
"@aws-sdk/types": "3.34.0",
25-
"tslib": "^1.8.0"
25+
"tslib": "^1.8.0",
26+
"re2": "^1.16.0"
2627
},
2728
"devDependencies": {
2829
"@types/jest": "^26.0.22",

smithy-typescript-ssdk-libs/server-common/src/validation/validators.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ describe("pattern validation", () => {
142142
path: "aField",
143143
});
144144
});
145+
it("is not vulnerable to ReDoS", () => {
146+
const validator = new PatternValidator("^([0-9]+)+$");
147+
expect(
148+
validator.validate(
149+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000!",
150+
"aField"
151+
)
152+
).toEqual({
153+
constraintType: "pattern",
154+
constraintValues: "^([0-9]+)+$",
155+
failureValue: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000!",
156+
path: "aField",
157+
});
158+
});
145159
});
146160

147161
describe("range validation", () => {

smithy-typescript-ssdk-libs/server-common/src/validation/validators.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
import RE2 from "re2";
17+
1618
import {
1719
EnumValidationFailure,
1820
LengthValidationFailure,
@@ -257,11 +259,11 @@ export class RangeValidator implements SingleConstraintValidator<number, RangeVa
257259

258260
export class PatternValidator implements SingleConstraintValidator<string, PatternValidationFailure> {
259261
private readonly inputPattern: string;
260-
private readonly pattern: RegExp;
262+
private readonly pattern: RE2;
261263

262264
constructor(pattern: string) {
263265
this.inputPattern = pattern;
264-
this.pattern = new RegExp(pattern, "u");
266+
this.pattern = new RE2(pattern, "u");
265267
}
266268

267269
validate(input: string | undefined | null, path: string): PatternValidationFailure | null {

0 commit comments

Comments
 (0)