feat(config): create a new project's memory file from Settings

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.
This commit is contained in:
2026-09-09 01:45:00 +07:00
parent 697384145a
commit 85187dfc78
16 changed files with 833 additions and 36 deletions
+1 -36
View File
@@ -25,45 +25,10 @@ const path = require("node:path");
const runs = require("../lib/pty-run");
const tmux = require("../lib/tmux");
const lanesLib = require("../lib/lanes");
const { sameOriginGuard } = require("../lib/same-origin");
const router = Router();
const ALLOWED_ORIGIN_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "0.0.0.0"]);
/**
* Loopback-Origin guard. Browser requests carry Origin; if it's not localhost,
* we reject. Server/CLI requests (curl) typically don't carry Origin and pass.
*
* Referer is checked as a fallback for older browsers / fetch with credentials
* disabled — the same loopback-host rule applies.
*/
function sameOriginGuard(req, res, next) {
const checkHost = (raw) => {
try {
const u = new URL(raw);
return ALLOWED_ORIGIN_HOSTS.has(u.hostname);
} catch {
return false;
}
};
const origin = req.headers.origin;
if (origin) {
if (!checkHost(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 && !checkHost(referer)) {
return res.status(403).json({
error: { code: "EBADORIGIN", message: "cross-origin requests are not allowed" },
});
}
return next();
}
router.use(sameOriginGuard);
function isExistingDir(p) {