Files
MyAiConfiguration/shared/rules/java/references/security.md
T
Julian LechnerandClaude Opus 5.5 6159c6373c
CI / CI configuration security checks (pull_request) Successful in 2s
CI / Capabilities and shell quality (pull_request) Successful in 15s
CI / Python tests and compilation (pull_request) Successful in 36s
CI / QMD unit tests (pull_request) Successful in 7s
CI / Build and inspect generated packages (pull_request) Successful in 16s
CI / Bash integration and installation guards (pull_request) Failing after 51s
CI / PowerShell tests on Ubuntu (pull_request) Failing after 1m45s
CI / Installed client configuration smoke tests (pull_request) Canceled after 1m24s
docs(rules): fix contradictions, update technical rules, enforce rule structure
- Resolve contradictions: authorization is never a skipped "boundary check";
  one browser token policy (BFF/HttpOnly cookies first, RFC 10017); one
  sanitizer-bypass rule; Angular responses are unknown until validated.
- Add security rules for AI/LLM integration and Java XXE, concrete crypto
  and password-hashing parameters, and RFC 8725 JWT validation.
- Update version-dependent guidance after checking official sources:
  Angular 22, TypeScript 6/7, Node 24+, .NET 10/C# 14, JDK 21-25, Spring
  Boot 4; add a Spring Boot and Spring Security topic.
- Give every rule a unique level-1 title and scope line, keep language
  security files to language specifics, and fix routing for inbound vs
  outbound HTTP, XML parsing, and WPF/Angular parent rules.
- Validate rule structure and routing in validate-config.py, with tests.
- Fix CI issues found by a local run: shellcheck directive order, pinned
  ruff rule set, Invoke-Python test mock, CRLF-tolerant package checks,
  Windows-only credential test, and refreshed prompt-budget baselines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 10:51:08 +01:00

14 lines
3.6 KiB
Markdown

# Java Security
Apply these rules to security-sensitive Java code. Load the shared security rule for each affected boundary (API, auth, web, data, files, network, crypto, supply chain, AI); this file adds only Java specifics, and the Spring rule covers Spring Boot and Spring Security.
* Use parameterized database APIs such as `PreparedStatement`. Select dynamic identifiers or sort expressions from a fixed allowlist; never concatenate untrusted data into SQL.
* Resolve file paths with `root.resolve(input).normalize()` and check containment with `Path.startsWith(root)`, never `String.startsWith`, which accepts `/data/app2` for `/data/app`. `normalize()` does not resolve symlinks; use `toRealPath()` when links matter and the file exists, and account for replacement races. Check every `ZipEntry` name the same way. Limit file size and processing work.
* Use `ProcessBuilder` with an absolute executable path and separate arguments; avoid shell interpretation. On Windows, the argument list is flattened into one command line that the target re-parses, and `.bat` or `.cmd` targets run through `cmd.exe`; never pass untrusted arguments to them, and validate attacker-controlled arguments against an allowlist.
* Parse untrusted XML with DTDs disabled (`http://apache.org/xml/features/disallow-doctype-decl` set to `true`), or, where DTDs are required, with external entities and external DTD and schema access disabled (`XMLConstants.ACCESS_EXTERNAL_DTD` and `ACCESS_EXTERNAL_SCHEMA` set to `""`, `XMLConstants.FEATURE_SECURE_PROCESSING` enabled). Apply this to every factory that reads untrusted XML: `DocumentBuilderFactory`, `SAXParserFactory`, `XMLInputFactory`, `TransformerFactory`, `SchemaFactory`, and JAXB unmarshallers.
* Deserialize into explicit allowed types and bound input size and nesting. Avoid native Java serialization for untrusted data; where it is unavoidable, install an `ObjectInputFilter` allowlist. With Jackson, never enable default typing (`activateDefaultTyping`) or `@JsonTypeInfo(use = Id.CLASS)` for untrusted input; use `Id.NAME` with registered subtypes or a strict `PolymorphicTypeValidator`. Spring Boot 4 uses Jackson 3 (`tools.jackson.*` packages; annotations stay in `com.fasterxml.jackson.annotation`). Load YAML with SnakeYAML 2.x or a `SafeConstructor`. Never evaluate untrusted SpEL, OGNL, or JNDI names.
* Use the platform's maintained TLS, cryptographic, and secure-random APIs with the parameters in the shared cryptography rule. Always specify the full `Cipher` transformation, for example `AES/GCM/NoPadding`, because `Cipher.getInstance("AES")` means ECB. Use `SecureRandom`, never `java.util.Random`, for security values, and `MessageDigest.isEqual` for constant-time comparison. Never install trust-all `X509TrustManager` or `HostnameVerifier` implementations. Do not rely on the Security Manager; JDK 24+ disables it permanently.
* For outbound URLs, parse with `java.net.URI` (the `URL` constructors are deprecated since JDK 20) and check every resolved `InetAddress` (`isLoopbackAddress`, `isLinkLocalAddress`, `isSiteLocalAddress`, `isAnyLocalAddress`, `isMulticastAddress`); these miss IPv6 unique-local `fc00::/7`, carrier-grade NAT `100.64.0.0/10`, and IPv4-mapped IPv6, so check those explicitly. Keep `java.net.http.HttpClient` on its default `Redirect.NEVER` or revalidate every hop.
* For regular expressions on untrusted input, bound the input length first and avoid nested or overlapping quantifiers, because `java.util.regex` has no timeout; use RE2/J for untrusted patterns.
* Encode output with the template engine's automatic escaping or the OWASP Java Encoder for the exact context; never build HTML, XML, LDAP, or JPQL from untrusted strings.