Improved: Extend URL_RESOURCE validation to writeDataResourceText, completing the work of af8ee514a2#1031
Merged
Conversation
…mpleting the work of af8ee51
There was a problem hiding this comment.
Pull request overview
This PR completes prior URL_RESOURCE hardening work by extending the existing URL_RESOURCE validation to the writeDataResourceText code path in DataResourceWorker, aiming for consistent handling across URL_RESOURCE consumers.
Changes:
- Add a call to
checkUrlResourceAllowed(url)before reading from an absolute URL inwriteDataResourceText.
Comments suppressed due to low confidence (2)
applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java:1174
checkUrlResourceAllowed(url)is only applied whenurl.getHost() != null. In the relative-URL branch you buildfixedUrland fetch content without any SSRF validation, which makes URL_RESOURCE handling inconsistent withgetDataResourceStream()and leaves a bypass path. Consider normalizing to a single final URL (convert relative to absolute first) and then callingcheckUrlResourceAllowed()on that final URL before any network I/O.
if (url.getHost() != null) { // is absolute
checkUrlResourceAllowed(url);
int c;
try (InputStream in = url.openStream(); StringWriter sw = new StringWriter()) {
while ((c = in.read()) != -1) {
sw.write(c);
}
text = sw.toString();
}
} else {
String prefix = DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https);
String sep = "";
if (url.toString().indexOf('/') != 0 && prefix.lastIndexOf('/') != (prefix.length() - 1)) {
sep = "/";
}
String fixedUrlStr = prefix + sep + url.toString();
URL fixedUrl = UtilURL.fromUrlString(fixedUrlStr);
text = (String) fixedUrl.getContent();
applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java:1164
- This URL fetch path uses
url.openStream()/fixedUrl.getContent()without connect/read timeouts, without disabling redirects, and without a maximum response size. That can lead to hangs and memory exhaustion (response is fully buffered into aStringWriter), and redirects could be used to pivot to disallowed targets. Consider aligning this implementation withgetDataResourceStream()(URLConnetion w/ timeouts, redirects disabled, and a bounded read).
checkUrlResourceAllowed(url);
int c;
try (InputStream in = url.openStream(); StringWriter sw = new StringWriter()) {
while ((c = in.read()) != -1) {
sw.write(c);
}
text = sw.toString();
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is a completion of the improvements performed in a previous change in the commit af8ee51, related to data resource validation. This change extends the existing validation to writeDataResourceText, ensuring consistent URL_RESOURCE handling across both methods in DataResourceWorker.