All files / core/blong-gogo/src/adapter/impl workspacePath.ts

100% Statements 65/65
100% Branches 17/17
100% Functions 8/8
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 12x 13x 13x 11x 11x  
import {isAbsolute, resolve, sep} from 'node:path';
 
/**
 * A ticket's directory, and what may not be named inside it.
 *
 * Every path a tool hands in is resolved against the ticket root and refused if it leaves — a model
 * writing a parent path is the ordinary case, not the attack, which is why this is a refusal with a
 * code rather than a comment asking nicely (`@@D-710`).
 *
 * The ticket id is checked too: it becomes a directory name, and `..` there would move the root rather
 * than stay inside it.
 */
export class WorkspaceEscape extends Error {
    readonly code = 'agent.workspaceEscape';
    constructor(message: string) {
        super(message);
        this.name = 'WorkspaceEscape';
    }
}
 
/** What a command needs switched off, and where it may run. */
export class WorkspaceCommandDisabled extends Error {
    readonly code = 'agent.commandDisabled';
    constructor(message = 'running commands is disabled') {
        super(message);
        this.name = 'WorkspaceCommandDisabled';
    }
}
 
/** A command that outlived its timeout. */
export class WorkspaceCommandTimeout extends Error {
    readonly code = 'agent.commandTimeout';
    constructor(message: string) {
        super(message);
        this.name = 'WorkspaceCommandTimeout';
    }
}
 
const SAFE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
 
/** The directory a ticket owns under the configured root. */
export const workspaceRootPath = (root: string, ticketId: string): string => {
    if (!SAFE_ID.test(ticketId) || ticketId === '.' || ticketId === '..')
        throw new WorkspaceEscape(`ticket id ${ticketId} is not a directory name`);
    return resolve(root, ticketId);
};
 
/**
 * The absolute path of `requested` inside a ticket workspace.
 *
 * An absolute request is refused rather than re-rooted: a caller that names one is describing a place
 * outside the ticket, and silently reading something else would answer a different question.
 */
export const workspaceResolve = (
    root: string,
    ticketId: string,
    requested: string = '.',
): string => {
    const base = workspaceRootPath(root, ticketId);
    if (isAbsolute(requested)) throw new WorkspaceEscape(`${requested} is an absolute path`);
    const full = resolve(base, requested);
    if (full !== base && !full.startsWith(base + sep))
        throw new WorkspaceEscape(`${requested} leaves the workspace of ${ticketId}`);
    return full;
};