fix(nuxt): use destr in more places over JSON.parse (#22997)

This commit is contained in:
Daniel Roe 2023-09-05 09:42:16 +01:00 committed by GitHub
parent b27740cf50
commit 1a08079710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import { parseURL } from 'ufo'
import { defineComponent, h } from 'vue' import { defineComponent, h } from 'vue'
import { parseQuery } from 'vue-router' import { parseQuery } from 'vue-router'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import destr from 'destr'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { devRootDir } from '#build/nuxt.config.mjs' import { devRootDir } from '#build/nuxt.config.mjs'
@ -10,7 +11,7 @@ export default (url: string) => defineComponent({
async setup (props, { attrs }) { async setup (props, { attrs }) {
const query = parseQuery(parseURL(url).search) const query = parseQuery(parseURL(url).search)
const urlProps = query.props ? JSON.parse(query.props as string) : {} const urlProps = query.props ? destr<Record<string, any>>(query.props as string) : {}
const path = resolve(query.path as string) const path = resolve(query.path as string)
if (!path.startsWith(devRootDir)) { if (!path.startsWith(devRootDir)) {
throw new Error(`[nuxt] Cannot access path outside of project root directory: \`${path}\`.`) throw new Error(`[nuxt] Cannot access path outside of project root directory: \`${path}\`.`)

View File

@ -1,3 +1,4 @@
import destr from 'destr'
import { useNuxtApp } from '#app/nuxt' import { useNuxtApp } from '#app/nuxt'
export interface ReloadNuxtAppOptions { export interface ReloadNuxtAppOptions {
@ -34,7 +35,7 @@ export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {
let handledPath: Record<string, any> = {} let handledPath: Record<string, any> = {}
try { try {
handledPath = JSON.parse(sessionStorage.getItem('nuxt:reload') || '{}') handledPath = destr(sessionStorage.getItem('nuxt:reload') || '{}')
} catch {} } catch {}
if (options.force || handledPath?.path !== path || handledPath?.expires < Date.now()) { if (options.force || handledPath?.path !== path || handledPath?.expires < Date.now()) {

View File

@ -1,3 +1,4 @@
import destr from 'destr'
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt' import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'
export default defineNuxtPlugin({ export default defineNuxtPlugin({
@ -9,7 +10,7 @@ export default defineNuxtPlugin({
const state = sessionStorage.getItem('nuxt:reload:state') const state = sessionStorage.getItem('nuxt:reload:state')
if (state) { if (state) {
sessionStorage.removeItem('nuxt:reload:state') sessionStorage.removeItem('nuxt:reload:state')
Object.assign(nuxtApp.payload.state, JSON.parse(state)?.state) Object.assign(nuxtApp.payload.state, destr<Record<string, any>>(state)?.state)
} }
} catch {} } catch {}
} }

View File

@ -1,4 +1,5 @@
import { reactive, ref, shallowReactive, shallowRef } from 'vue' import { reactive, ref, shallowReactive, shallowRef } from 'vue'
import destr from 'destr'
import { definePayloadReviver, getNuxtClientPayload } from '#app/composables/payload' import { definePayloadReviver, getNuxtClientPayload } from '#app/composables/payload'
import { createError } from '#app/composables/error' import { createError } from '#app/composables/error'
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt' import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'
@ -8,8 +9,8 @@ import { componentIslands } from '#build/nuxt.config.mjs'
const revivers: Record<string, (data: any) => any> = { const revivers: Record<string, (data: any) => any> = {
NuxtError: data => createError(data), NuxtError: data => createError(data),
EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : JSON.parse(data)), EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : JSON.parse(data)), EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
ShallowRef: data => shallowRef(data), ShallowRef: data => shallowRef(data),
ShallowReactive: data => shallowReactive(data), ShallowReactive: data => shallowReactive(data),
Ref: data => ref(data), Ref: data => ref(data),