Skip to content

Commit c87b149

Browse files
authored
fix: Tighten key2Re regex with non-capturing group (#2225)
1 parent 6d4bb5e commit c87b149

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/util/minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ util.Long = /* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore
186186
* @type {RegExp}
187187
* @const
188188
*/
189-
util.key2Re = /^true|false|0|1$/;
189+
util.key2Re = /^(?:true|false|0|1)$/;
190190

191191
/**
192192
* Regular expression used to verify 32 bit (`int32` etc.) map keys.

tests/api_util.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,21 @@ tape.test("util", function(test) {
169169
test.end();
170170
});
171171

172+
test.test(test.name + " - key2Re", function(test) {
173+
[ "true", "false", "0", "1" ].forEach(function(k) {
174+
test.ok(util.key2Re.test(k), "should accept canonical bool key " + JSON.stringify(k));
175+
});
176+
[
177+
"TRUE", "FALSE", "True",
178+
"yes", "no",
179+
"trueblue", "true-yes", "blah-false-blah",
180+
"x100x", "abc1", "100", "0x0", "01",
181+
"true ", " true", "", "truefalse"
182+
].forEach(function(k) {
183+
test.notOk(util.key2Re.test(k), "should reject non-canonical bool key " + JSON.stringify(k));
184+
});
185+
test.end();
186+
});
187+
172188
test.end();
173189
});

0 commit comments

Comments
 (0)