Skip to content

Commit

Permalink
normalize domains before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbl committed Oct 2, 2024
1 parent 65888c2 commit beb8c77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/solana-actions/src/signMessageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ export function verifySignMessageData(
errors.push(SignMessageVerificationErrorType.ADDRESS_MISMATCH);
}

// verify if parsed domain is in the expected domains
if (expectedDomains && !expectedDomains.includes(data.domain)) {
errors.push(SignMessageVerificationErrorType.DOMAIN_MISMATCH);
if (expectedDomains) {
const expectedDomainsNormalized = expectedDomains.map(normalizeDomain);
const normalizedDomain = normalizeDomain(data.domain);

if (!expectedDomainsNormalized.includes(normalizedDomain)) {
errors.push(SignMessageVerificationErrorType.DOMAIN_MISMATCH);
}
}

// verify if parsed chainId is same as the expected chainId
if (
expectedChainIds &&
data.chainId &&
Expand All @@ -128,7 +131,6 @@ export function verifySignMessageData(
errors.push(SignMessageVerificationErrorType.CHAIN_ID_MISMATCH);
}

// verify if parsed issuedAt is within +- issuedAtThreshold of the current timestamp
if (issuedAtThreshold !== undefined) {
const iat = Date.parse(data.issuedAt);
if (Math.abs(iat - now) > issuedAtThreshold) {
Expand All @@ -149,3 +151,7 @@ export function verifySignMessageData(
return [SignMessageVerificationErrorType.INVALID_DATA];
}
}

function normalizeDomain(domain: string): string {
return domain.replace(/^www\./, "");
}
9 changes: 9 additions & 0 deletions packages/solana-actions/test/signMessageData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ Issued At: ${validSignMessageData.issuedAt}
expect(errors).toStrictEqual([]);
});

it("should pass verification with www domain data", () => {
const opts = {
expectedDomains: ["www.example.com"],
};

const errors = verifySignMessageData(validSignMessageData, opts);
expect(errors).toStrictEqual([]);
});

it("should return ADDRESS_MISMATCH error if the address does not match", () => {
const opts = {
...verificationOptions,
Expand Down

0 comments on commit beb8c77

Please sign in to comment.