Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/long-pillows-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-static-copy': patch
---

Files not included in `src` was possible to acess with a crafted request. See [GHSA-pp7p-q8fx-2968](https://github.com/sapphi-red/vite-plugin-static-copy/security/advisories/GHSA-pp7p-q8fx-2968) for more details.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ typings/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

Expand Down
17 changes: 15 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
OutgoingHttpHeaders,
ServerResponse,
} from 'node:http'
import { join, resolve } from 'node:path'
import { join, resolve, sep } from 'node:path'
import type { FileMap } from './serve'
import type { TransformOptionObject } from './options'
import {
Expand Down Expand Up @@ -54,6 +54,13 @@ function shouldServeOverwriteCheck(
return true
}

function isFileInside(filepath: string, srcBase: string) {
const srcBaseWithTrailingSlash = srcBase.endsWith(sep)
? srcBase
: `${srcBase}${sep}`
return filepath.startsWith(srcBaseWithTrailingSlash)
}

function viaLocal(
root: string,
publicDir: string,
Expand Down Expand Up @@ -87,7 +94,13 @@ function viaLocal(
if (!uri.startsWith(dir)) continue

for (const val of vals) {
const filepath = resolve(root, val.src, uri.slice(dir.length))
const srcBase = resolve(root, val.src)
const filepath = resolve(srcBase, uri.slice(dir.length))
if (!isFileInside(filepath, srcBase)) {
// uri includes non-normalized `../`
return undefined
}

const overwriteCheck = shouldServeOverwriteCheck(
val.overwrite,
filepath,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SHOULD_BE_HIDDEN=PRIVATE
24 changes: 21 additions & 3 deletions test/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import { describe, test, beforeAll, afterAll, expect } from 'vitest'
import type { PreviewServer, ViteDevServer } from 'vite'
import { build, createServer, preview } from 'vite'
import { testcases } from './testcases'
import { getConfig, loadFileContent, normalizeLineBreak } from './utils'
import {
getConfig,
loadFileContent,
normalizeLineBreak,
sendRawRequest,
} from './utils'
import type { AddressInfo } from 'node:net'

const constructUrl = (server: ViteDevServer | PreviewServer, path: string) => {
const port = (server.httpServer!.address() as AddressInfo).port
return `http://localhost:${port}${path}`
}

const fetchFromServer = async (
server: ViteDevServer | PreviewServer,
path: string,
) => {
const port = (server.httpServer!.address() as AddressInfo).port
const url = `http://localhost:${port}${path}`
const url = constructUrl(server, path)
const res = await fetch(url)
return res
}
Expand Down Expand Up @@ -94,6 +103,15 @@ describe('serve', () => {
)
expect(res.headers.get('Cross-Origin-Opener-Policy')).toBe('same-origin')
})

test.concurrent('disallow path traversal with ../', async () => {
const res = await sendRawRequest(
constructUrl(server, '/'),
'/fixture1/foo.txt/../.env',
)
expect(res).not.toContain('SHOULD_BE_HIDDEN')
expect(res).toContain('HTTP/1.1 404 Not Found')
})
})
})

Expand Down
36 changes: 36 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import type { InlineConfig } from 'vite'
import { normalizePath } from 'vite'
import net from 'node:net'

export const root = new URL('./fixtures/', import.meta.url)

Expand Down Expand Up @@ -31,3 +32,38 @@ export const loadFileContent = async (

export const normalizeLineBreak = (input: string) =>
input.replace(/\r\n/g, '\n')

export const sendRawRequest = async (
baseUrl: string,
requestTarget: string,
) => {
return new Promise<string>((resolve, reject) => {
const parsedUrl = new URL(baseUrl)

const buf: Buffer[] = []
const client = net.createConnection(
{ port: +parsedUrl.port, host: parsedUrl.hostname },
() => {
client.write(
[
`GET ${encodeURI(requestTarget)} HTTP/1.1`,
`Host: ${parsedUrl.host}`,
'Connection: Close',
'\r\n',
].join('\r\n'),
)
},
)
client.on('data', (data) => {
buf.push(data)
})
client.on('end', (hadError: unknown) => {
if (!hadError) {
resolve(Buffer.concat(buf).toString())
}
})
client.on('error', (err) => {
reject(err)
})
})
}
Loading