From d1074af13909c82107f134a1cb1a64533dd48e62 Mon Sep 17 00:00:00 2001 From: Aleksandr <158179635+tynianov1@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:55:00 +0100 Subject: [PATCH] fix(local): match allowed directories on a path boundary An allowed directory such as posts no longer matches posts-evil/x. Exact matches (used for singleton data files and templates) still work. Also note in the local mode docs that the local API has no authentication and is for local development only. --- .changeset/local-mode-path-boundary.md | 5 + docs/src/content/pages/local-mode.mdoc | 2 + packages/keystatic/src/api/api-node.test.ts | 101 ++++++++++++++++++++ packages/keystatic/src/api/api-node.ts | 4 +- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 .changeset/local-mode-path-boundary.md create mode 100644 packages/keystatic/src/api/api-node.test.ts diff --git a/.changeset/local-mode-path-boundary.md b/.changeset/local-mode-path-boundary.md new file mode 100644 index 000000000..9aa1b8359 --- /dev/null +++ b/.changeset/local-mode-path-boundary.md @@ -0,0 +1,5 @@ +--- +'@keystatic/core': patch +--- + +Only allow local mode reads and writes inside the configured directories, not in sibling paths that share the same prefix. diff --git a/docs/src/content/pages/local-mode.mdoc b/docs/src/content/pages/local-mode.mdoc index ccfcfe092..9f0149819 100644 --- a/docs/src/content/pages/local-mode.mdoc +++ b/docs/src/content/pages/local-mode.mdoc @@ -16,6 +16,8 @@ export default config({ Content is stored on your local file system directly, and this is what makes the most sense when starting a project. +The API routes used in `local` mode have no authentication and are meant for local development only. Do not expose `/api/keystatic` on a server that other people can reach while using `local` mode. + {% aside icon="☝️"%} New projects started with the [Keystatic CLI](/docs/quick-start) will be running in `local` mode. {% /aside %} diff --git a/packages/keystatic/src/api/api-node.test.ts b/packages/keystatic/src/api/api-node.test.ts new file mode 100644 index 000000000..7553b22ea --- /dev/null +++ b/packages/keystatic/src/api/api-node.test.ts @@ -0,0 +1,101 @@ +/** @vitest-environment node */ +import fs from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import { afterAll, beforeAll, expect, test } from 'vitest'; +import { Config } from '../config'; +import { fields } from '../form/api'; +import { localModeApiHandler } from './api-node'; +import { KeystaticRequest } from './internal-utils'; + +const config: Config = { + storage: { kind: 'local' }, + collections: { + posts: { + label: 'Posts', + slugField: 'title', + path: 'posts/*', + schema: { title: fields.slug({ name: { label: 'Title' } }) }, + }, + }, + singletons: { + settings: { + label: 'Settings', + path: 'settings', + schema: { title: fields.text({ label: 'Title' }) }, + }, + }, +}; + +let baseDirectory: string; +let handler: ReturnType; + +beforeAll(async () => { + baseDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'keystatic-')); + handler = localModeApiHandler(config, baseDirectory); +}); + +afterAll(async () => { + await fs.rm(baseDirectory, { recursive: true, force: true }); +}); + +function makeRequest(method: string, body?: unknown): KeystaticRequest { + const headers: Record = { + 'no-cors': '1', + 'content-type': 'application/json', + }; + return { + url: 'http://localhost:3000/api/keystatic', + method, + headers: { get: name => headers[name.toLowerCase()] ?? null }, + json: async () => body, + }; +} + +test('blob allows paths inside an allowed directory', async () => { + const res = await handler(makeRequest('GET'), [ + 'blob', + 'sha', + 'posts', + 'a.yaml', + ]); + expect(res.status).toBe(404); +}); + +test('blob rejects paths in a sibling directory with the same prefix', async () => { + const res = await handler(makeRequest('GET'), [ + 'blob', + 'sha', + 'posts-evil', + 'a.yaml', + ]); + expect(res.status).toBe(400); +}); + +test('update rejects paths in a sibling directory with the same prefix', async () => { + const res = await handler( + makeRequest('POST', { + additions: [{ path: 'posts-evil/a.yaml', contents: 'YQ' }], + deletions: [], + }), + ['update'] + ); + expect(res.status).toBe(400); + await expect( + fs.access(path.join(baseDirectory, 'posts-evil')) + ).rejects.toThrow(); +}); + +test('update rejects a sibling file next to a singleton directory', async () => { + const res = await handler( + makeRequest('POST', { + additions: [{ path: 'settings-evil.yaml', contents: 'YQ' }], + deletions: [], + }), + ['update'] + ); + expect(res.status).toBe(400); + await expect( + fs.access(path.join(baseDirectory, 'settings-evil.yaml')) + ).rejects.toThrow(); +}); diff --git a/packages/keystatic/src/api/api-node.ts b/packages/keystatic/src/api/api-node.ts index a1b9d65cc..7dfc8248f 100644 --- a/packages/keystatic/src/api/api-node.ts +++ b/packages/keystatic/src/api/api-node.ts @@ -131,7 +131,9 @@ function getIsPathValid(config: Config) { return (filepath: string) => !filepath.includes('\\') && filepath.split('/').every(x => x !== '.' && x !== '..') && - allowedDirectories.some(x => filepath.startsWith(x)); + allowedDirectories.some( + x => x === '' || filepath === x || filepath.startsWith(x + '/') + ); } async function blob(