Skip to content

Commit 651962d

Browse files
committed
fix: prefer runtime config for secret
1 parent e82bc65 commit 651962d

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# Changelog
22

33

4+
## v6.2.6...main
5+
6+
[compare changes](https://github.com/nuxt-modules/og-image/compare/v6.2.6...main)
7+
8+
### 🚀 Enhancements
9+
10+
- **security:** Add URL signing to prevent parameter tampering ([#546](https://github.com/nuxt-modules/og-image/pull/546))
11+
12+
### 🩹 Fixes
13+
14+
- **security:** Strict mode, deprecate `html` ([#545](https://github.com/nuxt-modules/og-image/pull/545))
15+
16+
### ❤️ Contributors
17+
18+
- Harlan Wilton ([@harlan-zw](https://github.com/harlan-zw))
19+
420
## v6.2.4...main
521

622
[compare changes](https://github.com/nuxt-modules/og-image/compare/v6.2.4...main)

docs/content/3.guides/13.security.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ The primary security concern with runtime OG image generation is **denial of ser
99

1010
For full protection, we recommend combining URL signing with a **web application firewall** (WAF) or rate limiting on the `/_og/` path prefix. Services like [Cloudflare](https://cloudflare.com), AWS WAF, or your hosting provider's built-in rate limiting can add an additional layer of defense.
1111

12+
```bash [.env]
13+
NUXT_OG_IMAGE_SECRET=<your-secret>
14+
```
15+
1216
```ts [nuxt.config.ts]
1317
export default defineNuxtConfig({
1418
ogImage: {
1519
security: {
1620
strict: true,
17-
secret: process.env.OG_IMAGE_SECRET,
1821
}
1922
}
2023
})
2124
```
2225

26+
The secret is automatically picked up from the `NUXT_OG_IMAGE_SECRET` environment variable.
27+
2328
## Strict Mode
2429

2530
Enabling `strict` mode applies all recommended security defaults in a single flag:
@@ -51,13 +56,19 @@ This prevents unauthorized image generation requests that would otherwise consum
5156
npx nuxt-og-image generate-secret
5257
```
5358

54-
2. Set the environment variable and reference it in your config:
59+
2. Set the environment variable:
60+
61+
```bash [.env]
62+
NUXT_OG_IMAGE_SECRET=<your-secret>
63+
```
64+
65+
Alternatively, you can set the secret directly in your nuxt config:
5566

5667
```ts [nuxt.config.ts]
5768
export default defineNuxtConfig({
5869
ogImage: {
5970
security: {
60-
secret: process.env.OG_IMAGE_SECRET,
71+
secret: 'your-secret',
6172
}
6273
}
6374
})

docs/content/4.api/3.config.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,15 @@ Security limits for image generation. See the [Security Guide](/docs/og-image/gu
188188
- **`restrictRuntimeImagesToOrigin`**: Restrict runtime image generation to requests whose `Host` header matches allowed hosts. Default `false`. See the [Security Guide](/docs/og-image/guides/security#restrict-runtime-images-to-origin) for details.
189189
- **`strict`**: Enable strict security mode. Requires `secret`, disables the deprecated `html` option, defaults `maxQueryParamSize` to `2048`, and enables `restrictRuntimeImagesToOrigin`. Default `false`. See the [Security Guide](/docs/og-image/guides/security#strict-mode) for details.
190190

191+
```bash [.env]
192+
NUXT_OG_IMAGE_SECRET=<your-secret>
193+
```
194+
191195
```ts [nuxt.config.ts]
192196
export default defineNuxtConfig({
193197
ogImage: {
194198
security: {
195199
strict: true,
196-
secret: process.env.OG_IMAGE_SECRET,
197200
}
198201
}
199202
})

src/cli.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,20 +1664,12 @@ async function runEnable(renderer: string, args: string[]): Promise<void> {
16641664
function generateSecret() {
16651665
const secret = randomBytes(32).toString('hex')
16661666
p.intro('nuxt-og-image generate-secret')
1667-
p.note([
1668-
`${secret}`,
1669-
'',
1670-
'Add this to your nuxt.config.ts:',
1671-
'',
1672-
' ogImage: {',
1673-
' security: {',
1674-
` secret: process.env.OG_IMAGE_SECRET,`,
1675-
' }',
1676-
' }',
1677-
'',
1678-
'Then set the environment variable:',
1679-
` OG_IMAGE_SECRET=${secret}`,
1680-
].join('\n'), 'Generated Secret')
1667+
p.log.step(`Secret: ${secret}`)
1668+
p.log.message('')
1669+
p.log.message('Set the environment variable:')
1670+
p.log.message(` NUXT_OG_IMAGE_SECRET=${secret}`)
1671+
p.log.message('')
1672+
p.log.message('The secret is automatically picked up via runtime config.')
16811673
p.outro('')
16821674
}
16831675

src/module.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,18 @@ export default defineNuxtModule<ModuleOptions>({
349349
logger.warn('`ogImage.debug` is enabled in production. This exposes the `/_og/debug.json` endpoint and should not be enabled in production. Disable it before deploying.')
350350
}
351351

352-
if (config.security?.strict && !config.security?.secret) {
353-
throw new Error('[nuxt-og-image] `security.strict` requires `security.secret` to be set. Generate one with: npx nuxt-og-image generate-secret')
352+
const hasSecret = !!(config.security?.secret || process.env.NUXT_OG_IMAGE_SECRET)
353+
354+
if (config.security?.strict && !hasSecret) {
355+
throw new Error('[nuxt-og-image] `security.strict` requires a signing secret. Generate one with: npx nuxt-og-image generate-secret')
354356
}
355357

356-
if (nuxt.options.dev && !config.zeroRuntime && !config.security?.secret) {
358+
if (nuxt.options.dev && !config.zeroRuntime && !hasSecret) {
357359
logger.warn([
358360
'OG image URLs are not signed. Anyone can craft arbitrary image generation requests.',
359361
'',
360-
'Either set a signing secret:',
361-
' ogImage: { security: { secret: process.env.OG_IMAGE_SECRET } }',
362+
'Set a signing secret via env variable:',
363+
' NUXT_OG_IMAGE_SECRET=<secret>',
362364
'',
363365
' Generate one with: npx nuxt-og-image generate-secret',
364366
'',
@@ -1468,7 +1470,7 @@ export const rootDir = ${JSON.stringify(nuxt.options.rootDir)}`
14681470
restrictRuntimeImagesToOrigin: config.security?.restrictRuntimeImagesToOrigin === true || (config.security?.strict && config.security?.restrictRuntimeImagesToOrigin == null)
14691471
? []
14701472
: (config.security?.restrictRuntimeImagesToOrigin || false),
1471-
secret: config.security?.secret || '',
1473+
secret: config.security?.secret || process.env.NUXT_OG_IMAGE_SECRET || '',
14721474
},
14731475
}
14741476
if (nuxt.options.dev) {

0 commit comments

Comments
 (0)