mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-15 18:59:33 +01:00
fix(preview): hold a folder file's resolved target to the attachment gate; accept long names
Folder routes only looked at the requested name, so `shot.png -> ../secret.bin` and `notes.txt -> ../history` were served (registration answers 400 for both), and a FIFO or device behind a supported name parked a worker on an open that never returned. After resolving, the target's extension must be supported and it must be a regular file (locally fs.stat, remotely the probe kind). A 101..255-character file name answered 404 "Route not found": Fastify caps a path parameter at 100 characters. The server now sets routerOptions.maxParamLength (MAX_ROUTE_PARAM_LENGTH, shared with the route test harness) and a wiring guard pins it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3857fba065
commit
6bffc0481c
5 files changed
+182
-4
No files matched your search
@@ -17,6 +17,15 @@ import { parseRalphLoopConfig, extractCompletionPhrase } from '../ralph-config.j
|
||||
import { SseEvent } from './sse-events.js';
|
||||
import type { SessionPort } from './ports/session-port.js';
|
||||
import type { EventPort } from './ports/event-port.js';
|
||||
|
||||
/**
|
||||
* Longest path PARAMETER the router accepts (Fastify's default is 100). A file
|
||||
* name goes up to 255 characters and arrives percent-encoded, so the folder
|
||||
* grid's `…/files/:name/raw` answered 404 "Route not found" for a name the
|
||||
* listing happily showed. Raw length, not decoded: 255 four-byte characters are
|
||||
* 3060 characters of `%XX`.
|
||||
*/
|
||||
export const MAX_ROUTE_PARAM_LENGTH = 4096;
|
||||
import type { AuthSessionRecord } from './ports/auth-port.js';
|
||||
import type { StaleExpirationMap } from '../utils/index.js';
|
||||
import { dataPath } from '../config/instance.js';
|
||||
|
||||
@@ -2469,6 +2469,24 @@ export function registerFileRoutes(app: FastifyInstance, ctx: SessionPort & Even
|
||||
};
|
||||
const servable = await resolveServableAttachmentPath(reply, record, scope);
|
||||
if (!servable) return null;
|
||||
// The requested NAME only opens the door; registration judges the file a
|
||||
// symlink resolves to (supported extension, regular file) and so does this.
|
||||
// Without it `shot.png -> ../secret.bin` is served, and a FIFO or a device
|
||||
// behind a supported name parks a worker on an open that never returns.
|
||||
if (!isSupportedAttachmentExtension(extname(servable.path))) {
|
||||
reply.code(400).send(createErrorResponse(ApiErrorCode.INVALID_INPUT, 'Unsupported attachment type'));
|
||||
return null;
|
||||
}
|
||||
const isRegularFile = scope.remote
|
||||
? servable.probe?.kind === 'file'
|
||||
: await fs.stat(servable.path).then(
|
||||
(stat) => stat.isFile(),
|
||||
() => false
|
||||
);
|
||||
if (!isRegularFile) {
|
||||
reply.code(400).send(createErrorResponse(ApiErrorCode.INVALID_INPUT, 'Attachment path is not a file'));
|
||||
return null;
|
||||
}
|
||||
if (!folderAllowedForUser(servable.path, getAuthUser(req), scope)) {
|
||||
reply.code(403).send(createErrorResponse(ApiErrorCode.INVALID_INPUT, 'File is outside your space'));
|
||||
return null;
|
||||
|
||||
+8
-2
@@ -159,6 +159,7 @@ import { MAX_CONCURRENT_SESSIONS, MAX_SSE_CLIENTS } from '../config/map-limits.j
|
||||
import { MAX_PASTE_IMAGE_BYTES } from '../config/buffer-limits.js';
|
||||
import { resolveTerminalHistoryConfig } from '../config/terminal-history.js';
|
||||
import { SseEvent } from './sse-events.js';
|
||||
import { MAX_ROUTE_PARAM_LENGTH } from './route-helpers.js';
|
||||
import { getLatestPlanUsage, setLatestCodexPlanUsage } from './plan-usage-latest.js';
|
||||
import { telemetrySignature } from '../usage-telemetry.js';
|
||||
import { readCodexPlanUsage, resolveCodexBinaryPath } from '../utils/codex-cli-resolver.js';
|
||||
@@ -433,9 +434,14 @@ export class WebServer extends EventEmitter {
|
||||
const rewriteUrl = (req: { url?: string }): string => rewriteApiV1Url(stripBasePath(this.basePath, req.url || ''));
|
||||
if (https) {
|
||||
const { key, cert } = getOrCreateSelfSignedCert();
|
||||
this.app = Fastify({ logger: false, https: { key, cert }, rewriteUrl });
|
||||
this.app = Fastify({
|
||||
logger: false,
|
||||
https: { key, cert },
|
||||
rewriteUrl,
|
||||
routerOptions: { maxParamLength: MAX_ROUTE_PARAM_LENGTH },
|
||||
});
|
||||
} else {
|
||||
this.app = Fastify({ logger: false, rewriteUrl });
|
||||
this.app = Fastify({ logger: false, rewriteUrl, routerOptions: { maxParamLength: MAX_ROUTE_PARAM_LENGTH } });
|
||||
}
|
||||
this.mux = createMultiplexer();
|
||||
this.sse = new SseStreamManager(
|
||||
|
||||
Reference in new issue
Block a user