fix(core): prevent signature verification from silently disabling due to NaN ratio - #397
Open
aykoooo wants to merge 2 commits into
Open
fix(core): prevent signature verification from silently disabling due to NaN ratio#397aykoooo wants to merge 2 commits into
aykoooo wants to merge 2 commits into
Conversation
defaultValidationRatioFn reads this.initialValidationRatio and this.lowestValidationRatio, which are declared on NDK. It was assigned unbound and then re-bound to the NDKRelay in the relay constructor, making 'this' an NDKRelay (which has no initialValidationRatio). Result: for validatedCount < 10 the fn returned undefined; for >= 10 it returned NaN (undefined arithmetic + Math.max(NaN, 0.1)). The NaN flowed into targetValidationRatio on the 30s tick, and shouldValidateEvent() returned false forever for that relay — verification dropped to 0%, not the intended 10% floor. Bind at assignment so the relay's rebind is a no-op for the default fn. User-supplied validationRatioFn remains unbound (relay binds it, as today).
…ssion test
shouldValidateEvent previously checked only targetValidationRatio === undefined,
which left NaN (produced by the bound-to-relay defaultValidationRatioFn reading
a missing initialValidationRatio field) falling through to NaN>=1.0 (false)
and Math.random()<NaN (false), permanently disabling verification for that
relay.
Replace the undefined check with !Number.isFinite, which covers undefined,
NaN, and +/-Infinity, and fails safe (verify) instead of unsafe (skip).
Also extend validation-ratio.test.ts with regression coverage that the
existing suite lacked at validatedCount>=10:
- rebound validationRatioFn returns finite ratios in [0.1,1.0] at
counts {0,9,10,50,100,1000}, with exact values at the decay thresholds;
- the 30s updateValidationRatio timer keeps targetValidationRatio finite
and shouldValidateEvent is not permanently false;
- non-finite targetValidationRatio fails safe (verifies).
All three new tests fail on master (4b86acd) and pass with this fix.
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
defaultValidationRatioFnreadsthis.initialValidationRatio(declared onNDK) but was bound toNDKRelay, which lacks that field. OncevalidatedCount >= 10, the function returnedNaN, causingtargetValidationRatioto becomeNaNon the next 30s timer tick.shouldValidateEvent()then returnedfalseforever for that relay—verification dropped to 0% instead of the documented 10% floor.Fix
defaultValidationRatioFntoNDKat assignment (ndk/index.ts:501)!Number.isFinite()guard inshouldValidateEvent()as a fail-safe (relay/index.ts:296)Tests
Three new regression tests in
validation-ratio.test.ts:All three new tests fail on master and pass with this fix.
Security Note
This bug silently disables signature verification per-relay after just 10 validated events. Disclosed privately to maintainer prior to submission.