Repository navigation
fix: validate alphabets for duplicates, surrogates, and 256-character limit (#54) - #58
Open
sagargupta6259 wants to merge 1 commit into
Open
sagargupta6259 wants to merge 1 commit into
sagargupta6259 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Description
Closes #54.
Currently, custom alphabets passed into generators and entropy utilities can accept invalid inputs—such as duplicate characters, surrogate code units (e.g., emoji halves), or lengths exceeding 256 characters—which silently degrade randomness, cause biased IDs, or trigger infinite loops during rejection sampling.
This pull request centralizes alphabet validation into a shared internal helper and enforces consistent invariants across all public APIs that accept an
alphabetoption.Changes
src/internal/alphabet.ts):assertAlphabet(alphabet)helper.RangeError: prefid:alphabetmust contain at least 2 characters.).RangeError: prefid:alphabetmust not contain more than 256 characters.).RangeError: prefid:alphabetmust not contain surrogate code units.).RangeError: prefid:alphabetmust not contain duplicate characters.).assertAlphabetincreateId(src/generators/generate.ts),createSortableId(src/generators/sortable.ts), andtemplate(src/generators/template.ts).templateplaceholder validation to ensure single BMP characters (RangeError: prefid:placeholdermust be a single BMP character.).assertAlphabetconsistently across entropy utilities insrc/utils/entropy.ts(entropyBits,sizeForEntropy, andcollisionProbability).README.md.[Unreleased]inCHANGELOG.md.test/alphabet.test.ts.test/generate.test.ts,test/sortable.test.ts,test/template.test.ts, andtest/entropy.test.ts.Verification
npm test: 11 test files, 172/172 tests passed.npm run verify: Full verification suite green (100% coverage onalphabet.ts,publint --strictpassed,attw --packpassed, ESM/CJS smoke tests passed).