3 Commits

Author SHA1 Message Date
fa6d2ec342 fix(security): harden web3/SIWE auth per security review (CRITICAL+HIGH)
CRITICAL-1: validate SIWE domain/chainId server-side (EIP-4361 cross-site replay guard); AuthService gains siweDomain/siweChainIds, wired from AUTH_SIWE_DOMAIN/AUTH_SIWE_CHAIN_IDS in apps/api context.
CRITICAL-2: UNIQUE index on auth_accounts.wallet_address (migration 0007) as the final gate against the link/login race; loginWithWallet + linkWallet catch the violation -> conflict. Verified on live Postgres (dup rejected, NULLs allowed).
CRITICAL-3: /api/session (dashboard + portal) now verify the token with the API before persisting the cookie (no session fixation); + same-origin check on cookie-setting + /api/wallet/link routes (MEDIUM-3 CSRF).
HIGH-1: require expirationTime; enforce notBefore + issuedAt skew (shared verifyWalletMessage); clients now set expirationTime.
HIGH-2 (rate-limiting, task a): /v1/auth no longer exempt — stricter AUTH_RATE_LIMIT_PER_MINUTE (default 20) in a separate bucket; opportunistic expired-nonce prune (store + Pg).
HIGH-3: reserve the wallet email domain — block email/magic-link registration on it (no synthetic-email squatting).
HIGH-4: gate X-Forwarded-For trust behind TRUST_PROXY.
LOW-1: require a 65-byte (130-hex) ECDSA signature at the API boundary.
+ web3 tests for domain mismatch, missing-expiration, reserved-email. 1308 tests / 213 suites, typecheck + full build green; migrations 0006/0007 verified on Postgres.
2026-06-21 01:30:27 +03:00
740ffe91aa feat(auth): web3 sign-in for portal + wallet-linking for existing accounts
- portal: 'Connect Wallet' SIWE sign-in on login/signup (customer accounts), lib/auth wallet helpers, viem dep. (checkout has no sign-in surface — skipped.)
- auth core: AuthService.linkWallet(sessionToken, {message, signature}) — verify session + SIWE sig + single-use nonce, attach wallet to the existing account, conflict if the wallet is already linked elsewhere. +2 tests (link, conflict).
- api: POST /v1/auth/wallet/link (bearer-authenticated).
- dashboard: 'Link wallet' button in Settings → /api/wallet/link route forwards the session cookie as bearer; Account.walletAddress surfaced.
- 1304 tests / 213 suites, typecheck + full build green.
2026-06-21 00:31:48 +03:00
7941e96f27 feat(auth): web3 wallet login (Sign-In-With-Ethereum)
- Add SIWE login to @settlekit/auth: requestWalletNonce + loginWithWallet on AuthService.
- web3.ts: viem-backed SIWE helpers (generateWalletNonce, buildSiweMessage, parseWalletMessage, offline EOA recoverWalletSigner) — no new external dep beyond viem (already in workspace).
- Single-use, address-bound, expiring nonce (replay-safe); nonce consumed only after the signature verifies.
- Account.walletAddress + WalletNonce types; optional findAccountByWallet/saveWalletNonce/consumeWalletNonce on AuthStore (non-breaking — PgAuthStore opts in later), implemented in InMemoryAuthStore.
- 6 tests: nonce issue/validation, login+account creation, returning account, replay rejection, expiry, wrong-signer rejection.
2026-06-20 23:57:15 +03:00