feat(nwc): validate wallet connect URI pubkey, relays, and secret#544
feat(nwc): validate wallet connect URI pubkey, relays, and secret#544DSanich wants to merge 3 commits intogetAlby:masterfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded robust NWC URI parsing and normalization: relay URL trimming/validation, wallet pubkey normalization (hex/bech32 rejection cases tested), secret normalization (hex and nsec), and an optional parse option to require or allow missing secrets; constructor and URL builder updated to enforce secret presence where applicable. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/nwc/NWCClient.test.ts`:
- Around line 70-166: Tests embed a real-looking hardcoded wallet-connect secret
repeatedly; replace those literals with a synthetic/generated fixture and build
URLs programmatically. Create a shared test fixture (e.g., generateSecretKey() +
bytesToHex(...) or nip19.nsecEncode(...) used in the first test) and reuse it
when constructing URLs for the tests that call NWCClient.parseWalletConnectUrl
and NWCClient constructor; update all occurrences that currently hardcode the
secret string so they interpolate the fixture instead, ensuring tests still
cover hex and nsec normalization, missing/invalid secret, and
parseWalletConnectUrl options.
In `@src/nwc/NWCClient.ts`:
- Around line 156-159: The new parse-only constructor path allows creating an
NWCClient with secret === undefined (via
parseWalletConnectUrlOptions.requireSecret = false), but NWCClient methods like
getNostrWalletConnectUrl() and other secret-dependent APIs are not handling that
and will serialize "&secret=undefined" or crash; update the NWCClient
constructor and relevant methods (constructor, getNostrWalletConnectUrl, and any
secret-dependent operations referenced around the parse logic) to enforce one of
two behaviors: either reject/throw if an instance is constructed without a
secret (when methods that need it are called) or clearly mark/guard the instance
as "secretless" and make getNostrWalletConnectUrl() and other serializers omit
the secret (not serialize undefined) and throw early with a clear error when
secret-required operations are invoked; implement the chosen approach
consistently for parseWalletConnectUrlOptions and requireSecret handling so no
instance can accidentally serialize "&secret=undefined" or perform secret-only
ops without an explicit runtime check.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 17bbfa96-3fb5-47ae-ae06-60cfdfbbd665
📒 Files selected for processing (2)
src/nwc/NWCClient.test.tssrc/nwc/NWCClient.ts
|
Thanks for the PR! There is a lot of excessive code here I think - it should follow the NIP-47 spec and not do extra stuff like npub parsing or relay URLs starting with http. |
|
@rolznz Thanks for review! Working on it |
Summary
Improves validation when parsing Nostr Wallet Connect (
nostr+walletconnect/ legacynostrwalletconnect) connection strings inNWCClient.parseWalletConnectUrl, closing the gaps described in #539.Changes
url.host): must be either a 64-character hex string or a validnpub(decoded vianip19).relayafter trimming; each value must parse as a URL with an allowed scheme (wss,ws,https,http). Fixes the previous check that never fired for an emptygetAll("relay")result.secret— either 64 hex chars ornsec;nsecis normalized to hex so the rest of the client (e.g.hexToBytes/ signing) stays consistent.ParseWalletConnectUrlOptions: optionalrequireSecret(defaulttrue). Exposed onNewNWCClientOptionsasparseWalletConnectUrlOptionswhen constructing fromnostrWalletConnectUrl.Closes #539
Summary by CodeRabbit