File tree Expand file tree Collapse file tree 2 files changed +46
-2
lines changed
src/runtime/server/og-image Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -63,8 +63,28 @@ export async function resolveContext(e: H3Event): Promise<H3Error | OgImageRende
6363 statusMessage : `[Nuxt OG Image] Unknown OG Image type ${ extension } .` ,
6464 } )
6565 }
66- let queryParams = { ...getQuery ( e ) }
67- queryParams . props = JSON . parse ( queryParams . props || '{}' )
66+
67+ const query = getQuery ( e )
68+ let queryParams : Record < string , any > = { }
69+ for ( const k in query ) {
70+ const v = String ( query [ k ] )
71+ if ( ! v )
72+ continue
73+ if ( v . startsWith ( '{' ) ) {
74+ // we need to parse the JSON string
75+ try {
76+ queryParams [ k ] = JSON . parse ( v )
77+ }
78+ catch ( error ) {
79+ if ( import . meta. dev ) {
80+ logger . error ( `[Nuxt OG Image] Invalid JSON in ${ k } parameter: ${ error . message } ` )
81+ }
82+ }
83+ }
84+ else {
85+ queryParams [ k ] = v
86+ }
87+ }
6888 queryParams = separateProps ( queryParams )
6989 let basePath = withoutTrailingSlash ( path
7090 . replace ( `/__og-image__/image` , '' )
Original file line number Diff line number Diff line change 1+ import { createResolver } from '@nuxt/kit'
2+ import { $fetch , setup } from '@nuxt/test-utils'
3+ import { describe , expect , it } from 'vitest'
4+
5+ const { resolve } = createResolver ( import . meta. url )
6+
7+ await setup ( {
8+ rootDir : resolve ( '../fixtures/basic' ) ,
9+ dev : true ,
10+ } )
11+
12+ describe ( 'partial JSON query parameter handling' , async ( ) => {
13+ it ( 'handles malformed JSON in query parameters gracefully' , async ( ) => {
14+ // This is the problematic URL from the issue
15+ const response = await $fetch ( '/__og-image__/image/satori/og.png?_query=%7B%22utm_source%22' ) . catch ( ( ) => false )
16+ expect ( response ) . not . toBe ( false )
17+ } )
18+
19+ it ( 'still processes valid JSON queries correctly' , async ( ) => {
20+ // Test with valid JSON query
21+ const response = await $fetch ( '/__og-image__/image/satori/og.png?_query=%7B%22utm_source%22%3A%22facebook%22%7D' ) . catch ( ( ) => false )
22+ expect ( response ) . not . toBe ( false )
23+ } )
24+ } )
You can’t perform that action at this time.
0 commit comments