85187dfc78
The Memory tab could only show this machine's user memory and the
dashboard's own project file. Setting up a project you had not run
through the dashboard meant opening a terminal — `PUT /file`'s
"project" scope always resolves against the server's cwd, on purpose.
Add one narrow route per tool that writes into the directory you name:
CLAUDE.md for Claude Code, AGENTS.md for Codex, both through the same
`project-memory.js`. Being the only write path that honours a
caller-supplied directory, it pays for that:
- the directory must be absolute and already exist — nothing is
created for you;
- the filename comes from the route, never from the body;
- an existing file, symlink included, is a 409 — this can only
create, never overwrite;
- it sits behind the loopback-Origin guard /api/run uses, so a page
in the user's browser cannot plant agent instructions in a project
directory. That guard moves to lib/same-origin.js to be shared.
The modal takes the dashboard's known project dirs as suggestions on a
free-text input rather than a closed list: a project it has never seen
is exactly the case this exists for.
Mutation-checked: dropping the origin guard, and swapping lstat for
stat, each fail a test.
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
/**
|
|
* @file same-origin.js
|
|
* @description Loopback-Origin guard for the routes that act on the machine
|
|
* rather than on the dashboard's own database — spawning a process, or
|
|
* writing a file into a directory the request names.
|
|
*
|
|
* The dashboard binds to localhost, so any page in the user's browser can
|
|
* reach it. Without this, a malicious site could POST to it and, say, drop a
|
|
* CLAUDE.md full of instructions into a project the user later opens.
|
|
*
|
|
* Browser requests carry `Origin`; it must be a loopback host. Requests with
|
|
* no `Origin` (curl, the CLI) pass — they are not the drive-by case. `Referer`
|
|
* is the fallback for older browsers.
|
|
*
|
|
* Extracted from routes/run.js so the config routes enforce the identical
|
|
* rule instead of a second, drifting copy.
|
|
*
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const ALLOWED_ORIGIN_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "0.0.0.0"]);
|
|
|
|
function isLoopback(raw) {
|
|
try {
|
|
return ALLOWED_ORIGIN_HOSTS.has(new URL(raw).hostname);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function sameOriginGuard(req, res, next) {
|
|
const origin = req.headers.origin;
|
|
if (origin) {
|
|
if (!isLoopback(origin)) {
|
|
return res.status(403).json({
|
|
error: { code: "EBADORIGIN", message: "cross-origin requests are not allowed" },
|
|
});
|
|
}
|
|
return next();
|
|
}
|
|
const referer = req.headers.referer;
|
|
if (referer && !isLoopback(referer)) {
|
|
return res.status(403).json({
|
|
error: { code: "EBADORIGIN", message: "cross-origin requests are not allowed" },
|
|
});
|
|
}
|
|
return next();
|
|
}
|
|
|
|
module.exports = { sameOriginGuard, ALLOWED_ORIGIN_HOSTS };
|