-
Notifications
You must be signed in to change notification settings - Fork 755
fix(base-account): parse the SIWE nonce correctly and bind verification to the app domain #1794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dusk1e
wants to merge
1
commit into
base:master
Choose a base branch
from
Dusk1e:fix/siwe-domain-binding-and-nonce-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,12 +147,22 @@ try { | |
| ```ts Backend (Viem) | ||
| import { createPublicClient, http } from 'viem'; | ||
| import { base } from 'viem/chains'; | ||
| import { verifySiweMessage } from 'viem/siwe'; | ||
|
|
||
| // The domain your app is served from. Anything signed for another | ||
| // domain must not be accepted here. | ||
| const APP_DOMAIN = 'yourapp.com'; | ||
|
|
||
| const client = createPublicClient({ chain: base, transport: http() }); | ||
|
|
||
| export async function verifySig(req, res) { | ||
| const { address, message, signature } = req.body; | ||
| const valid = await client.verifyMessage({ address, message, signature }); | ||
| const valid = await verifySiweMessage(client, { | ||
| address, | ||
| message, | ||
| signature, | ||
| domain: APP_DOMAIN, | ||
| }); | ||
| if (!valid) return res.status(401).json({ error: 'Invalid signature' }); | ||
| // create session / JWT | ||
| res.json({ ok: true }); | ||
|
|
@@ -178,20 +188,33 @@ export async function verifySig(req, res) { | |
| regardless of where it originated. | ||
| </Note> | ||
|
|
||
| <Warning> | ||
| Always check the `domain` field of the signed message against your own domain. | ||
| A signature is valid for whichever domain it was signed for, so a signature a | ||
| user produced on another site is cryptographically valid on yours too. | ||
| `verifySiweMessage` performs this check when you pass `domain`; a bare | ||
| `verifyMessage` call does not, and accepts the signature. | ||
| </Warning> | ||
|
|
||
| ### Example Express Server | ||
|
|
||
| ```ts title="server/auth.ts" expandable | ||
| import crypto from "crypto"; | ||
| import express from "express"; | ||
| import { createPublicClient, http } from "viem"; | ||
| import { base } from "viem/chains"; | ||
| import { parseSiweMessage, verifySiweMessage } from "viem/siwe"; | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Simple in-memory nonce store (swap for Redis or DB in production) | ||
| const nonces = new Set<string>(); | ||
|
|
||
| // The domain your app is served from. Anything signed for another | ||
| // domain must not be accepted here. | ||
| const APP_DOMAIN = "yourapp.com"; | ||
|
|
||
| app.get("/auth/nonce", (_, res) => { | ||
| const nonce = crypto.randomBytes(16).toString("hex"); | ||
| nonces.add(nonce); | ||
|
|
@@ -203,14 +226,19 @@ const client = createPublicClient({ chain: base, transport: http() }); | |
| app.post("/auth/verify", async (req, res) => { | ||
| const { address, message, signature } = req.body; | ||
|
|
||
| // 1. Check nonce hasn\'t been reused | ||
| const nonce = message.match(/at (\w{32})$/)?.[1]; | ||
| // 1. Check this server issued the nonce and hasn't seen it before | ||
| const { nonce } = parseSiweMessage(message); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing, optional: |
||
| if (!nonce || !nonces.delete(nonce)) { | ||
| return res.status(400).json({ error: "Invalid or reused nonce" }); | ||
| } | ||
|
|
||
| // 2. Verify signature | ||
| const valid = await client.verifyMessage({ address, message, signature }); | ||
| // 2. Verify the signature and bind it to your domain | ||
| const valid = await verifySiweMessage(client, { | ||
| address, | ||
| message, | ||
| signature, | ||
| domain: APP_DOMAIN, | ||
| }); | ||
| if (!valid) return res.status(401).json({ error: "Invalid signature" }); | ||
|
|
||
| // 3. Create session / JWT here | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: worth stating this must equal the message's
domainfield exactly, i.e. host including port (localhost:3000in dev), otherwise readers get a puzzling 401 locally.