localfile

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MPL-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package localfile provides explicit policies for opening regular local files. The zero value denies access. Callers must deliberately choose either a symlink-safe filesystem root or unrestricted host-filesystem access.

On Unix, files are opened nonblocking and verified from the opened descriptor before Open returns, preventing ordinary FIFO open waits. Device-specific open behavior can still vary. Other platforms have no portable nonblocking open flag, so a hostile special file may block before it can be rejected. In addition, os.Root cannot provide a symlink security boundary on GOOS=js and does not retain directory identity across renames on GOOS=js or GOOS=plan9. Callers on those platforms must not treat Rooted as a complete boundary for hostile filesystems.

Index

Constants

This section is empty.

Variables

View Source
var ErrDenied = errors.New("localfile: access denied")

ErrDenied reports that a policy does not permit a local-file path.

Functions

This section is empty.

Types

type Policy

type Policy struct {
	// contains filtered or unexported fields
}

Policy controls access to local files. Its zero value denies all local-file access and is safe for untrusted input.

Policy implements the part of fs.FS needed by D2's compiler imports. Open intentionally rejects directories and other non-regular files, so Policy is not a general-purpose directory-browsing filesystem.

func Rooted

func Rooted(root string) (Policy, error)

Rooted returns a policy that permits files at or beneath root. Opens use one retained os.Root, so symbolic links cannot escape root and replacing the configured path cannot retarget the policy on supported platforms.

func Unrestricted

func Unrestricted() Policy

Unrestricted returns a policy that permits arbitrary host-filesystem reads. Use it only for trusted local input, such as the D2 command-line interface.

func (Policy) CacheKey

func (p Policy) CacheKey(name string) (string, error)

CacheKey returns a policy-scoped, canonical key for name. It rejects names outside the policy's lexical scope; Open additionally enforces the rooted symbolic-link boundary. The key prevents a cache filled under an unrestricted policy from bypassing a rooted or denied policy.

func (Policy) Close

func (p Policy) Close() error

Close releases the retained OS root handle. Copies of a rooted Policy share one copy-safe close operation and return the same close result. It does not erase or revoke immutable resources that callers already retained in their own caches. Closing a denied or unrestricted policy is a no-op.

func (Policy) FilesWithExtension

func (p Policy) FilesWithExtension(ext string) fs.FS

FilesWithExtension returns a view of a rooted policy for reading source files. Both the requested name and the symlink target must have ext (for example ".d2"). The view shares the policy's root and import path semantics.

func (Policy) Open

func (p Policy) Open(name string) (fs.File, error)

Open opens a regular file according to the policy. For rooted policies, relative names are interpreted relative to the configured root. Absolute names are accepted only when they are lexically beneath that root, and the retained os.Root enforces the same boundary while following symbolic links.

func (Policy) ResolveImportPath

func (Policy) ResolveImportPath(parent, name string) string

ResolveImportPath joins D2 imports using host filesystem path semantics.

func (Policy) RootPath

func (p Policy) RootPath() (string, bool)

RootPath returns the absolute configured root and true for a rooted policy. It returns an empty string and false for denied and unrestricted policies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL