Skip to content
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
23 changes: 19 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ const MIME = {
// ── Static file server with SPA fallback ──────────────────────────────────────

async function serveStatic(req, res) {
const urlPath = req.url.split('?')[0]
const filePath = path.join(DIST_DIR, urlPath)
let urlPath = req.url.split('?')[0]
try {
urlPath = decodeURIComponent(urlPath)
} catch {
// ignore invalid encoding
}
const resolvedDist = path.resolve(DIST_DIR)
const filePath = path.resolve(DIST_DIR, '.' + urlPath)

// Security: prevent path traversal outside dist/
if (!filePath.startsWith(DIST_DIR)) {
if (!filePath.startsWith(resolvedDist + path.sep) && filePath !== resolvedDist) {
res.statusCode = 403
res.end('Forbidden')
return
Expand Down Expand Up @@ -103,9 +109,18 @@ function handleStore(req, res) {
const existing = await fs.promises.readFile(DATA_FILE, 'utf8')
.then(raw => JSON.parse(raw))
.catch(() => ({}))

// Security: Prevent prototype pollution
for (const key in updates) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue
}
existing[key] = updates[key]
}

await fs.promises.writeFile(
DATA_FILE,
JSON.stringify({ ...existing, ...updates }, null, 2),
JSON.stringify(existing, null, 2),
)
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
Expand Down
11 changes: 10 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ function sonosStorePlugin() {
const existing = await fs.promises.readFile(DATA_FILE, 'utf8')
.then(raw => JSON.parse(raw))
.catch(() => ({}))
await fs.promises.writeFile(DATA_FILE, JSON.stringify({ ...existing, ...updates }, null, 2))

// Security: Prevent prototype pollution
for (const key in updates) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue
}
existing[key] = updates[key]
}

await fs.promises.writeFile(DATA_FILE, JSON.stringify(existing, null, 2))
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ ok: true }))
Expand Down