Skip to content

Commit f52b578

Browse files
committed
check for an invalid id with a repeating reserved character
1 parent d33db79 commit f52b578

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ export default class Sqids {
244244
const chunks = id.split(separator);
245245
if (chunks.length) {
246246
// decode the number without using the `separator` character
247+
// but also check that ID can be decoded (eg: does not contain any non-alphabet characters)
247248
const alphabetWithoutSeparator = alphabet.slice(0, -1);
249+
for (const c of chunks[0]) {
250+
if (!alphabetWithoutSeparator.includes(c)) {
251+
return [];
252+
}
253+
}
248254
ret.push(this.toNumber(chunks[0], alphabetWithoutSeparator));
249255

250256
// if this ID has multiple numbers, shuffle the alphabet because that's what encoding function did

tests/encoding.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ test('decoding an ID with an invalid character', () => {
113113
expect.soft(sqids.decode('*')).toEqual([]);
114114
});
115115

116+
test('decoding an invalid ID with a repeating reserved character', () => {
117+
const sqids = new Sqids();
118+
expect.soft(sqids.decode('fff')).toEqual([]);
119+
});
120+
116121
test.fails('encode out-of-range numbers', () => {
117122
const sqids = new Sqids();
118123
expect(sqids.encode([sqids.minValue() - 1])).rejects;

0 commit comments

Comments
 (0)