Skip to content

Commit eaecd75

Browse files
committed
loader: resolve the jail root once in the constructor
1 parent 99f3cf7 commit eaecd75

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

‎src/main/java/com/squarespace/less/JailedFilesystemLessLoader.java‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public class JailedFilesystemLessLoader extends FilesystemLessLoader {
4545
*/
4646
private final Path jailRoot;
4747

48+
/**
49+
* Real path of the jail root. The root itself may sit behind symlinks
50+
* (e.g. /tmp on macOS), so containment compares real paths on both
51+
* sides. Falls back to the lexical root when it cannot be resolved.
52+
*/
53+
private final Path realJailRoot;
54+
4855
public JailedFilesystemLessLoader(Path jailRoot) {
4956
this.jailRoot = jailRoot.toAbsolutePath().normalize();
57+
// The root is final and set once here, so resolve its real path once
58+
// at construction instead of on every containment check.
59+
Path real;
60+
try {
61+
real = this.jailRoot.toRealPath();
62+
} catch (IOException e) {
63+
real = this.jailRoot;
64+
}
65+
this.realJailRoot = real;
5066
}
5167

5268
@Override
@@ -82,7 +98,7 @@ private Path resolveContained(Path path) {
8298
}
8399
try {
84100
Path real = candidate.toRealPath();
85-
if (!real.startsWith(realJailRoot())) {
101+
if (!real.startsWith(realJailRoot)) {
86102
return null;
87103
}
88104
return real;
@@ -92,17 +108,4 @@ private Path resolveContained(Path path) {
92108
}
93109
}
94110

95-
/**
96-
* Real path of the jail root. The root itself may sit behind symlinks
97-
* (e.g. /tmp on macOS), so containment compares real paths on both
98-
* sides. Falls back to the lexical root when it cannot be resolved.
99-
*/
100-
private Path realJailRoot() {
101-
try {
102-
return jailRoot.toRealPath();
103-
} catch (IOException e) {
104-
return jailRoot;
105-
}
106-
}
107-
108111
}

0 commit comments

Comments
 (0)