oscerd commented on code in PR #31250:
URL: https://github.com/apache/camel/pull/31250#discussion_r4768967753


##########
components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java:
##########
@@ -281,7 +281,7 @@ private Map<String, InputStream> getBinaryFieldMap(Object 
sObject) {
     @Override
     public void updateSObject(
             String sObjectName, String id, InputStream sObject, Map<String, 
List<String>> headers, ResponseCallback callback) {
-        final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName + 
"/" + id), headers);
+        final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName + 
"/" + encodePathSegment(id)), headers);

Review Comment:
   Fixed in 7179277b73f6 — `updateSObjectMultipart` now builds its URL with 
`encodePathSegment(id)` like the other id operations, and 
`DefaultRestClientRequestUrlTest.updateSObjectMultipartEncodesIdSegment` covers 
it. Thanks for catching it.



##########
components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java:
##########
@@ -630,6 +639,15 @@ private String urlEncode(String query) throws 
UnsupportedEncodingException {
         return encodedQuery;
     }
 
+    private String encodePathSegment(String value) {
+        try {
+            return urlEncode(value);

Review Comment:
   Addressed in 7179277b73f6 — `encodePathSegment` now rejects a null or empty 
id with a clear `IllegalArgumentException` ("The record id must not be null or 
empty") instead of the NPE.



##########
components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java:
##########
@@ -156,7 +156,7 @@ public void getSObject(
             }
             params = fieldsValue.toString();
         }
-        Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" 
+ id + params), headers);
+        Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" 
+ encodePathSegment(id) + params), headers);

Review Comment:
   Noted. `URLEncoder` leaves `.` unencoded, so an id of `..` stays `..`; it is 
a single segment appended to the fixed `/sobjects/<name>/` prefix and is not a 
valid record id (the API returns 404), so I left it rather than reject dots and 
risk a legitimate id. Happy to add a stricter id check as a follow-up if you 
prefer.



##########
components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBase.java:
##########
@@ -177,6 +178,48 @@ protected Request getRequest(String method, String url, 
Map<String, List<String>
         return request;
     }
 
+    /**
+     * Resolves the request URL for a path that is relative to the configured 
Salesforce instance and verifies that it
+     * stays on that instance. The {@code relativePath} must be an absolute 
path (it has to begin with a single
+     * {@code /}), and the fully resolved URL must keep the scheme, host and 
port of the configured instance URL.
+     * Operations that build a request URL by appending a caller-supplied path 
onto {@link #instanceUrl} use this to
+     * keep the request on the configured instance.
+     *
+     * @param  relativePath        the path to append to the configured 
instance URL
+     * @return                     the fully resolved request URL
+     * @throws SalesforceException if {@code relativePath} is not an absolute 
path on the configured instance
+     */
+    protected String resolveInstanceUrl(String relativePath) throws 
SalesforceException {
+        if (relativePath == null || !relativePath.startsWith("/") || 
relativePath.startsWith("//")) {
+            throw new SalesforceException(
+                    "Request path must be an absolute path on the configured 
Salesforce instance: " + relativePath);
+        }
+        final String requestUrl = instanceUrl + relativePath;
+        final URI instanceUri;
+        final URI requestUri;
+        try {
+            instanceUri = new URI(instanceUrl);
+            requestUri = new URI(requestUrl);
+        } catch (URISyntaxException e) {
+            throw new SalesforceException("Invalid request URL: " + 
requestUrl, e);
+        }
+        if (!onSameInstance(instanceUri, requestUri)) {

Review Comment:
   Keeping the scheme/host/port check in `resolveInstanceUrl` as 
defence-in-depth, as you suggested — it guards a future caller that might not 
enforce the leading `/`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to