THE FILESYSTEM SECURITY BOUNDARY. A peer-supplied path plus an intended
access in; an OPEN HANDLE on a confined object, or a refusal, out.
TWO DIMENSIONS, ONE DECISION. Where a file may live (path confinement) and
what kind of file may be touched (extension gating) are decided together and
yield one verdict. They are not separable, because on Windows they share
failure modes: 'logo.png::$DATA' and 'secrets.ini.' both defeat an extension
allow-list through the PATH rules, not the extension rules.
THE HANDLE IS THE RESULT, NOT THE PATH. This is the single most important
thing about this unit's shape, and it was got wrong first time round. A jail
whose output is a path STRING cannot be free of check-then-open races on any
platform, however carefully the backend is written: the jail opens the object
to inspect it, closes it, and hands back a name, and the caller then performs
a second, independent open of that name at some later moment. Everything
proved by the inspection has expired by then. So the verified handle IS the
return value - see OpenVerified and OpenStream - and the physical path is
demoted to a logging field on the handle. Resolve and ResolveUrl remain, but
they are lexical only and deliberately emit the canonical VIRTUAL path: they
cannot hand back anything openable, because the convenient method must not
also be the unsafe one.
THE ROOT IS PINNED. NewFileJail opens the root through the same OS-truth
mechanism the backend uses for every later object, holds that handle for the
jail's lifetime, and stores the canonical path the OS reported - never the
answer from a lexical canonicaliser, which resolves '.' and '..' but not
symbolic links on any platform. Without this, a document root that is itself
a symlink (macOS /tmp and /var are; an admin-convenience symlinked docroot
very often is) makes an honest backend refuse every request, and the usual
repair - comparing lexically instead - makes verification decorative.
VALIDATE WHAT YOU OPEN, NOT WHAT YOU WERE GIVEN. The lexical pass emits a
RELATIVE path, and the backend walks it from the pinned root handle one
segment at a time. Nothing concatenates the root with a relative path and
re-canonicalises it, because Win32 canonicalisation MUTATES names - it strips
trailing dots and spaces - so a jail built that way validates one name and
opens another, which is precisely the attack this unit exists to stop.
IMMUTABLE ONCE MINTED. NewFileJail snapshots the policy. Nothing on a live
jail is settable and the policy object is not reachable through it. Two
policies over one tree - a public read jail and an admin write jail over the
same document root - are two jails. This is also what makes a jail safe to
share across the reactor's I/O threads without locking.
FAILS CLOSED EVERYWHERE. Every guard exits with the verdict already set to a
refusal; an empty allow-list permits nothing; every call into a backend is
guarded, and an unexpected exception is jvInternalError. A REFUSAL IS NEVER
AN EXCEPTION - a Boolean gate that raises is a gate a swallowed exception
opens - and that applies to NewFileJail itself, which always returns a usable
interface even if construction failed.
WHAT THIS DOES NOT CLOSE. Stated plainly, because an absolute claim here
would be worth less than an accurate one:
- HARD LINKS. A hard link inside the root to a file outside it is, on
POSIX, indistinguishable from the file itself: there is no primary name
to report. On Windows the final path of a hard link is the in-root name,
so containment passes. jrRefuseHardLinks (link count > 1) is the only
signal available and is blunt; it is off by default.
- BIND MOUNTS and junction-free mount points, which are containment
failures the filesystem itself endorses. jrSameDevice narrows this.
- A LYING SERVER. Over SMB or NFS the answers come from the far end.
- CASE-INSENSITIVE VOLUMES reached from a case-sensitive build, and the
reverse. See tCaseMode; the default follows the platform, which is the
right guess and not a guarantee.
- ANYTHING AFTER THE HANDLE IS HANDED OVER. The jail guarantees what was
opened. What the caller then does with it is the caller's business.
WHAT THIS UNIT DOES NOT DECIDE. It does not map a verdict to an HTTP status.
Distinguishing "not on the allow-list" from "not found" to the PEER is an
information-disclosure oracle; distinguishing them in the LOG is what tells
an operator whether they have a misconfiguration or an attacker. The jail
reports the true cause; collapsing it for the wire is the caller's job.Feature notes: File jail (filesystem confinement) notes
An open, verified object. The jail's actual output.
Native is the platform descriptor - a THandle on Windows, a file descriptor on POSIX - and is cJailHandleNone when nothing is open. Physical is OS TRUTH, reported by the operating system for the object that was actually opened, and is for LOGGING ONLY: re-opening it re-introduces exactly the race the handle exists to avoid.
Native: NativeInt;
Parent: NativeInt;
Leaf: string;
IsDirectory: Boolean;
Physical: string;
function IsOpen: Boolean;
inherits from TPersistent
Configuration for one jail. A TPersistent sub-object: configuration, not identity, so it may be copied.
NOTE ON THE OWNER TYPE. The house template (tCustomHTTPLimits) takes aOwner: TComponent. This one widens to TPersistent, a strict superset - every TComponent is a TPersistent, and GetOwner returns TPersistent regardless - because a policy must be ownable by a TCollectionItem as well as by a component: an HTTP static-file mount is a collection item, and N mounts over one tree each need their own policy without cloning a component.
constructor Create(aOwner: TPersistent); virtual;
destructor Destroy; override;
procedure AssignTo(Dest: TPersistent); override;
procedure AllowFromMimeTypes(const aMap: iMimeTypeMap);
property Access: tJailAccessSet read fAccess write fAccess default [jaRead];
property Options: tJailOptions read fOptions write fOptions;
property ExtensionMode: tExtensionMode read fExtensionMode write fExtensionMode default emAllowListed;
property CaseMode: tCaseMode read fCaseMode write fCaseMode default cmPlatform;
property ShareMode: tJailShareMode read fShareMode write fShareMode default jsmShareAll;
property Allowed: TStringList read fAllowed write SetAllowed;
property Refused: TStringList read fRefused write SetRefused;
property MaxPathLength: Integer read fMaxPathLength write SetMaxPathLength default cMaxJailPath;
The verification backend. Declared here so the core carries no platform API; stFileJail.Windows and stFileJail.Posix each provide a descendant and NewJailVerifier branches, following the stSocket / stSocket.Posix precedent.
EVERY METHOD MUST ASSIGN EVERY out PARAMETER ON EVERY PATH, INCLUDING REFUSALS. Delphi does not zero an out parameter of an unmanaged type on entry, so a backend that Exits early without assigning leaves the caller reading a stale value - and the stale value here decides whether something is treated as a directory.
function OpenRoot(const aRoot: string; out aHandle: tJailHandle; out aVerdict: tJailVerdict): Boolean; virtual; abstract;
function OpenAt(const aRoot: tJailHandle; const aRelative: string; aAccess: tJailAccessSet; aOptions: tJailOptions; aCaseMode: tCaseMode; aShareMode: tJailShareMode; out aHandle: tJailHandle; out aVerdict: tJailVerdict): Boolean; virtual; abstract;
function NewStream(var aHandle: tJailHandle; aAccess: tJailAccessSet; out aStream: TStream; out aVerdict: tJailVerdict): Boolean; virtual; abstract;
procedure Close(var aHandle: tJailHandle); virtual; abstract;
A minted jail. Immutable; safe to share across threads.
function Status: tJailStatus;
function Root: string;
function Resolve(const aVirtualPath: string; aAccess: tJailAccess; out aCanonical: string; out aVerdict: tJailVerdict): Boolean;
function ResolveUrl(const aUrlPath: string; aAccess: tJailAccess; out aCanonical: string; out aVerdict: tJailVerdict): Boolean;
function OpenVerified(const aVirtualPath: string; aAccess: tJailAccess; out aHandle: tJailHandle; out aVerdict: tJailVerdict): Boolean;
function OpenUrlVerified(const aUrlPath: string; aAccess: tJailAccess; out aHandle: tJailHandle; out aVerdict: tJailVerdict): Boolean;
function OpenStream(const aVirtualPath: string; aAccess: tJailAccess; out aStream: TStream; out aVerdict: tJailVerdict): Boolean;
function OpenUrlStream(const aUrlPath: string; aAccess: tJailAccess; out aStream: TStream; out aVerdict: tJailVerdict): Boolean;
procedure Close(var aHandle: tJailHandle);
function ToVirtual(const aPhysical: string; out aVirtual: string): Boolean;
function PermitsSegment(const aSegment: string; out aVerdict: tJailVerdict): Boolean;
Why a path was refused. One member per distinguishable cause, because the log line and the operator's next action differ for each. MEMBERS ARE ONLY EVER APPENDED - the ordinal is compiled into every consumer of a given package generation.
Whether an unlisted extension is permitted. The veto list applies in BOTH modes and always wins.
How containment comparisons fold case. Both halves of every comparison use ONE fold - an earlier draft folded the equality test with ASCII SameText and the prefix test with locale-aware AnsiStartsText, so the two halves of a single containment decision disagreed about which names are the same.
What a jail carries. A jail refuses any access outside this set before it parses the path at all.
What OTHER processes may do to an object while this jail holds it open. WINDOWS ONLY, and that is not a caveat to bury. Windows share modes are MANDATORY - the kernel enforces them - whereas POSIX has no equivalent: flock and fcntl locks are ADVISORY, so a process that does not ask is not stopped. The POSIX backend therefore ignores this setting rather than pretending to honour it with a lock that guarantees nothing. Code that relies on a deny mode is relying on Windows behaviour. The jail's own VERIFICATION opens are unaffected and always share fully: the pinned root is held for the jail's lifetime, and a brief interrogating open must never be able to make somebody else's operation fail.
Lexical and structural rules.
Why NewFileJail did not produce a usable jail. A jail is always returned - it simply refuses everything - so a caller that ignores the status gets a server that serves nothing, never a server that serves anything.