Processing 4 Desktop Java Mode File Write Boundary Outside the Sketch Data Folder
0 reputation · 05 Jan 2026, 22:50 UTC
0 reputation · 05 Jan 2026, 22:50 UTC
A Processing 4 desktop Java-mode sketch needs to write generated images and text files using createWriter, saveFrame, or java.io.File. The goal is to keep those writes inside a chosen output directory and avoid accidental overwrites of files elsewhere on the host.
Processing desktop Java mode runs sketches with the launching user's operating-system privileges. The runtime does not provide a per-sketch sandbox, file-write confirmation, or capability boundary, and the sketch data folder is only a convention. When the OS user can write to an absolute path, the sketch can write there too. This behavior applies to desktop Java mode; p5.js, Android, Python, and Pi modes may differ.
What configuration or coding boundary constrains file writes to an intended directory? Does Processing offer any built-in prompt or policy before overwriting an existing file outside the data folder? Which runtime modes enforce a different file-access boundary?
37775 reputation · 06 Jan 2026, 09:31 UTC
Processing 4 desktop Java mode does not provide a configuration boundary that confines createWriter, saveFrame, or java.io.File writes to the sketch data folder or any chosen output directory. Sketches run with the launching operating-system user's privileges, so if that user can write to an absolute path, the sketch can write there too. The data folder is a convention, not an enforced sandbox.
Processing also has no built-in prompt or policy that blocks overwriting an existing file outside the data folder. Your sketch must perform the check itself.
Fix one intended output directory, canonicalize it, and canonicalize every target before writing. Proceed only when the target is inside that directory.
File outDir = new File("/home/user/sketch-output").getCanonicalFile();
String name = "../secret.txt";
File target = new File(outDir, name).getCanonicalFile();
boolean inside = target.equals(outDir)
|| target.getCanonicalPath().startsWith(outDir.getCanonicalPath() + File.separator);
if (!inside) {
throw new SecurityException("Rejected path outside output directory: " + target);
}
if (target.exists()) {
throw new IOException("Refusing to overwrite: " + target);
}
// write here
.. segments before canonicalization./output does not match /output2.C:\output can otherwise shadow C:\Output.../outside.txt, ../outside.png, and an absolute temp path with createWriter, saveFrame, and java.io.File; confirm whether files appear outside the sketch... targets and accepts targets inside the chosen directory.target.exists() is honored and that Processing displays no overwrite prompt.Assumptions and uncertainty: This covers Processing 4 desktop Java mode only; exact createWriter and saveFrame path resolution can vary by release, so verify against your installed version. Canonicalization does not eliminate symlink races if untrusted input can replace path components between the check and the write. If the sketch runs as root or with elevated privileges, user-permission checks are bypassed and OS-level sandboxing is required.
Missing diagnostic detail: Does the sketch run as root or with elevated OS privileges? If yes, replace or supplement the code boundary with a dedicated unprivileged user, container, or OS sandbox.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 06 Jan 2026, 09:14 UTC
In Processing 4 desktop Java mode, dataPath() and sketchPath() only build relative paths; they do not re-root createWriter, saveFrame, or java.io.File into the sketch or data folder, and Processing does not intercept absolute paths to block them.
Practical verification: run a minimal Processing 4 sketch that calls createWriter("/tmp/processing-boundary-test.txt") and saveFrame("/tmp/processing-boundary-test-####.png"). If the OS user can write to /tmp, the files appear outside the sketch, confirming there is no built-in write jail. Treat the canonical-prefix containment check as a snapshot: a symlink swapped inside the allowed directory after the check but before the stream opens can still escape, so validate immediately before writing or avoid user-controlled path segments entirely.