-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add request signature validation (#3)
- Loading branch information
Showing
16 changed files
with
167 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { SecretManagerServiceClient } from "@google-cloud/secret-manager"; | ||
import config from "./config.js"; | ||
|
||
/** | ||
* Load a secret from Secret Manager | ||
*/ | ||
export default async function getSecret(secretId: string): Promise<string> { | ||
try { | ||
const secretManager = new SecretManagerServiceClient(); | ||
const secretFullResourceName = `projects/${config.GCP_PROJECT_ID}/secrets/${secretId}/versions/latest`; | ||
const [version] = await secretManager.accessSecretVersion({ | ||
name: secretFullResourceName, | ||
}); | ||
|
||
const secret = version.payload?.data?.toString(); | ||
|
||
if (!secret) { | ||
throw new Error( | ||
`Secret '${secretId}' is empty or undefined. Please check the secret in Secret Manager.`, | ||
); | ||
} | ||
|
||
return secret; | ||
} catch (error) { | ||
console.error( | ||
`Failed to retrieve secret '${secretId}' from secret manager:`, | ||
error, | ||
); | ||
throw error; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Request } from "@google-cloud/functions-framework"; | ||
import crypto from "crypto"; | ||
import config from "./config"; | ||
import getSecret from "./get-secret"; | ||
|
||
export async function isFromQuicknode(req: Request): Promise<boolean> { | ||
const quicknodeSecurityToken = await getSecret( | ||
config.QUICKNODE_SECURITY_TOKEN_SECRET_ID, | ||
); | ||
const givenSignature = req.headers["x-qn-signature"]; | ||
const nonce = req.headers["x-qn-nonce"]; | ||
const contentHash = req.headers["x-qn-content-hash"]; | ||
const timestamp = req.headers["x-qn-timestamp"]; | ||
|
||
if (!nonce || typeof nonce !== "string") { | ||
return false; | ||
} | ||
|
||
if (!contentHash || typeof contentHash !== "string") { | ||
return false; | ||
} | ||
|
||
if (!timestamp || typeof timestamp !== "string") { | ||
return false; | ||
} | ||
|
||
const hmac = crypto.createHmac("sha256", quicknodeSecurityToken); | ||
hmac.update(`${nonce}${contentHash}${timestamp}`); | ||
|
||
const expectedSignature = hmac.digest("base64"); | ||
|
||
return givenSignature === expectedSignature; | ||
} | ||
|
||
export async function hasAuthToken(req: Request): Promise<boolean> { | ||
const authToken = req.headers["x-auth-token"]; | ||
const expectedAuthToken = await getSecret(config.X_AUTH_TOKEN_SECRET_ID); | ||
|
||
return authToken === expectedAuthToken; | ||
} |
This file contains 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