Skip to content

Commit

Permalink
check for an invalid id with a repeating reserved character
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Aug 12, 2023
1 parent d33db79 commit f52b578
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ export default class Sqids {
const chunks = id.split(separator);
if (chunks.length) {
// decode the number without using the `separator` character
// but also check that ID can be decoded (eg: does not contain any non-alphabet characters)
const alphabetWithoutSeparator = alphabet.slice(0, -1);
for (const c of chunks[0]) {
if (!alphabetWithoutSeparator.includes(c)) {
return [];
}
}
ret.push(this.toNumber(chunks[0], alphabetWithoutSeparator));

// if this ID has multiple numbers, shuffle the alphabet because that's what encoding function did
Expand Down
5 changes: 5 additions & 0 deletions tests/encoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ test('decoding an ID with an invalid character', () => {
expect.soft(sqids.decode('*')).toEqual([]);
});

test('decoding an invalid ID with a repeating reserved character', () => {
const sqids = new Sqids();
expect.soft(sqids.decode('fff')).toEqual([]);
});

test.fails('encode out-of-range numbers', () => {
const sqids = new Sqids();
expect(sqids.encode([sqids.minValue() - 1])).rejects;
Expand Down

0 comments on commit f52b578

Please sign in to comment.