Skip to content

Commit

Permalink
Merge pull request #555 from Baroshem/fix-crypto-node-18
Browse files Browse the repository at this point in the history
core(fix): Make crypto compatible with Node 18
  • Loading branch information
vejja authored Nov 14, 2024
2 parents 84f13ee + 577b2dd commit 642bfce
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: [20]
node: [18]

steps:
- uses: actions/setup-node@v3
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "nuxt-security",
"version": "2.1.0",
"version": "2.1.1",
"license": "MIT",
"type": "module",
"engines": {
"node": ">=20.0.0"
"node": ">=18.0.0"
},
"homepage": "https://nuxt-security.vercel.app",
"description": "🛡️ Security Module for Nuxt based on HTTP Headers and Middleware",
Expand Down Expand Up @@ -79,7 +79,7 @@
},
"unbuild": {
"entries": [
"./src/utils/hash.ts",
"./src/utils/crypto.ts",
"./src/utils/headers.ts",
"./src/utils/merge.ts",
"./src/defaultConfig.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join, isAbsolute } from 'pathe'
import { defu } from 'defu'
import viteRemove from 'unplugin-remove/vite'
import { getHeadersApplicableToAllResources } from './utils/headers'
import { generateHash } from './utils/hash'
import { generateHash } from './utils/crypto'
import { defuReplaceArray } from './utils/merge'
import { defaultSecurityConfig } from './defaultConfig'
import type { Nuxt } from '@nuxt/schema'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/plugins/30-cspSsgHashes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineNitroPlugin } from '#imports'
import { resolveSecurityRules } from '../context'
import { generateHash } from '../../../utils/hash'
import { generateHash } from '../../../utils/crypto'
import type { Section } from '../../../types/module'


Expand Down
5 changes: 2 additions & 3 deletions src/runtime/nitro/plugins/40-cspSsrNonce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineNitroPlugin } from '#imports'
import { resolveSecurityRules } from '../context'
import { generateRandomNonce } from '../../../utils/crypto'

const LINK_RE = /<link([^>]*?>)/gi
const SCRIPT_RE = /<script([^>]*?>)/gi
Expand Down Expand Up @@ -27,9 +28,7 @@ export default defineNitroPlugin((nitroApp) => {

const rules = resolveSecurityRules(event)
if (rules.enabled && rules.nonce && !import.meta.prerender) {
const array = new Uint8Array(18);
crypto.getRandomValues(array)
const nonce = btoa(String.fromCharCode(...array))
const nonce = generateRandomNonce()
event.context.security!.nonce = nonce
}
})
Expand Down
12 changes: 12 additions & 0 deletions src/utils/hash.ts → src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// These two lines are required only to maintain compatibility with Node 18
// - In Node 19 and above, crypto is available in the global scope
// - In Workers environments, crypto is available in the global scope
import { webcrypto } from 'node:crypto'
globalThis.crypto ??= webcrypto as Crypto

export async function generateHash(content: Buffer | string, hashAlgorithm: 'SHA-256' | 'SHA-384' | 'SHA-512') {
let buffer: Uint8Array
Expand All @@ -11,3 +16,10 @@ export async function generateHash(content: Buffer | string, hashAlgorithm: 'SHA
const prefix = hashAlgorithm.replace('-', '').toLowerCase()
return `${prefix}-${base64}`;
}

export function generateRandomNonce() {
const array = new Uint8Array(18);
crypto.getRandomValues(array)
const nonce = btoa(String.fromCharCode(...array))
return nonce
}
8 changes: 4 additions & 4 deletions src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createDefu } from 'defu';
import { createDefu } from 'defu'

export const defuReplaceArray = createDefu((obj, key, value) => {
if (Array.isArray(obj[key]) || Array.isArray(value)) {
obj[key] = value;
return true;
obj[key] = value
return true
}
});
})

0 comments on commit 642bfce

Please sign in to comment.