org.llm4s.security
Members list
Type members
Classlikes
Network security utilities for SSRF protection.
Network security utilities for SSRF protection.
Provides IP address validation to prevent Server-Side Request Forgery (SSRF) attacks by blocking requests to internal networks, cloud metadata endpoints, and other potentially sensitive destinations.
== Protected IP Ranges == Checked against the IANA IPv4 and IPv6 Special-Purpose Address Registries: every entry that is not globally reachable is blocked.
- Private networks: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 (RFC 1918); IPv6 unique-local fc00::/7 (RFC 4193) and the deprecated site-local fec0::/10
- Loopback: 127.0.0.0/8, ::1
- Unspecified / "this network": 0.0.0.0/8, ::
- Link-local: 169.254.0.0/16, fe80::/10
- Cloud metadata: 169.254.169.254 (AWS, GCP, Azure)
- Multicast: 224.0.0.0/4, ff00::/8
- Reserved 240.0.0.0/4, including the limited broadcast address 255.255.255.255
- IETF protocol assignments 192.0.0.0/24 and the deprecated 6to4 relay anycast 192.88.99.0/24
- Carrier-grade NAT 100.64.0.0/10, benchmarking 198.18.0.0/15 and 2001:2::/48
- Documentation/test ranges: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32, 3fff::/20
- Every IPv6 address outside global unicast 2000::/3 (RFC 4291 2.4), which IANA has never allocated for routing: among others the IPv4-compatible ::/96, the SIIT IPv4-translated ::ffff:0:0:0/96, discard-only 100::/64, the dummy prefix 100:0:0:1::/64, local-use NAT64 64:ff9b:1::/48 and SRv6 5f00::/16
- The IETF protocol assignments block 2001::/23 (Teredo 2001::/32, benchmarking, the deprecated ORCHID and the unassigned rest), except its globally reachable entries: 2001:1::1, 2001:1::2 and 2001:1::3, AMT 2001:3::/32, AS112 2001:4:112::/48, ORCHIDv2 2001:20::/28 and drone remote ID 2001:30::/28
- An IPv6 address that embeds an IPv4 address - IPv4-mapped ::ffff:0:0/96, NAT64 64:ff9b::/96 and 6to4 2002::/16 - is blocked when the embedded IPv4 address is
The globally reachable entries of the IPv4 registry (192.31.196.0/24, 192.52.193.0/24, 192.175.48.0/24) are allowed.
== Name resolution == validateUrl and validateHostname resolve the host with InetAddress.getAllByName, which cannot be interrupted: a caller that runs them under a deadline (as the HTTP tool does) can stop waiting, but the thread doing the lookup stays blocked until the system resolver answers or gives up, by its own timeout.
Both return the addresses they vetted. A caller that connects to one of those addresses (sending the original Host header and, for https, the original server name) is safe from a host whose DNS answer changes after the check (DNS rebinding); a caller that hands the URL to a client that resolves the name again is not.
== Errors == A refusal - a blocked host name or address, a protocol that is not allowed, a URL that cannot be parsed - is a org.llm4s.error.ValidationError: asking again gives the same answer, so it is not retryable. A name that cannot be resolved is a org.llm4s.error.NetworkError, which is.
Attributes
- Example
-
import org.llm4s.security.NetworkSecurity // Validate a URL before fetching NetworkSecurity.validateUrl("https://example.com/api") // Right(the addresses example.com resolves to) NetworkSecurity.validateUrl("http://169.254.169.254/") // Left(ValidationError) NetworkSecurity.validateUrl("http://192.168.1.1/admin") // Left(ValidationError) - Source
- NetworkSecurity.scala
- Supertypes
- Self type
-
NetworkSecurity.type
Safety wrapper for user-supplied regex compilation and matching.
Safety wrapper for user-supplied regex compilation and matching.
ReDoS is mitigated by two complementary defences:
- A cheap pre-screen (safeCompile) rejects null/empty/oversized patterns and a set of well-known catastrophic-backtracking shapes before they are ever compiled.
- A hard execution bound (safeFind) caps the number of character accesses the backtracking engine may perform for a single match, without relying on thread interruption or wall-clock timeouts.
The step bound is the real security boundary, and safeCompile's structural check is what makes it sound (#3051): the bound counts reads, and the engine could otherwise do unbounded work without reading, in many required iterations of a loop whose body matches the empty string ((){2000000000}b) or across many ways of matching the empty string at the end of the input ((?:|)(?:|)...b). With every pattern refused that can take more than 1000 steps at one position without a read, the work between two reads is bounded. The shape blocklist is only a fast early-reject and must not be relied on for safety.
Every refusal is a Left of org.llm4s.error.ValidationError, for the field pattern or input; nothing here throws.
NOTE: kept intentionally in sync with org.llm4s.runner.WorkspaceRegexSafetyManager, which duplicates this logic because the workspace runner module cannot depend on core (and so returns Either[String, _], not Result). Changes to the heuristics or bounds here should be mirrored there.
Attributes
- Source
- RegexSafetyManager.scala
- Supertypes
- Self type
-
RegexSafetyManager.type
A credential. Its toString is ***, and its value is read only through reveal. Creating one registers the value with llm4s, which then removes it, by exact match, from the error messages, exchange logs and other diagnostic text llm4s builds (traces follow in a later release). Values under 8 characters are not registered (they would mask ordinary words). A null value is read as "": never registered, and revealed as "".
A credential. Its toString is ***, and its value is read only through reveal. Creating one registers the value with llm4s, which then removes it, by exact match, from the error messages, exchange logs and other diagnostic text llm4s builds (traces follow in a later release). Values under 8 characters are not registered (they would mask ordinary words). A null value is read as "": never registered, and revealed as "".
The registry is process-wide and there is no public way to remove a value from it; the companion says which factory keeps a value for the life of the process and which lets it go.
Attributes
- Companion
- object
- Source
- Secret.scala
- Supertypes
The factories of Secret, and the contract of the registry behind them.
The factories of Secret, and the contract of the registry behind them.
Creating a Secret registers its value process-wide, for exact-match scrubbing of the diagnostic text llm4s builds. Which factory creates it decides how long the value stays registered:
- Secret.apply is a configured credential. It is never evicted: it stays registered for the life of the process, whether or not the
Secretis still referenced. So is the password of Secret.basic. - Secret.token is a short-lived token. Once the registry holds more than 4096 values, the tokens registered longest ago are evicted to bring it back to that number.
- A value under 8 characters is not registered by either.
- There is no public way to forget a value.
The consequence: Secret(...) is for a fixed set of long-lived credentials - the keys an application is configured with. A key created per tenant, per user or per request must be a Secret.token(...). Created with Secret(...), each distinct one is held, and matched against every scrubbed text, until the process exits: the registry grows without bound, and llm4s logs one warning when it passes 4096 values.
Attributes
- Companion
- class
- Source
- Secret.scala
- Supertypes
- Self type
-
Secret.type
