Repository navigation
Reject alphabets the generator cannot sample from - #609
Merged
Merged
Conversation
ai
reviewed
Sep 1, 2026
| "name": "customAlphabet", | ||
| "import": "{ customAlphabet }", | ||
| "limit": "207 B" | ||
| "limit": "239 B" |
Owner
There was a problem hiding this comment.
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.
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
force-pushed
the
fix-unsamplable-alphabet
branch
from
September 1, 2026 12:43
ec2fd6c to
d7324e4
Compare
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
customAlphabetandcustomRandomaccept alphabets they cannot sample from, and the failure is a hang rather than an error:The non-secure build returns IDs made of the literal text
undefinedfor 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-limitbudgets -customAlphabetand non-securecustomAlphabet- and those two entries are moved to their measured sizes in this PR.nanoiditself, the headline number, is untouched.Verification: the new tests fail on
mainand pass with the fix; the fullpnpm test(oxlint, tsc, node --test with coverage, size-limit) is green.index.d.tsand 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.