Skip to content

Commit 1298799

Browse files
committed
chore(nuxt): use sha256 utils helper
1 parent 2ed0e27 commit 1298799

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/runtime/server/lib/validators/nuxthub.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { subtle } from 'node:crypto'
2-
import { Buffer } from 'node:buffer'
31
import { type H3Event, getRequestHeaders, readRawBody } from 'h3'
4-
import { encoder, HMAC_SHA256, ensureConfiguration } from '../helpers'
2+
import { ensureConfiguration, sha256 } from '../helpers'
53

64
const NUXTHUB_SIGNATURE = 'x-nuxthub-signature'
75

@@ -22,8 +20,7 @@ export const isValidNuxtHubWebhook = async (event: H3Event): Promise<boolean> =>
2220
if (!body || !webhookSignature) return false
2321

2422
const payload = body + config.secretKey
25-
const signatureBuffer = await subtle.digest(HMAC_SHA256.hash, encoder.encode(payload))
26-
const signature = Buffer.from(signatureBuffer).toString('hex')
23+
const signature = await sha256(payload)
2724

2825
return signature === webhookSignature
2926
}

test/simulations/nuxthub.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { subtle } from 'node:crypto'
2-
import { Buffer } from 'node:buffer'
31
import { $fetch } from '@nuxt/test-utils/e2e'
4-
import { encoder, HMAC_SHA256 } from '../../src/runtime/server/lib/helpers'
2+
import { sha256 } from '../../src/runtime/server/lib/helpers'
53
import nuxtConfig from '../fixtures/basic/nuxt.config'
64

7-
const body = 'testBody'
5+
const body = { message: 'NuxtHub Sample Webhook, this is a test message from NuxtHub Admin' }
86
const secretKey = nuxtConfig.runtimeConfig?.webhook?.nuxthub?.secretKey
97

108
export const simulateNuxthubEvent = async () => {
11-
const payload = body + secretKey
12-
const signatureBuffer = await subtle.digest(HMAC_SHA256.hash, encoder.encode(payload))
13-
const validSignature = Buffer.from(signatureBuffer).toString('hex')
9+
const payload = JSON.stringify(body) + secretKey
10+
const validSignature = await sha256(payload)
1411

1512
const headers = { 'x-nuxthub-signature': validSignature }
1613

0 commit comments

Comments
 (0)