|
12 | 12 | * information: "Portions copyright [year] [name of copyright owner]". |
13 | 13 | * |
14 | 14 | * Copyright 2016 ForgeRock AS. |
| 15 | + * Portions copyright 2024-2026 3A Systems LLC |
15 | 16 | */ |
16 | 17 | package org.forgerock.openidm.maintenance.impl; |
17 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.InvalidPathException; |
18 | 22 | import java.nio.file.Path; |
19 | 23 | import java.nio.file.Paths; |
20 | 24 | import java.util.Iterator; |
21 | 25 |
|
22 | 26 | import org.forgerock.json.JsonValue; |
23 | 27 | import org.forgerock.json.resource.AbstractRequestHandler; |
| 28 | +import org.forgerock.json.resource.BadRequestException; |
24 | 29 | import org.forgerock.json.resource.InternalServerErrorException; |
25 | 30 | import org.forgerock.json.resource.ReadRequest; |
26 | 31 | import org.forgerock.json.resource.RequestHandler; |
@@ -57,16 +62,58 @@ public class UpdateArchiveService extends AbstractRequestHandler { |
57 | 62 | @Reference(policy= ReferencePolicy.STATIC) |
58 | 63 | private UpdateManager updateManager; |
59 | 64 |
|
| 65 | + File getInstallLocation() { |
| 66 | + return IdentityServer.getInstance().getInstallLocation(); |
| 67 | + } |
| 68 | + |
60 | 69 | @Override |
61 | 70 | public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest request) { |
62 | 71 | final String archiveName = request.getResourcePathObject().get(0); |
63 | | - final Path archivePath = IdentityServer.getInstance().getInstallLocation().toPath().resolve("bin/update").resolve(archiveName); |
| 72 | + // Resolve the real path of the allowed directory (collapses symlinks + "..") |
| 73 | + final Path updateBaseDir; |
| 74 | + try { |
| 75 | + updateBaseDir = getInstallLocation().toPath() |
| 76 | + .resolve("bin/update") |
| 77 | + .toRealPath(); // throws IOException if directory absent |
| 78 | + } catch (IOException e) { |
| 79 | + return new InternalServerErrorException("Unable to resolve update directory", e).asPromise(); |
| 80 | + } |
| 81 | + |
| 82 | + // Normalize the resolved archive path, then enforce prefix |
| 83 | + final Path archivePath; |
| 84 | + try { |
| 85 | + archivePath = updateBaseDir.resolve(archiveName).normalize(); |
| 86 | + } catch (InvalidPathException e) { |
| 87 | + return new BadRequestException("Invalid archive path", e).asPromise(); |
| 88 | + } |
| 89 | + if (!archivePath.startsWith(updateBaseDir)) { |
| 90 | + return new BadRequestException("Invalid archive path").asPromise(); |
| 91 | + } |
| 92 | + |
| 93 | + // Defense in depth: resolve symlinks on the archive itself, then re-verify containment. |
| 94 | + try { |
| 95 | + final Path realArchivePath = archivePath.toRealPath(); |
| 96 | + if (!realArchivePath.startsWith(updateBaseDir)) { |
| 97 | + return new BadRequestException("Invalid archive path").asPromise(); |
| 98 | + } |
| 99 | + } catch (IOException e) { |
| 100 | + return new BadRequestException("Invalid archive path", e).asPromise(); |
| 101 | + } |
| 102 | + |
64 | 103 | Path requestedFile = Paths.get(""); |
65 | 104 | Iterator<String> it = request.getResourcePathObject().tail(1).iterator(); |
66 | 105 | while (it.hasNext()) { |
67 | 106 | requestedFile = requestedFile.resolve(it.next()); |
68 | 107 | } |
69 | 108 |
|
| 109 | + // Normalize to collapse any ".." sequences embedded in the segments |
| 110 | + requestedFile = requestedFile.normalize(); |
| 111 | + |
| 112 | + // Reject absolute paths or paths that start with ".." after normalization |
| 113 | + if (requestedFile.isAbsolute() || requestedFile.startsWith("..")) { |
| 114 | + return new BadRequestException("Invalid file path within archive").asPromise(); |
| 115 | + } |
| 116 | + |
70 | 117 | try { |
71 | 118 | final JsonValue fileContents = updateManager.getArchiveFile(archivePath, requestedFile); |
72 | 119 | return Responses.newResourceResponse(null, null, fileContents).asPromise(); |
|
0 commit comments