This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new c37708127bb4 [backport camel-4.22.x] CAMEL-28020: camel-salesforce - 
validate REST and raw client request URLs and encode id segments (#31321)
c37708127bb4 is described below

commit c37708127bb42c3d48659f204c71ef4c28efbeae
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Dec 3 20:23:28 2026 +0100

    [backport camel-4.22.x] CAMEL-28020: camel-salesforce - validate REST and 
raw client request URLs and encode id segments (#31321)
    
    (cherry picked from commit 1b237910a5ba3eb23332fbe14f7c04cbd5910a51)
    
    Co-Authored-By: Claude Fable 5.2 <[email protected]>
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
---
 .../internal/client/AbstractClientBase.java        |  43 ++++++
 .../internal/client/DefaultRawClient.java          |  10 +-
 .../internal/client/DefaultRestClient.java         |  33 ++++-
 .../client/DefaultRawClientRequestUrlTest.java     | 105 ++++++++++++++
 .../client/DefaultRestClientRequestUrlTest.java    | 160 +++++++++++++++++++++
 5 files changed, 344 insertions(+), 7 deletions(-)

diff --git 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBase.java
 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBase.java
index af142cf6773f..9326242d28c1 100644
--- 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBase.java
+++ 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBase.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.salesforce.internal.client;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -176,6 +177,48 @@ public abstract class AbstractClientBase extends 
ServiceSupport
         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)) {
+            throw new SalesforceException(
+                    "Request URL must target the configured Salesforce 
instance " + instanceUrl + ": " + requestUrl);
+        }
+        return requestUrl;
+    }
+
+    private static boolean onSameInstance(URI instanceUri, URI requestUri) {
+        return equalsIgnoreCase(instanceUri.getScheme(), 
requestUri.getScheme())
+                && equalsIgnoreCase(instanceUri.getHost(), 
requestUri.getHost())
+                && instanceUri.getPort() == requestUri.getPort();
+    }
+
+    private static boolean equalsIgnoreCase(String left, String right) {
+        return left == null ? right == null : left.equalsIgnoreCase(right);
+    }
+
     protected interface ClientResponseCallback {
         void onResponse(InputStream response, Map<String, String> headers, 
SalesforceException ex);
     }
diff --git 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClient.java
 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClient.java
index 27f59e4a9cff..21a96bf88e7c 100644
--- 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClient.java
+++ 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClient.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.salesforce.internal.client;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -81,7 +82,14 @@ public class DefaultRawClient extends AbstractClientBase 
implements RawClient {
     public void makeRequest(
             String method, String path, PayloadFormat format, InputStream 
body, Map<String, List<String>> headers,
             ResponseCallback callback) {
-        final Request request = getRequest(method, instanceUrl + path, 
headers);
+        final String requestUrl;
+        try {
+            requestUrl = resolveInstanceUrl(path);
+        } catch (SalesforceException e) {
+            callback.onResponse(null, Collections.emptyMap(), e);
+            return;
+        }
+        final Request request = getRequest(method, requestUrl, headers);
         final String contentType = PayloadFormat.JSON.equals(format) ? 
APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8;
         if (!request.getHeaders().contains(HttpHeader.ACCEPT)) {
             request.headers(h -> h.add(HttpHeader.ACCEPT, contentType));
diff --git 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java
 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java
index ef428c95184a..2330c57d91dd 100644
--- 
a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java
+++ 
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClient.java
@@ -155,7 +155,7 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
             }
             params = fieldsValue.toString();
         }
-        Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" 
+ id + params), headers);
+        Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" 
+ encodePathSegment(id) + params), headers);
         // requires authorization token
         setAccessToken(get);
 
@@ -280,7 +280,7 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
     @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);
         // requires authorization token
         setAccessToken(patch);
 
@@ -296,7 +296,7 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
             String sObjectName, String id, Object sObjectDto, InputStream 
sObject,
             Map<String, List<String>> headers, ResponseCallback callback) {
         try {
-            final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName 
+ "/" + id), headers);
+            final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName 
+ "/" + encodePathSegment(id)), headers);
             setAccessToken(patch);
 
             try (MultiPartRequestContent multipartContent = new 
MultiPartRequestContent()) {
@@ -361,7 +361,7 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
 
     @Override
     public void deleteSObject(String sObjectName, String id, Map<String, 
List<String>> headers, ResponseCallback callback) {
-        final Request delete = getRequest(HttpMethod.DELETE, 
sobjectsUrl(sObjectName + "/" + id), headers);
+        final Request delete = getRequest(HttpMethod.DELETE, 
sobjectsUrl(sObjectName + "/" + encodePathSegment(id)), headers);
 
         // requires authorization token
         setAccessToken(delete);
@@ -414,7 +414,9 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
     @Override
     public void getBlobField(
             String sObjectName, String id, String blobFieldName, Map<String, 
List<String>> headers, ResponseCallback callback) {
-        final Request get = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName 
+ "/" + id + "/" + blobFieldName), headers);
+        final Request get
+                = getRequest(HttpMethod.GET, sobjectsUrl(sObjectName + "/" + 
encodePathSegment(id) + "/" + blobFieldName),
+                        headers);
         // TODO this doesn't seem to be required, the response is always the
         // content binary stream
         // get.header(HttpHeader.ACCEPT_ENCODING, "base64");
@@ -445,7 +447,14 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
 
     @Override
     public void queryMore(String nextRecordsUrl, Map<String, List<String>> 
headers, ResponseCallback callback) {
-        final Request get = getRequest(HttpMethod.GET, instanceUrl + 
nextRecordsUrl, headers);
+        final String requestUrl;
+        try {
+            requestUrl = resolveInstanceUrl(nextRecordsUrl);
+        } catch (SalesforceException e) {
+            callback.onResponse(null, Collections.emptyMap(), e);
+            return;
+        }
+        final Request get = getRequest(HttpMethod.GET, requestUrl, headers);
 
         // requires authorization token
         setAccessToken(get);
@@ -629,6 +638,18 @@ public class DefaultRestClient extends AbstractClientBase 
implements RestClient
         return encodedQuery;
     }
 
+    private String encodePathSegment(String value) {
+        if (value == null || value.isEmpty()) {
+            throw new IllegalArgumentException("The record id must not be null 
or empty");
+        }
+        try {
+            return urlEncode(value);
+        } catch (UnsupportedEncodingException e) {
+            // UTF-8 is always available, so this is not expected to happen
+            throw new IllegalArgumentException("Unexpected error: " + 
e.getMessage(), e);
+        }
+    }
+
     private static class DelegatingClientCallback implements 
ClientResponseCallback {
         private final ResponseCallback callback;
 
diff --git 
a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClientRequestUrlTest.java
 
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClientRequestUrlTest.java
new file mode 100644
index 000000000000..8deda7983cc6
--- /dev/null
+++ 
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRawClientRequestUrlTest.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.salesforce.internal.client;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.component.salesforce.SalesforceHttpClient;
+import org.apache.camel.component.salesforce.SalesforceLoginConfig;
+import org.apache.camel.component.salesforce.api.SalesforceException;
+import org.apache.camel.component.salesforce.internal.PayloadFormat;
+import org.apache.camel.component.salesforce.internal.SalesforceSession;
+import org.eclipse.jetty.client.Request;
+import org.eclipse.jetty.http.HttpFields;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Verifies that {@link DefaultRawClient} keeps the request URL it builds on 
the configured Salesforce instance.
+ */
+public class DefaultRawClientRequestUrlTest {
+
+    private static final String INSTANCE_URL = 
"https://myinstance.my.salesforce.com";;
+
+    private RecordingRawClient client;
+
+    @BeforeEach
+    public void setup() throws SalesforceException {
+        client = new RecordingRawClient();
+        client.setInstanceUrl(INSTANCE_URL);
+    }
+
+    @Test
+    public void makeRequestKeepsRequestOnConfiguredInstance() {
+        final SalesforceException reported = 
makeRequest("/services/data/v64.0/limits");
+
+        assertNull(reported);
+        assertEquals(INSTANCE_URL + "/services/data/v64.0/limits", 
client.requestUrl);
+    }
+
+    @Test
+    public void makeRequestRejectsPathWithUserInfo() {
+        final SalesforceException reported = 
makeRequest("@replaced.example/services/data/v64.0/limits");
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    @Test
+    public void makeRequestRejectsPathWithAuthorityPrefix() {
+        final SalesforceException reported = 
makeRequest("//replaced.example/services/data/v64.0/limits");
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    private SalesforceException makeRequest(String path) {
+        final SalesforceException[] reported = new SalesforceException[1];
+        client.makeRequest("GET", path, PayloadFormat.JSON, null, 
Collections.emptyMap(),
+                (response, headers, exception) -> reported[0] = exception);
+        return reported[0];
+    }
+
+    private static final class RecordingRawClient extends DefaultRawClient {
+        private String requestUrl;
+
+        private RecordingRawClient() throws SalesforceException {
+            super(mock(SalesforceHttpClient.class), "64.0", 
mock(SalesforceSession.class), new SalesforceLoginConfig());
+        }
+
+        @Override
+        protected Request getRequest(String method, String url, Map<String, 
List<String>> headers) {
+            this.requestUrl = url;
+            final Request request = mock(Request.class);
+            when(request.getHeaders()).thenReturn(HttpFields.build());
+            return request;
+        }
+
+        @Override
+        protected void doHttpRequest(Request request, ClientResponseCallback 
callback) {
+            // no network: the resolved request URL is captured in getRequest
+        }
+    }
+}
diff --git 
a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClientRequestUrlTest.java
 
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClientRequestUrlTest.java
new file mode 100644
index 000000000000..2e45a061d400
--- /dev/null
+++ 
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/DefaultRestClientRequestUrlTest.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.salesforce.internal.client;
+
+import java.io.ByteArrayInputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.component.salesforce.SalesforceHttpClient;
+import org.apache.camel.component.salesforce.SalesforceLoginConfig;
+import org.apache.camel.component.salesforce.api.SalesforceException;
+import org.apache.camel.component.salesforce.internal.SalesforceSession;
+import org.eclipse.jetty.client.Request;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Verifies that {@link DefaultRestClient} keeps the request URL it builds on 
the configured Salesforce instance and
+ * percent-encodes the record id placed in the request path.
+ */
+public class DefaultRestClientRequestUrlTest {
+
+    private static final String VERSION = "64.0";
+    private static final String INSTANCE_URL = 
"https://myinstance.my.salesforce.com";;
+    private static final String DATA_URL = INSTANCE_URL + "/services/data/v" + 
VERSION;
+
+    private RecordingRestClient client;
+
+    @BeforeEach
+    public void setup() throws SalesforceException {
+        client = new RecordingRestClient();
+        client.setInstanceUrl(INSTANCE_URL);
+    }
+
+    @Test
+    public void queryMoreKeepsRequestOnConfiguredInstance() {
+        final SalesforceException reported = 
queryMore("/services/data/v64.0/query/01g5000000AbcdEFGH-2000");
+
+        assertNull(reported);
+        assertEquals(INSTANCE_URL + 
"/services/data/v64.0/query/01g5000000AbcdEFGH-2000", client.requestUrl);
+    }
+
+    @Test
+    public void queryMoreRejectsNextRecordsUrlWithUserInfo() {
+        final SalesforceException reported = 
queryMore("@replaced.example/services/data/v64.0/query/01g-2000");
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    @Test
+    public void queryMoreRejectsNextRecordsUrlWithUserInfoAndPort() {
+        final SalesforceException reported = 
queryMore("@replaced.example:8443/services/data/v64.0/query/01g-2000");
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    @Test
+    public void queryMoreRejectsNextRecordsUrlWithAuthorityPrefix() {
+        final SalesforceException reported = 
queryMore("//replaced.example/services/data/v64.0/query/01g-2000");
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    @Test
+    public void queryMoreRejectsNextRecordsUrlWithAbsoluteUrl() {
+        final SalesforceException reported = 
queryMore("https://replaced.example/services/data/v64.0/query/01g-2000";);
+
+        assertNotNull(reported);
+        assertNull(client.requestUrl);
+    }
+
+    @Test
+    public void getSObjectEncodesIdSegment() {
+        client.getSObject("Account", "001/002", null, Collections.emptyMap(), 
noop());
+
+        assertEquals(DATA_URL + "/sobjects/Account/001%2F002", 
client.requestUrl);
+    }
+
+    @Test
+    public void updateSObjectEncodesIdSegment() {
+        client.updateSObject("Account", "001 002", new 
ByteArrayInputStream(new byte[0]), Collections.emptyMap(), noop());
+
+        assertEquals(DATA_URL + "/sobjects/Account/001%20002", 
client.requestUrl);
+    }
+
+    @Test
+    public void deleteSObjectEncodesIdSegment() {
+        client.deleteSObject("Account", "001?x=y", Collections.emptyMap(), 
noop());
+
+        assertEquals(DATA_URL + "/sobjects/Account/001%3Fx%3Dy", 
client.requestUrl);
+    }
+
+    @Test
+    public void getBlobFieldEncodesIdSegment() {
+        client.getBlobField("Attachment", "001#x", "Body", 
Collections.emptyMap(), noop());
+
+        assertEquals(DATA_URL + "/sobjects/Attachment/001%23x/Body", 
client.requestUrl);
+    }
+
+    @Test
+    public void updateSObjectMultipartEncodesIdSegment() {
+        client.updateSObjectMultipart("Account", "001/002", new Object(), new 
ByteArrayInputStream(new byte[0]),
+                Collections.emptyMap(), noop());
+
+        assertEquals(DATA_URL + "/sobjects/Account/001%2F002", 
client.requestUrl);
+    }
+
+    private SalesforceException queryMore(String nextRecordsUrl) {
+        final SalesforceException[] reported = new SalesforceException[1];
+        client.queryMore(nextRecordsUrl, Collections.emptyMap(), (response, 
headers, exception) -> reported[0] = exception);
+        return reported[0];
+    }
+
+    private static RestClient.ResponseCallback noop() {
+        return (response, headers, exception) -> {
+        };
+    }
+
+    private static final class RecordingRestClient extends DefaultRestClient {
+        private String requestUrl;
+
+        private RecordingRestClient() throws SalesforceException {
+            super(mock(SalesforceHttpClient.class), VERSION, 
mock(SalesforceSession.class), new SalesforceLoginConfig());
+        }
+
+        @Override
+        protected Request getRequest(String method, String url, Map<String, 
List<String>> headers) {
+            this.requestUrl = url;
+            return mock(Request.class);
+        }
+
+        @Override
+        protected void doHttpRequest(Request request, ClientResponseCallback 
callback) {
+            // no network: the resolved request URL is captured in getRequest
+        }
+    }
+}

Reply via email to