Documentation
¶
Index ¶
Constants ¶
const ( // DefaultMaxFileSize is the default maximum file size for safety checks (100MB) DefaultMaxFileSize = 100 * 1024 * 1024 // 100MB // SampleDatabaseMaxFileSize is the maximum file size for sample database files (10MB) // Sample databases should be smaller since they're downloaded from remote sources SampleDatabaseMaxFileSize = 10 * 1024 * 1024 // 10MB )
Variables ¶
This section is empty.
Functions ¶
func SafeReadFile ¶
func SafeReadFile(path string, opts *FileSafetyOptions) ([]byte, error)
SafeReadFile reads a file after performing safety checks, validating the same file descriptor it reads from so it never reads from an unexpected target.
The flow mirrors streamio.openOutputFile's open-then-fstat pattern:
- A pre-open Stat rejects unsafe targets (e.g. non-regular files when AllowNonRegular is unset, or directories) before we open them. This matters because opening a FIFO O_RDONLY blocks until a writer appears, so we must not open one we are only going to reject.
- After os.Open, f.Stat() re-validates against the descriptor actually opened. If the path was swapped between the two stats (e.g. a regular file replaced by a device or FIFO), this catches it before we read.
Scope of the guarantee: the fd re-validation ensures we never READ from a swapped or unexpected file type. It does NOT eliminate an open-time block: a hostile swap of a regular file to a FIFO in the window between the pre-open Stat and os.Open can still block the blocking os.Open until a writer appears, on platforms where opening a FIFO read-only blocks. This is an accepted limitation for a local CLI. O_NONBLOCK open is deliberately not used so that process-substitution FIFOs (--file <(...)) keep working with blocking reads.
The read itself is always capped via io.LimitReader for BOTH regular and allowed non-regular inputs. Stat sizes are meaningless for pipes and can be stale for regular files, so the limit is enforced at read time rather than only from the FileInfo size.
func ValidateFileSafety ¶
func ValidateFileSafety(fi os.FileInfo, path string, opts *FileSafetyOptions) error
ValidateFileSafety checks if a file is safe to read based on the given options
Types ¶
type FileSafetyOptions ¶
type FileSafetyOptions struct {
// MaxSize is the maximum allowed file size (0 means use DefaultMaxFileSize)
MaxSize int64
// AllowNonRegular allows reading from non-regular files (not recommended)
AllowNonRegular bool
}
FileSafetyOptions configures file safety checks