Files
cobble/src/functions/backend/db/bot-files/copy-bot-file.ts
T
tbenandClaude Opus 5.5 cdb93ac1bc Fix file access, XSS and access-control bugs from a security scan
- Media rows can no longer be created through the CRUD API, updates only
  take description fields on the caller's own row, and every media read
  or delete goes through mediaFilePath, which refuses paths outside
  DATA_DIR or inside the database folder.
- Uploads strip the file name and extension to safe characters and keep
  only title, description and summary from the request's media object.
- Folder imports no longer follow symlinks (walkRegularFiles), and the
  source path is quoted in the computer's shell.
- User and bot files are served with nosniff, and HTML, SVG, XML and
  scripts are sandboxed by CSP.
- Sandbox cookies are kept to the computer's prefix, and websocket
  upgrades on /ws and the computer proxy must come from the app's own
  address (README and Help note nginx's Host header).
- user_types CRUD writes go through the assignment matrix; members only
  read their own user record; read-only users can read CRUD again;
  grabUsers builds valid SQL for array search terms and bad pages; an
  admin media DELETE needs an id; read-only users can't delete media.
- Rate limits and the audit log use the connection's address, trusting
  forwarded headers only from a proxy, and login limits are split per
  address, per account from that address and per account overall.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 03:38:07 +01:00

66 lines
1.9 KiB
TypeScript

import mediaFilePath from "@/src/utils/media-file-path";
import AppError from "@/src/utils/app-error";
import type { StorageScope } from "@/src/utils/storage-access";
import { canReachScope } from "@/src/utils/storage-access";
import grabBotFile from "./grab-bot-file";
import recordBotFile from "./record-bot-file";
type Params = {
scopes: StorageScope[];
file_id: number | string;
/**
* The bot making the copy (it owns the new row).
*/
bot_id: number | string;
/**
* Where the copy goes: one of the run's directories.
*/
scope: StorageScope;
/**
* Its path there. Defaults to the source's path.
*/
path?: string | null;
user_id?: number | string;
};
/**
* Copies a file into another of the run's directories (the coordinator
* publishing a worker's artifact into its own, or into a workspace). The
* bytes are copied, so deleting one leaves the other.
*/
export default async function copyBotFile({
scopes,
file_id,
bot_id,
scope,
path,
user_id,
}: Params) {
if (!canReachScope({ scopes, scope: scope.scope, owner_id: scope.owner_id })) {
throw new AppError(`You can't write to that folder.`);
}
const file = await grabBotFile({ scopes, file_id });
const absolute = mediaFilePath({ write_path: file?.media?.media_write_path });
if (!file?.id || !absolute) {
throw new AppError(`That file isn't in your files.`);
}
const source = await Bun.file(absolute).arrayBuffer();
return await recordBotFile({
bot_id,
scope: scope.scope,
owner_id: scope.owner_id,
path: path || file.path || file.name || `copy-${file.id}`,
data: new Uint8Array(source),
purpose: (file.purpose as any) || "writing",
source: "tool",
media_type: (file.media_type as any) || undefined,
mime_type: file.mime_type || undefined,
user_id,
});
}