fix: reject an out-of-range id in toUUID instead of silently truncating - #83
Open
spokodev wants to merge 1 commit into
Open
fix: reject an out-of-range id in toUUID instead of silently truncating#83spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
`maxLength` is `ceil(128 / log2(alphabetLength))`, which rounds up, so a
maxLength-character id can encode a value larger than 2^128-1. Such an id
decodes to more than 32 hex characters, and the unanchored match in
restoreUUID silently kept the first 32 and dropped the overflow, returning
a wrong UUID:
const t = short.createTranslator(short.constants.flickrBase58);
const x = 'ZZZZZZZZZZZZZZZZZZZZZZ'; // 22 chars, decodes to > 2^128-1
t.validate(x); // true
t.fromUUID(t.toUUID(x)); // !== x (silently corrupted)
Guard the decoded hex length and throw, matching the library's existing
behaviour of throwing on invalid input. Anchor the match so the 32-hex
expectation is explicit.
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.
Problem
toUUIDcan silently return a wrong UUID. BecausemaxLengthisceil(128 / log2(alphabetLength))(rounded up), a maxLength-character id can encode a value larger than2^128 - 1. Such an id decodes to more than 32 hex characters, andrestoreUUID's unanchored match keeps only the first 32 and drops the overflow:This breaks the documented
toUUID/fromUUIDround-trip.validate(x, true)(rigorous) already returnsfalsefor these ids, so the library treats them as invalid, yettoUUIDaccepts and corrupts them.Cause
src/translate.ts:When the decoded hex is longer than 32 characters the overflow is silently discarded.
Fix
Guard the decoded length and throw (matching the library's existing behaviour of throwing on invalid input, e.g. a duplicate-character alphabet), and anchor the match so the 32-hex expectation is explicit. Normal ids (<= 2^128-1) are unaffected.
Tests
Added a case asserting
toUUIDthrows on an out-of-range id and that a normally generated id still round-trips. It fails ondevelop(the id silently truncates) and passes with the fix. Full suite green (195 tests).