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
5 changes: 5 additions & 0 deletions .changeset/local-mode-path-boundary.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions docs/src/content/pages/local-mode.mdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
101 changes: 101 additions & 0 deletions packages/keystatic/src/api/api-node.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof localModeApiHandler>;

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<string, string> = {
'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();
});
4 changes: 3 additions & 1 deletion packages/keystatic/src/api/api-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down