/** * @fileoverview Path-policy enforcement for the Obsidian Local REST API service. * Single chokepoint for OBSIDIAN_READ_PATHS / OBSIDIAN_WRITE_PATHS / * OBSIDIAN_DENY_PATHS / OBSIDIAN_READ_ONLY — tools and resources call into the * service, the service consults this policy before every upstream HTTP call. * See issues #40 and #76 for the spec. * * A denial outside the allowlists carries the active scope so error data * echoes back which paths are allowed; the LLM (or operator) can self-correct * without poking at logs. A denial by the deny list names no prefix: echoing * the list would name exactly what it hides. * * @module services/obsidian/path-policy */ import type { ServerConfig } from '../../config/server-config.js'; export type PathOp = 'read' | 'write'; export type PathForbiddenSubreason = 'outside_read_paths' | 'outside_write_paths' | 'read_only_mode' | 'denied_path'; /** Wire data shape thrown with `path_forbidden`. A `denied_path` denial carries no scope. */ export type PathForbiddenData = { op: PathOp; path: string; reason: 'path_forbidden'; recovery: { hint: string; }; } & ({ activeScope: string[]; subreason: Exclude; } | { subreason: 'denied_path'; }); /** An allow prefix a deny prefix covers, which therefore grants nothing. */ export interface DenyShadowedPath { prefix: string; variable: 'OBSIDIAN_READ_PATHS' | 'OBSIDIAN_WRITE_PATHS'; } /** * Enforces folder-scoped read/write permissions on vault-relative paths. * Constructed once from validated `ServerConfig`; the path lists arrive * already in `normalize()` form and deduplicated by the config parser, so * matching is a straight prefix-or-equal compare. The deny list wins over * every allow rule, write-implies-read included. */ export declare class PathPolicy { #private; constructor(config: ServerConfig); /** True when no path policy is active — every op falls through to the upstream. */ get isUnrestricted(): boolean; /** * True when reads are gated: OBSIDIAN_READ_PATHS or OBSIDIAN_DENY_PATHS is * set. Write paths and read-only gate writes only, so they leave this false * and vault-wide reads stay vault-wide. */ get restrictsReads(): boolean; /** True when OBSIDIAN_DENY_PATHS withholds paths inside the scopes `effectiveScope` names. */ get withholdsPaths(): boolean; /** Snapshot for startup-banner logging: the configured lists, `denyPaths` only when set. */ describe(): { readPaths: readonly string[] | 'full vault'; writePaths: readonly string[] | 'full vault' | 'denied (read-only)'; denyPaths?: readonly string[]; readOnly: boolean; }; /** True when both READ_ONLY=true and WRITE_PATHS is non-empty (operator should know WRITE_PATHS is ignored). */ get readOnlyShadowsWritePaths(): boolean; /** * Configured allow prefixes a deny prefix covers: they grant nothing, so the * startup log warns about each. Write paths are skipped under read-only, * which ignores them already (`readOnlyShadowsWritePaths`). */ get denyShadowedPaths(): DenyShadowedPath[]; isReadable(path: string): boolean; isWritable(path: string): boolean; /** * Throws `path_forbidden` if the path is not readable. `resolvedFrom` is the * label of the `active` / `periodic` target `path` was resolved from; the * denial names it in place of a path the caller never sent. A path outside * the read allowlist is `outside_read_paths` whether or not it is also * denied, so `denied_path` never confirms deny-list membership of a path the * allowlist already refuses. */ assertReadable(path: string, resolvedFrom?: string): void; /** * Throws `path_forbidden` if the path is not writable (write tools also * implicitly need read access). `resolvedFrom` as for `assertReadable`. * Subreason precedence: `read_only_mode`, then `outside_write_paths`, then * `denied_path`. */ assertWritable(path: string, resolvedFrom?: string): void; /** Drop hits outside the read scope, silently. Used by `obsidian_search_notes`, `listTags`, and the scoped JSONLogic context fetch. */ filterReadable(hits: readonly T[]): T[]; /** * The scope `path_forbidden` enforces for each op, as prefixes: `undefined` * when the op reaches the full vault, `[]` when it reaches nothing. Readable * is the read paths plus the write paths (read paths alone under read-only). * Writable is the write paths, or the readable scope when only reads are * restricted, since a write also needs read access to its target. An allow * prefix a deny prefix covers grants nothing and is left out; whatever else * the deny list withholds inside these scopes, `withholdsPaths` flags * without naming. */ effectiveScope(): { readable: readonly string[] | undefined; writable: readonly string[] | undefined; }; /** * Source of a regular expression that matches every path `isReadable` * accepts, and possibly more: the `regexp` guard that narrows a scoped * JSONLogic fetch upstream. It narrows the transfer only — callers still * gate the results with `filterReadable`. See `prefixPattern` for how it * stays a superset. With no read allowlist it matches every path; deny * prefixes are never subtracted, since negating a superset would drop * readable notes. When no allow prefix survives the deny list it matches * no path — an empty alternation would match every one. */ readScopePattern(): string; /** * True when `path` is a folder on the way to the read scope: some readable * prefix sits strictly below it, matched at segment boundaries and * case-insensitively. The vault root is an ancestor whenever reads are * restricted. Ancestors are listable so a nested scope can be browsed to; * their names leak nothing the scope itself (in `instructions` and every * `path_forbidden`) doesn't already state. An ancestor is never denied: a * deny prefix covering it would cover the allow prefix below it too, and an * allow prefix a deny prefix covers is not part of the scope. */ isScopeAncestor(path: string): boolean; /** * Throws `path_forbidden` unless `dir` is readable or a folder on the way to * the read scope, with the subreason `assertReadable` would give. */ assertListable(dir: string): void; /** * Keep the entries of a listing of `dir` a caller may see: readable entries, * plus folders on the way to the read scope. Entries are names relative to * `dir`, folders ending in `/`, as the Local REST API lists them. */ filterListing(dir: string, entries: readonly string[]): string[]; } /** * A regex source matching every string whose `toLowerCase()` contains `text`, * and possibly more, for lower-cased `text` (`foldedChar`). `undefined` when * `text` holds a non-ASCII character, whose case folds include * multi-character forms. The scoped text search's candidate guard builds one * per query token. */ export declare function caseFoldedPattern(text: string): string | undefined; //# sourceMappingURL=path-policy.d.ts.map