fix(compilers): jail Fe source writes to the compilation temp dir #2933
Remove this pre-check and the helper assertFeSourcesStayInside. The reasons:
1. The check in the write loop is sufficient. It stops the compilation before the code writes one byte.
2. Escape detection for a relative key does not change with the root directory. Thus this call cannot find a problem that the loop does not find.
3. The root sourcify-fe-jail-root does not exist. It is not the real tmpDir. This can confuse the reader.
The pre-check only moves the error before getFeExecutable. But the binary is in the cache after the first download. If an early error is important: create the temp directory first, then do the check on the real tmpDir.
---
_Generated by Claude Code_
The condition resolved !== rootResolved permits a key that resolves to the root itself. Example: src/...
The middleware also permits this key. There, the key normalizes to .. That value is not absolute and does not start with ...
The result: fs.writeFile(tmpDir) fails with a raw EISDIR error, not with a clear validation error.
A source key must point to a file inside the root. Reject the equal path also:
if (!resolved.startsWith(prefix)) {
---
_Generated by Claude Code_
This block is not necessary for the fix. The jail in useFeCompiler stops the write in all cases. But this block gives a clean 400 error at request time, not a failed verification job. Keep it for that behavior. Remove it for the smallest diff.
If the block stays, two points:
1. The rules here are different from resolveFeSourcePath. This check changes \ to / and normalizes the path. The compiler jail does not. Thus the two layers accept different key sets. Example: src/.. passes here, but causes an fs error in the compiler (see the comment there). The server has @ethereum-sourcify/compilers as a direct dependency, and the PR exports resolveFeSourcePath. Use the same rules in the two places, or add a comment that says the middleware is more strict.
2. The comment text is not clear: "does not stop src/../... path.join on the compiler treats that as". Add the missing sentence break. Also, this loop uses the name normalized again. The block above uses the same name for the sources record. The two scopes are separate, but one name with two meanings in one function decreases readability.
---
_Generated by Claude Code_
The marker path and the injected key can point to different files. The key contains the fixed string /tmp/sourcify-fe-escape-probe. The marker value comes from os.tmpdir(). On macOS, os.tmpdir() is /var/folders/.... There, existsSync(marker) examines a different path, and the assertion passes without value.
Also, the jail throws before the code writes a file. Thus the marker assertion shows intent more than it guards the write.
Two possible corrections:
os.tmpdir(). Then the marker is the true write target.---
_Generated by Claude Code_
Thanks for the review. You were right on all four.
Dropped the fake-root pre-check. The write loop is the jail. resolveFeSourcePath now rejects keys that resolve to the root (src/..), so that is a validation error instead of EISDIR. Middleware keeps the 400-before-job behavior and now calls the same resolver. Removed the marker file from the compiler test.
Pushed on a3c37c2.
Dropped. The write loop is the jail. No fake root.
Agreed. src/.. now fails the prefix check, so it is a validation error instead of EISDIR.
Kept the 400 so a bad key never starts a job. It now calls resolveFeSourcePath, same rules as the write loop.
Removed the marker. The unit test now covers src/.. and the escape key, and those assertions do the work.
Looks good. Thanks for this finding!
Thanks for the review and the merge. Glad the follow-up landed cleanly.
manuelwedler
SashaMIT
Summary
useFeCompilerwrotepath.join(tmpDir, sourcePath). A Fe verify body whose source keys start withsrc/(the API prefix check) can still besrc/../../../tmp/..., whichpath.jointreats as a write outside the temp ingot.The attacker already controls
stdJsonInput.sourceskeys on the public verify API. Thesrc/prefix does not dominate...Jail is resolve-under-dir fail-closed in
@ethereum-sourcify/compilers(covers library callers) and the same class is rejected invalidateAndNormalizeFeInput.Test plan
mocha --grep 'escape|resolve Fe source'inpackages/compilers(2/2)Should resolve Fe source paths under the compilation directoryfails (expected [Function] to throw)Made with Cursor