|
| 1 | +--- |
| 2 | +id: 9e0d265f-d778-4115-9fc9-b09e37c55e41 |
| 3 | +slug: "security-manager-migration" |
| 4 | +title: "SecurityManager checks to explicit authorization" |
| 5 | +category: "security" |
| 6 | +difficulty: "advanced" |
| 7 | +jdkVersion: "24" |
| 8 | +oldLabel: "JDK 23 and earlier" |
| 9 | +modernLabel: "JDK 24+" |
| 10 | +oldApproach: "SecurityManager checks" |
| 11 | +modernApproach: "Explicit authorization" |
| 12 | +oldCode: |- |
| 13 | + SecurityManager manager = System.getSecurityManager(); |
| 14 | + if (manager != null) { |
| 15 | + manager.checkRead(path.toString()); |
| 16 | + } |
| 17 | + return Files.readString(path); |
| 18 | +modernCode: |- |
| 19 | + // Untrusted users must not be able to modify this tree |
| 20 | + Path root = allowedRoot.toRealPath(); |
| 21 | + Path resolved = root.resolve(requested) |
| 22 | + .normalize() |
| 23 | + .toRealPath(); |
| 24 | + if (!resolved.startsWith(root)) { |
| 25 | + throw new SecurityException( |
| 26 | + "Path is outside the allowed root"); |
| 27 | + } |
| 28 | + return Files.readString(resolved); |
| 29 | +summary: "Replace disabled SecurityManager checks with explicit application authorization\ |
| 30 | + \ and deployment isolation." |
| 31 | +explanation: "JEP 486 permanently disabled the Security Manager in JDK 24, so checks\ |
| 32 | + \ through System.getSecurityManager() can enforce policy only on JDK 23 and earlier.\ |
| 33 | + \ Applications must authorize access explicitly in domain logic; resolving real filesystem\ |
| 34 | + \ paths prevents existing symbolic links from escaping an allowed root when untrusted\ |
| 35 | + \ users cannot modify that tree concurrently. For attacker-writable trees, use race-resistant,\ |
| 36 | + \ handle-relative access such as SecureDirectoryStream. Use process, container, or\ |
| 37 | + \ operating-system boundaries for isolation. JDK 24 retains the deprecated API temporarily,\ |
| 38 | + \ but it cannot be enabled and is not replaced by another in-process sandbox." |
| 39 | +whyModernWins: |
| 40 | +- icon: "🔍" |
| 41 | + title: "Explicit policy" |
| 42 | + desc: "Authorization is visible and testable in application logic." |
| 43 | +- icon: "🛡️" |
| 44 | + title: "Real isolation" |
| 45 | + desc: "Process, container, and operating-system boundaries protect the whole application." |
| 46 | +- icon: "🚫" |
| 47 | + title: "Required migration" |
| 48 | + desc: "Removes checks that can no longer enforce policy on JDK 24 and later." |
| 49 | +support: |
| 50 | + state: "available" |
| 51 | + description: "Required on JDK 24 and later, where JEP 486 permanently disables the\ |
| 52 | + \ Security Manager." |
| 53 | +prev: "security/random-generator" |
| 54 | +next: "tooling/jshell-prototyping" |
| 55 | +related: |
| 56 | +- "io/reading-files" |
| 57 | +- "io/deserialization-filters" |
| 58 | +- "security/tls-default" |
| 59 | +tags: |
| 60 | +- security |
| 61 | +- migration |
| 62 | +docs: |
| 63 | +- title: "Permanently Disable the Security Manager (JEP 486)" |
| 64 | + href: "https://openjdk.org/jeps/486" |
| 65 | +- title: "SecureDirectoryStream" |
| 66 | + href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/nio/file/SecureDirectoryStream.html" |
0 commit comments