fix(core): tighten isEvent type-guard against type confusion (#60)#86
Open
bnuckols13 wants to merge 1 commit into
Open
fix(core): tighten isEvent type-guard against type confusion (#60)#86bnuckols13 wants to merge 1 commit into
bnuckols13 wants to merge 1 commit into
Conversation
) isEvent validated `id` and `created_at` by length/comparison without checking their type, so a non-string `id` of length 64 (e.g. an array) and a non-integer `created_at` (0.5, Infinity) passed the guard and reached code that assumes a well-formed NostrEvent. Require `id` to be a string and `created_at` to be an integer, and add regression tests (isEvent previously had none). isEvent stays signature-agnostic per its docstring; sig-length policy belongs in a verifying path, since isValidNostrWebToken relies on the structural-only contract to check tokens before verification.
|
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.
Closes #60.
isEvent()guards most event handling in the library, but two of its checks read a property'slengthwithout first checking the value's type, so non-event objects pass the guard and reach code that assumes a well-formedNostrEvent.Reproduced on
master(98875b1f):Fix (
packages/core/src/helpers/event.ts): requireidto be astringbefore trusting its length, and requirecreated_atto be an integer.A regression test covering both bypasses (plus the valid and null/undefined cases) is added to
packages/core/src/helpers/__tests__/events.test.ts, which previously had no coverage forisEvent. It fails on the unpatched guard and passes with the fix; the rest of thevitestsuite is unchanged.On
siglength (issue point ②): I did not addevent.sig.length === 128.isEvent's docstring states it does not validate the signature, andisValidNostrWebToken(packages/common) relies on that contract to check token structure before the signature is verified (its fixture carries a placeholdersig). Requiring a 128-character signature inside the structural guard breaks that usage, so signature length and hex-format validation are better placed in a signature-checking path than inisEvent. This keeps the change scoped to the type-confusion holes and leaves the guard's documented contract intact. Happy to add stricter hex/format validation in a follow-up if you'd prefer it live here.