-
Notifications
You must be signed in to change notification settings - Fork 0
Add --file_write_strategy
to Bazel
#9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
# Conflicts: # src/main/java/com/google/devtools/build/lib/actions/FileArtifactValue.java # src/main/java/com/google/devtools/build/lib/remote/AbstractActionInputPrefetcher.java # src/main/java/com/google/devtools/build/lib/skyframe/CompletionFunction.java # Conflicts: # src/main/java/com/google/devtools/build/lib/actions/FileArtifactValue.java
� Conflicts: � src/main/java/com/google/devtools/build/lib/actions/cache/VirtualActionInput.java � src/main/java/com/google/devtools/build/lib/remote/merkletree/DirectoryTree.java � src/main/java/com/google/devtools/build/lib/remote/merkletree/MerkleTree.java
# Conflicts: # src/test/java/com/google/devtools/build/lib/actions/ActionCacheCheckerTest.java # src/test/java/com/google/devtools/build/lib/skyframe/FilesystemValueCheckerTest.java
/refacto-test |
Refacto is reviewing this PR. Please wait for the review comments to be posted. |
Secure Implementation of Lazy File Writing Strategy👍 Well Done
📌 Files Processed
📝 Additional Comments
|
public InputStream getInputStream() { | ||
// TODO: Avoid materializing the full content in memory by using a variant of | ||
// Piped{Input,Output}Stream that works well with virtual threads. | ||
var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Integer.MAX_VALUE)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential Memory Exhaustion in Inline File Handling
The code attempts to allocate a ByteArrayOutputStream with a size potentially as large as Integer.MAX_VALUE. While there is a clamp function to limit the size, this could still lead to memory exhaustion if a malicious or corrupted file has a very large reported size. This could be exploited as a denial of service attack by causing the JVM to run out of memory when processing large files.
var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Integer.MAX_VALUE)); | |
// Define a reasonable maximum size (e.g., 10MB) | |
private static final int MAX_IN_MEMORY_SIZE = 10 * 1024 * 1024; | |
var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Math.min(MAX_IN_MEMORY_SIZE, Integer.MAX_VALUE))); |
Standards
- CWE-400
- A05:2021-Security Misconfiguration
public InputStream getInputStream() { | ||
// TODO: Avoid materializing the full content in memory by using a variant of | ||
// Piped{Input,Output}Stream that works well with virtual threads. | ||
var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Integer.MAX_VALUE)); | ||
try { | ||
writeTo(out); | ||
} catch (IOException e) { | ||
// writer is not expected to throw if out doesn't. | ||
throw new IllegalStateException(e); | ||
} | ||
return new ByteArrayInputStream(out.toByteArray()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential Resource Leak in getInputStream Method
The getInputStream method creates a ByteArrayOutputStream, writes to it, and then returns a ByteArrayInputStream without closing the ByteArrayOutputStream. While ByteArrayOutputStream doesn't use system resources that need explicit closing, this pattern could lead to resource leaks with other stream implementations or if the implementation changes in the future. Additionally, the exception handling assumes the writer won't throw if the output stream doesn't, which may not always be true.
public InputStream getInputStream() { | |
// TODO: Avoid materializing the full content in memory by using a variant of | |
// Piped{Input,Output}Stream that works well with virtual threads. | |
var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Integer.MAX_VALUE)); | |
try { | |
writeTo(out); | |
} catch (IOException e) { | |
// writer is not expected to throw if out doesn't. | |
throw new IllegalStateException(e); | |
} | |
return new ByteArrayInputStream(out.toByteArray()); | |
} | |
public InputStream getInputStream() { | |
// TODO: Avoid materializing the full content in memory by using a variant of | |
// Piped{Input,Output}Stream that works well with virtual threads. | |
try (var out = new ByteArrayOutputStream(Math.clamp(getSize(), 0, Integer.MAX_VALUE))) { | |
try { | |
writeTo(out); | |
return new ByteArrayInputStream(out.toByteArray()); | |
} catch (IOException e) { | |
// writer is not expected to throw if out doesn't. | |
throw new IllegalStateException(e); | |
} | |
} | |
} |
Standards
- CWE-772
- A06:2021-Vulnerable and Outdated Components
Work towards bazelbuild#24808
RELNOTES: If the new
--file_write_strategy
flag is set tolazy
, files produced by file write actions (e.g. the ones produced byctx.actions.write
andctx.actions.expand_template
) are not written to disk with BwoB unless required by local execution or upon explicit request. The default iseager
, which retains the current behavior.