Skip to content

Reject alphabets the generator cannot sample from - #609

Merged
ai merged 1 commit into
ai:mainfrom
lenamonj:fix-unsamplable-alphabet
Sep 1, 2026
Merged

ai merged 1 commit into
ai:mainfrom
lenamonj:fix-unsamplable-alphabet

Conversation

@lenamonj

@lenamonj lenamonj commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

customAlphabet and customRandom accept alphabets they cannot sample from, and the failure is a hang rather than an error:

customAlphabet('')();              // spins forever in while (true), both secure builds
customAlphabet('a'.repeat(300))(); // same hang on the node build

The non-secure build returns IDs made of the literal text undefined for an empty alphabet. Reachable from the CLI too: nanoid --alphabet "$AN_EMPTY_OR_LONG_VALUE" hangs with no output, no error, exit only by kill. The test suite has full line, branch and function coverage - every one of these sits behind a case that only ever calls the generator with size 0, which is why coverage never saw it.

Fix: each module validates the alphabet once, at the boundary the argument enters, and rejects at construction time with RangeError: Wrong alphabet size (the message style the size check already uses). The bounds follow each implementation's real contract: the secure builds require 1..256 symbols, because a random byte addresses the alphabet; the non-secure build requires only non-empty, because it indexes rather than byte-addresses and genuinely supports longer alphabets - the tests pin the positive side of each bound (a 256-symbol alphabet still generates on the secure builds, a 300-symbol one on the non-secure build), so a guard that merely rejected more would fail them.

Size: the guard grows two size-limit budgets - customAlphabet and non-secure customAlphabet - and those two entries are moved to their measured sizes in this PR. nanoid itself, the headline number, is untouched.

Verification: the new tests fail on main and pass with the fix; the full pnpm test (oxlint, tsc, node --test with coverage, size-limit) is green. index.d.ts and the README's custom-alphabet sentence are updated to state the contract.


Provenance, for transparency: this defect was found by an automated audit loop I run against open-source projects; the patch and its verification were reviewed by me before filing, and I'm happy to answer any questions during review.

Comment thread package.json Outdated
"name": "customAlphabet",
"import": "{ customAlphabet }",
"limit": "207 B"
"limit": "239 B"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is too big price for the case which we haven't in reality.

Let's just add notice to docs.

Per review: state in the custom-alphabet section that an alphabet must
contain from 1 to 256 symbols, and that a generator built outside those
bounds can loop forever instead of returning an ID.
@lenamonj

lenamonj commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Agreed - reworked to a docs-only notice. The alphabet sentence in the custom-alphabet section now reads "from 1 to 256 symbols" and warns that a generator built outside those bounds can loop forever. Code, types, tests and the size budgets are back to main; the diff is now README-only.

@lenamonj
lenamonj force-pushed the fix-unsamplable-alphabet branch from ec2fd6c to d7324e4 Compare September 1, 2026 12:43
@ai
ai merged commit 8588e3a into ai:main Sep 1, 2026
4 checks passed
@ai

ai commented Sep 1, 2026

Copy link
Copy Markdown
Owner

Thanks!

@lenamonj
lenamonj deleted the fix-unsamplable-alphabet branch September 5, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants