Skip to content

Escape the attribute values that are passed into the Head component #2403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react/src/Head.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { escape } from 'es-toolkit'
import React, { FunctionComponent, useContext, useEffect, useMemo } from 'react'
import HeadContext from './HeadContext'

Expand Down Expand Up @@ -52,7 +53,7 @@ const Head: InertiaHead = function ({ children, title }) {
if (value === '') {
return carry + ` ${name}`
} else {
return carry + ` ${name}="${value}"`
return carry + ` ${name}="${escape(value)}"`
}
}, '')
return `<${node.type}${attrs}>`
Expand Down
14 changes: 14 additions & 0 deletions packages/react/test-app/Pages/Head.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Head } from '@inertiajs/react'

export default () => {
return (
<>
<Head title="Test Head Component">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content='This is an "escape" example' />
</Head>

<h1 style={{ fontSize: '40px' }}>Head Component</h1>
</>
)
}
3 changes: 2 additions & 1 deletion packages/vue3/src/head.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { escape } from 'es-toolkit'
import { defineComponent, DefineComponent } from 'vue'

export type InertiaHead = DefineComponent<{
Expand Down Expand Up @@ -51,7 +52,7 @@ const Head: InertiaHead = defineComponent({
} else if (value === '') {
return carry + ` ${name}`
} else {
return carry + ` ${name}="${value}"`
return carry + ` ${name}="${escape(value)}"`
}
}, '')
return `<${node.type}${attrs}>`
Expand Down
12 changes: 12 additions & 0 deletions packages/vue3/test-app/Pages/Head.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup>
import { Head } from '@inertiajs/vue3'
</script>

<template>
<Head title="Test Head Component">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content='This is an "escape" example' />
</Head>

<h1 :style="{ fontSize: '40px' }">Head Component</h1>
</template>
13 changes: 13 additions & 0 deletions tests/head.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from '@playwright/test'

test('renders the title tag and children', async ({ page }) => {
test.skip(process.env.PACKAGE === 'svelte', 'Svelte adapter has no Head component')

await page.goto('/head')

const inertiaTitle = await page.evaluate(() => document.querySelector('title[inertia]')?.textContent)

await expect(inertiaTitle).toBe('Test Head Component')
await expect(page.locator('meta[name="viewport"]')).toHaveAttribute('content', 'width=device-width, initial-scale=1')
await expect(page.locator('meta[name="description"]')).toHaveAttribute('content', 'This is an "escape" example')
})