Package com.tnsai.tools.file
Record Class FileGuardConfig
java.lang.Object
java.lang.Record
com.tnsai.tools.file.FileGuardConfig
- Record Components:
maxReadBytes- upper bound on a single file read; rejected before I/OmaxWriteBytes- upper bound on a single file write; rejected before I/OallowedExtensions- lowercased extensions (no leading dot); empty defers to toolkit defaultsandboxRoot- absolute, normalized directory; every resolved path must start with it
public record FileGuardConfig(long maxReadBytes, long maxWriteBytes, Set<String> allowedExtensions, Path sandboxRoot)
extends Record
Declarative guardrail for every file-touching
@Tool in tnsai-tools.
Holds the four invariants every file-aware toolkit needs to enforce:
- Sandbox — paths must resolve under
sandboxRoot; absolute paths to system files and../escapes are rejected. - Read cap — files larger than
maxReadBytesare refused before any byte is read into the JVM heap. - Write cap — write payloads above
maxWriteBytesare refused before disk I/O begins. - Extension whitelist — when non-empty, only files whose lowercased
extension is in
allowedExtensionsare admitted; an empty set defers to the per-toolkit default supplied at the call site.
Resolution order is defaults() → env vars → compiled fallback.
Programmatic per-tenant policy is expressed by passing a custom
FileGuardConfig to a toolkit's (FileGuardConfig) constructor.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long10 MB read cap when neither env var nor explicit config overrides.static final long1 MB write cap when neither env var nor explicit config overrides.static final StringEnv var: comma-separated extensions, no leading dot.static final StringEnv var: long, bytes.static final StringEnv var: long, bytes.static final StringEnv var: absolute directory path. -
Constructor Summary
ConstructorsConstructorDescriptionFileGuardConfig(long maxReadBytes, long maxWriteBytes, Set<String> allowedExtensions, Path sandboxRoot) Creates an instance of aFileGuardConfigrecord class. -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of theallowedExtensionsrecord component.static FileGuardConfigdefaults()Builds a config from environment variables, falling back to compiled defaults for any var that is unset or unparseable.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.longReturns the value of themaxReadBytesrecord component.longReturns the value of themaxWriteBytesrecord component.Returns the value of thesandboxRootrecord component.final StringtoString()Returns a string representation of this record class.withAllowedExtensions(Set<String> extensions) Returns a new config with the given allowed extensions, all other fields unchanged.
-
Field Details
-
DEFAULT_MAX_READ_BYTES
public static final long DEFAULT_MAX_READ_BYTES10 MB read cap when neither env var nor explicit config overrides.- See Also:
-
DEFAULT_MAX_WRITE_BYTES
public static final long DEFAULT_MAX_WRITE_BYTES1 MB write cap when neither env var nor explicit config overrides.- See Also:
-
ENV_MAX_READ_BYTES
Env var: long, bytes. OverridesDEFAULT_MAX_READ_BYTES.- See Also:
-
ENV_MAX_WRITE_BYTES
Env var: long, bytes. OverridesDEFAULT_MAX_WRITE_BYTES.- See Also:
-
ENV_ALLOWED_EXTS
Env var: comma-separated extensions, no leading dot.- See Also:
-
ENV_SANDBOX_ROOT
Env var: absolute directory path. Defaults to the JVM CWD.- See Also:
-
-
Constructor Details
-
FileGuardConfig
public FileGuardConfig(long maxReadBytes, long maxWriteBytes, Set<String> allowedExtensions, Path sandboxRoot) Creates an instance of aFileGuardConfigrecord class.- Parameters:
maxReadBytes- the value for themaxReadBytesrecord componentmaxWriteBytes- the value for themaxWriteBytesrecord componentallowedExtensions- the value for theallowedExtensionsrecord componentsandboxRoot- the value for thesandboxRootrecord component
-
-
Method Details
-
defaults
Builds a config from environment variables, falling back to compiled defaults for any var that is unset or unparseable. Called once at toolkit instantiation via the no-arg constructor path.The returned config has empty
allowedExtensionsunlessENV_ALLOWED_EXTSis set — empty defers to the per-toolkit default at call sites, which is the right behavior for a generic config.- Returns:
- resolved configuration
-
withAllowedExtensions
Returns a new config with the given allowed extensions, all other fields unchanged. Useful for toolkits that want to layer their own default on top ofdefaults().- Parameters:
extensions- new extension whitelist (may be empty)- Returns:
- derived config
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
maxReadBytes
public long maxReadBytes()Returns the value of themaxReadBytesrecord component.- Returns:
- the value of the
maxReadBytesrecord component
-
maxWriteBytes
public long maxWriteBytes()Returns the value of themaxWriteBytesrecord component.- Returns:
- the value of the
maxWriteBytesrecord component
-
allowedExtensions
Returns the value of theallowedExtensionsrecord component.- Returns:
- the value of the
allowedExtensionsrecord component
-
sandboxRoot
Returns the value of thesandboxRootrecord component.- Returns:
- the value of the
sandboxRootrecord component
-