Skip to content

Support for path traversal #2413

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions packages/core/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export function mergeDataIntoQueryString(
const hasHost = /^[a-z][a-z0-9+.-]*:\/\//i.test(href.toString())
const hasAbsolutePath = hasHost || href.toString().startsWith('/')
const hasRelativePath = !hasAbsolutePath && !href.toString().startsWith('#') && !href.toString().startsWith('?')
const hasRelativePathWithDotPrefix = /^[.]{1,2}([/]|$)/.test(href.toString())
const hasSearch = href.toString().includes('?') || (method === 'get' && Object.keys(data).length)
const hasHash = href.toString().includes('#')

const url = new URL(href.toString(), 'http://localhost')
const url = new URL(href.toString(), typeof window === 'undefined' ? 'http://localhost' : window.location.toString())

if (method === 'get' && Object.keys(data).length) {
const parseOptions = { ignoreQueryPrefix: true, parseArrays: false }
Expand All @@ -59,7 +60,7 @@ export function mergeDataIntoQueryString(
[
hasHost ? `${url.protocol}//${url.host}` : '',
hasAbsolutePath ? url.pathname : '',
hasRelativePath ? url.pathname.substring(1) : '',
hasRelativePath ? url.pathname.substring(hasRelativePathWithDotPrefix ? 0 : 1) : '',
hasSearch ? url.search : '',
hasHash ? url.hash : '',
].join(''),
Expand Down
17 changes: 17 additions & 0 deletions packages/react/test-app/Pages/Links/PathTraversal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Link } from '@inertiajs/react'

export default () => {
return (
<div>
<Link href="../">
Up one level
</Link>
<Link href="../../method">
Up two levels and open method
</Link>
<Link href="../../../">
Up three levels
</Link>
</div>
)
}
17 changes: 17 additions & 0 deletions packages/svelte/test-app/Pages/Links/PathTraversal.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import { inertia } from '@inertiajs/svelte'
</script>

<div class="space-y-2">
<a use:inertia href="../">
Up one level
</a>

<a use:inertia href="../../method">
Up two levels and open method
</a>

<a use:inertia href="../../../">
Up three levels
</a>
</div>
11 changes: 11 additions & 0 deletions packages/vue3/test-app/Pages/Links/PathTraversal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup>
import { Link } from '@inertiajs/vue3'
</script>

<template>
<div>
<Link href="../"> Up one level </Link>
<Link href="../../method"> Up two levels and open method </Link>
<Link href="../../../"> Up three levels </Link>
</div>
</template>
2 changes: 2 additions & 0 deletions tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ app.get('/links/headers/version', (req, res) =>
)
app.get('/links/data-loading', (req, res) => inertia.render(req, res, { component: 'Links/DataLoading' }))
app.get('/links/prop-update', (req, res) => inertia.render(req, res, { component: 'Links/PropUpdate' }))
app.get('/links/sub', (req, res) => inertia.render(req, res, { component: 'Links/PathTraversal' }))
app.get('/links/sub/sub', (req, res) => inertia.render(req, res, { component: 'Links/PathTraversal' }))

app.get('/client-side-visit', (req, res) =>
inertia.render(req, res, {
Expand Down
23 changes: 23 additions & 0 deletions tests/links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,26 @@ test('will update href if prop is updated', async ({ page }) => {
await button.click()
await expect(link).toHaveAttribute('href', /\/something-else$/)
})

test.describe('path traversal', () => {
test('it goes one level up', async ({ page }) => {
await page.goto('/links/sub/sub/')

await page.getByRole('link', { name: 'Up one level' }).click()
await expect(page).toHaveURL('/links/sub/')
})

test('it goes two levels up with a new path', async ({ page }) => {
await page.goto('/links/sub/sub/')

await page.getByRole('link', { name: 'Up two levels and open method' }).click()
await expect(page).toHaveURL('/links/method')
})

test('it goes three levels up', async ({ page }) => {
await page.goto('/links/sub/sub/')

await page.getByRole('link', { name: 'Up three levels' }).click()
await expect(page).toHaveURL('/')
})
})