Skip to content

Commit 736fe21

Browse files
committed
fix: siwe message validation
1 parent 2a395bf commit 736fe21

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

web/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ parcel-bundle-reports
2929
src/hooks/contracts/generated.ts
3030
src/graphql
3131
generatedGitInfo.json
32+
generatedNetlifyInfo.json
3233

3334
# logs
3435
npm-debug.log*

web/netlify/functions/authUser.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import middy from "@middy/core";
22
import jsonBodyParser from "@middy/http-json-body-parser";
3-
import { ETH_SIGNATURE_REGEX } from "src/consts";
3+
import { ETH_SIGNATURE_REGEX } from "consts/index";
4+
import { DEFAULT_CHAIN } from "consts/chains";
45
import { SiweMessage } from "siwe";
56
import * as jwt from "jose";
67
import { createClient } from "@supabase/supabase-js";
7-
import { Database } from "../../src/types/supabase-notification";
8+
import { netlifyUri } from "src/generatedNetlifyInfo.json";
9+
import { Database } from "src/types/supabase-notification";
810

911
const authUser = async (event) => {
1012
try {
@@ -32,15 +34,30 @@ const authUser = async (event) => {
3234
}
3335

3436
const siweMessage = new SiweMessage(message);
35-
const lowerCaseAddress = siweMessage.address.toLowerCase();
3637

37-
if (siweMessage.address.toLowerCase() !== address.toLowerCase()) {
38+
if (netlifyUri && netlifyUri !== siweMessage.uri) {
39+
console.debug(`Invalid URI: expected ${netlifyUri} but got ${siweMessage.uri}`);
40+
throw new Error(`Invalid URI`);
41+
}
42+
43+
if (siweMessage.chainId !== DEFAULT_CHAIN) {
44+
console.debug(`Invalid chain ID: expected ${DEFAULT_CHAIN} but got ${siweMessage.chainId}`);
45+
throw new Error(`Invalid chain ID`);
46+
}
47+
48+
if (!siweMessage.expirationTime || Date.parse(siweMessage.expirationTime) < Date.now()) {
49+
console.debug(`Message expired: ${siweMessage.expirationTime} < ${new Date().toISOString()}`);
50+
throw new Error("Message expired");
51+
}
52+
53+
const lowerCaseAddress = siweMessage.address.toLowerCase();
54+
if (lowerCaseAddress !== address.toLowerCase()) {
3855
throw new Error("Address mismatch in provided address and message");
3956
}
4057

4158
const supabase = createClient<Database>(process.env.SUPABASE_URL!, process.env.SUPABASE_CLIENT_API_KEY!);
4259

43-
// get nonce from db, if its null that means it was alrd used
60+
// get nonce from db, if its null that means it was already used
4461
const { error: nonceError, data: nonceData } = await supabase
4562
.from("user-nonce")
4663
.select("nonce")

web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"build-devnet-neo": "scripts/runEnv.sh devnet-neo 'yarn generate && parcel build'",
4141
"build-testnet": "scripts/runEnv.sh testnet 'yarn generate && parcel build'",
4242
"build-mainnet-neo": "scripts/runEnv.sh mainnet-neo 'yarn generate && parcel build'",
43-
"build-netlify": "node scripts/gitInfo.js && yarn generate && parcel build",
43+
"build-netlify": "scripts/generateBuildInfo.sh && yarn generate && parcel build",
4444
"check-style": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
4545
"check-types": "tsc --noEmit",
4646
"generate": "yarn generate:gql && yarn generate:hooks",

web/scripts/generateBuildInfo.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
5+
jq -n --arg uri "$DEPLOY_PRIME_URL" '{ netlifyUri: $uri }' > src/generatedNetlifyInfo.json
6+
node $SCRIPT_DIR/gitInfo.js

web/scripts/runEnv.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ if [[ ! " ${valid_deployments[@]} " =~ " ${deployment} " ]]; then
1717
exit 1
1818
fi
1919

20-
node $SCRIPT_DIR/gitInfo.js
21-
2220
envFile="$SCRIPT_DIR/../.env.${deployment}"
2321
[ -f "$envFile.public" ] && . $envFile.public
2422
[ -f "$envFile" ] && . $envFile
2523

24+
$SCRIPT_DIR/generateBuildInfo.sh
25+
2626
eval "$commands"

0 commit comments

Comments
 (0)