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

davsclaus pushed a commit to branch CAMEL-27429-http-activity-mask-headers
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c5e1b9b06475836427297283c75319bf4c5f236a
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 26 16:44:54 2026 +0100

    CAMEL-27429: camel-http - logHttpActivity masks the values of sensitive 
headers
    
    The LoggingHttpActivityListener logged Authorization, Cookie and other
    credential headers in clear text, also with logMask=true, as the masking
    formatter does not understand the "Name: value" shape of HTTP headers.
    
    The value of a request or response header whose name is a sensitive key
    (SensitiveUtils), or one of Proxy-Authorization, Cookie, Set-Cookie,
    X-Api-Key and X-Auth-Token, is now always masked, e.g. Authorization: xxxxx.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../catalog/beans/LoggingHttpActivityListener.json |   2 +-
 .../apache/camel/catalog/docs/http-component.adoc  |  14 +++
 .../camel/bean/LoggingHttpActivityListener.json    |   2 +-
 .../camel-http/src/main/docs/http-component.adoc   |  14 +++
 .../http/LoggingHttpActivityListener.java          |  40 +++++-
 .../http/HttpLoggingActivityMaskHeadersTest.java   | 138 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |   8 ++
 7 files changed, 214 insertions(+), 4 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/LoggingHttpActivityListener.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/LoggingHttpActivityListener.json
index be50d5366347..06beeb039909 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/LoggingHttpActivityListener.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/LoggingHttpActivityListener.json
@@ -10,7 +10,7 @@
     "groupId": "org.apache.camel",
     "artifactId": "camel-http",
     "version": "4.24.0-SNAPSHOT",
-    "properties": { "loggingLevel": { "index": 0, "kind": "property", 
"displayName": "Logging Level", "required": false, "type": "enum", "javaType": 
"java.lang.String", "enum": [ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" 
], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 
"INFO", "description": "" }, "showRouteId": { "index": 1, "kind": "property", 
"displayName": "Show Route Id", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": fa [...]
+    "properties": { "loggingLevel": { "index": 0, "kind": "property", 
"displayName": "Logging Level", "required": false, "type": "enum", "javaType": 
"java.lang.String", "enum": [ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" 
], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 
"INFO", "description": "" }, "showRouteId": { "index": 1, "kind": "property", 
"displayName": "Show Route Id", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": fa [...]
   }
 }
 
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
index 6432adcde2f6..b23f10a1e93d 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/http-component.adoc
@@ -980,6 +980,20 @@ all routes and their endpoints from the updated component 
configuration after ea
 No special configuration is needed for `camel-http`: the fresh endpoints 
created during the route
 restart carry the newly resolved credentials.
 
+=== Logging HTTP activity
+
+With `logHttpActivity=true` (on the component or the endpoint), the HTTP 
requests that are sent and the HTTP
+responses that are received are logged, with their headers and body. To 
control what is logged, configure a
+`LoggingHttpActivityListener` bean with the options you need and set it as 
`httpActivityListener`.
+
+The value of a header that carries a credential is always masked, also when 
`logMask` is not enabled. These are the
+headers whose name is a sensitive key of Camel (such as `Authorization`, 
`Api-Key` or `Token`), and the
+`Proxy-Authorization`, `Cookie`, `Set-Cookie`, `X-Api-Key` and `X-Auth-Token` 
headers. The header name is kept, so
+the log shows `Authorization: xxxxx`.
+
+With `logMask=true`, the values of the other headers and the body are also 
masked with the masking formatter of
+Camel, which masks values of sensitive keys such as `password=secret`.
+
 === Advanced Usage
 
 If you need more control over the HTTP producer, you should use the
diff --git 
a/components/camel-http/src/generated/resources/META-INF/services/org/apache/camel/bean/LoggingHttpActivityListener.json
 
b/components/camel-http/src/generated/resources/META-INF/services/org/apache/camel/bean/LoggingHttpActivityListener.json
index be50d5366347..06beeb039909 100644
--- 
a/components/camel-http/src/generated/resources/META-INF/services/org/apache/camel/bean/LoggingHttpActivityListener.json
+++ 
b/components/camel-http/src/generated/resources/META-INF/services/org/apache/camel/bean/LoggingHttpActivityListener.json
@@ -10,7 +10,7 @@
     "groupId": "org.apache.camel",
     "artifactId": "camel-http",
     "version": "4.24.0-SNAPSHOT",
-    "properties": { "loggingLevel": { "index": 0, "kind": "property", 
"displayName": "Logging Level", "required": false, "type": "enum", "javaType": 
"java.lang.String", "enum": [ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" 
], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 
"INFO", "description": "" }, "showRouteId": { "index": 1, "kind": "property", 
"displayName": "Show Route Id", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": fa [...]
+    "properties": { "loggingLevel": { "index": 0, "kind": "property", 
"displayName": "Logging Level", "required": false, "type": "enum", "javaType": 
"java.lang.String", "enum": [ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" 
], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 
"INFO", "description": "" }, "showRouteId": { "index": 1, "kind": "property", 
"displayName": "Show Route Id", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": fa [...]
   }
 }
 
diff --git a/components/camel-http/src/main/docs/http-component.adoc 
b/components/camel-http/src/main/docs/http-component.adoc
index 6432adcde2f6..b23f10a1e93d 100644
--- a/components/camel-http/src/main/docs/http-component.adoc
+++ b/components/camel-http/src/main/docs/http-component.adoc
@@ -980,6 +980,20 @@ all routes and their endpoints from the updated component 
configuration after ea
 No special configuration is needed for `camel-http`: the fresh endpoints 
created during the route
 restart carry the newly resolved credentials.
 
+=== Logging HTTP activity
+
+With `logHttpActivity=true` (on the component or the endpoint), the HTTP 
requests that are sent and the HTTP
+responses that are received are logged, with their headers and body. To 
control what is logged, configure a
+`LoggingHttpActivityListener` bean with the options you need and set it as 
`httpActivityListener`.
+
+The value of a header that carries a credential is always masked, also when 
`logMask` is not enabled. These are the
+headers whose name is a sensitive key of Camel (such as `Authorization`, 
`Api-Key` or `Token`), and the
+`Proxy-Authorization`, `Cookie`, `Set-Cookie`, `X-Api-Key` and `X-Auth-Token` 
headers. The header name is kept, so
+the log shows `Authorization: xxxxx`.
+
+With `logMask=true`, the values of the other headers and the body are also 
masked with the masking formatter of
+Camel, which masks values of sensitive keys such as `password=secret`.
+
 === Advanced Usage
 
 If you need more control over the HTTP producer, you should use the
diff --git 
a/components/camel-http/src/main/java/org/apache/camel/component/http/LoggingHttpActivityListener.java
 
b/components/camel-http/src/main/java/org/apache/camel/component/http/LoggingHttpActivityListener.java
index ba675a044343..82cf2f2354df 100644
--- 
a/components/camel-http/src/main/java/org/apache/camel/component/http/LoggingHttpActivityListener.java
+++ 
b/components/camel-http/src/main/java/org/apache/camel/component/http/LoggingHttpActivityListener.java
@@ -21,6 +21,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
+import java.util.Set;
 import java.util.StringJoiner;
 
 import org.apache.camel.CamelContext;
@@ -36,6 +38,7 @@ import org.apache.camel.support.GZIPHelper;
 import org.apache.camel.support.processor.DefaultMaskingFormatter;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.SensitiveUtils;
 import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpEntity;
@@ -54,6 +57,15 @@ import static 
org.apache.camel.support.LoggerHelper.getLineNumberLoggerName;
 @Configurer(metadataOnly = true)
 public class LoggingHttpActivityListener extends ServiceSupport implements 
CamelContextAware, HttpActivityListener {
 
+    /**
+     * HTTP headers that carry a credential, but whose name is not a sensitive 
key known by {@link SensitiveUtils} (such
+     * as Authorization), in lower case.
+     */
+    private static final Set<String> SENSITIVE_HEADERS
+            = Set.of("proxy-authorization", "cookie", "set-cookie", 
"x-api-key", "x-auth-token");
+
+    private static final String DEFAULT_MASK = "xxxxx";
+
     private CamelContext camelContext;
     private MaskingFormatter maskingFormatter;
 
@@ -77,7 +89,8 @@ public class LoggingHttpActivityListener extends 
ServiceSupport implements Camel
     private boolean showBinary;
     @Metadata(defaultValue = "50000", description = "Limits the number of 
characters logged from the HTTP body")
     private int maxChars = 50000;
-    @Metadata(description = "If true, mask sensitive information like password 
or passphrase in the log")
+    @Metadata(description = "If true, mask sensitive information like password 
or passphrase in the log."
+                            + " The values of the headers that carry a 
credential (such as Authorization or Cookie) are always masked.")
     private Boolean logMask;
     @Metadata(defaultValue = "false", description = "If enabled then each 
information is outputted as separate LOG events")
     private boolean multiline;
@@ -155,7 +168,14 @@ public class LoggingHttpActivityListener extends 
ServiceSupport implements Camel
         if (showHeaders) {
             Header[] headers = request != null ? request.getHeaders() : 
response.getHeaders();
             for (Header h : headers) {
-                lines.add(String.format("%s: %s", h.getName(), 
getValue(h.isSensitive(), h.getValue())));
+                String value;
+                if (isSensitiveHeader(h.getName())) {
+                    // the value of a credential header is always masked as a 
whole
+                    value = getMaskString();
+                } else {
+                    value = getValue(h.isSensitive(), h.getValue());
+                }
+                lines.add(String.format("%s: %s", h.getName(), value));
             }
         }
         if (showBody) {
@@ -259,6 +279,22 @@ public class LoggingHttpActivityListener extends 
ServiceSupport implements Camel
         return new CamelLogger(name, level);
     }
 
+    /**
+     * Whether the value of the HTTP header with the given name is a 
credential (such as Authorization, Cookie or an API
+     * key), which is never logged
+     */
+    protected boolean isSensitiveHeader(String name) {
+        return name != null
+                && 
(SENSITIVE_HEADERS.contains(name.toLowerCase(Locale.ENGLISH)) || 
SensitiveUtils.containsSensitive(name));
+    }
+
+    private String getMaskString() {
+        if (maskingFormatter instanceof DefaultMaskingFormatter dmf && 
dmf.getMaskString() != null) {
+            return dmf.getMaskString();
+        }
+        return DEFAULT_MASK;
+    }
+
     private String getValue(boolean sensitive, Object value) {
         String v = value != null ? value.toString() : null;
         if (v != null && (sensitive || logMask != null && logMask)) {
diff --git 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpLoggingActivityMaskHeadersTest.java
 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpLoggingActivityMaskHeadersTest.java
new file mode 100644
index 000000000000..cd6b7b0e861c
--- /dev/null
+++ 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpLoggingActivityMaskHeadersTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.http;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.CamelLogger;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+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.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The values of the HTTP headers that carry a credential are masked in the 
activity log, also without logMask.
+ */
+public class HttpLoggingActivityMaskHeadersTest extends BaseHttpTest {
+
+    @BindToRegistry("listener")
+    private final CapturingListener listener = new CapturingListener();
+
+    private HttpServer localServer;
+
+    @Override
+    public void setupResources() throws Exception {
+        localServer = ServerBootstrap.bootstrap()
+                
.setCanonicalHostName("localhost").setHttpProcessor(getBasicHttpProcessor())
+                
.setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+                .setSslContext(getSSLContext())
+                .register("/secure", (request, response, context) -> {
+                    response.setCode(HttpStatus.SC_OK);
+                    response.addHeader("Set-Cookie", 
"SESSIONID=response-session-secret; Path=/");
+                    response.addHeader("X-Trace", "trace-123");
+                    response.setEntity(new StringEntity(getExpectedContent(), 
ContentType.TEXT_PLAIN));
+                })
+                .create();
+        localServer.start();
+    }
+
+    @Override
+    public void cleanupResources() throws Exception {
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+
+    @BeforeEach
+    void clearLog() {
+        listener.logged.clear();
+    }
+
+    @Test
+    public void credentialHeadersAreMasked() {
+        Exchange exchange = template.request(
+                "http://localhost:"; + localServer.getLocalPort()
+                                             + 
"/secure?logHttpActivity=true&httpActivityListener=#listener",
+                e -> {
+                    e.getIn().setHeader("Authorization", "Bearer 
secret-token");
+                    e.getIn().setHeader("Proxy-Authorization", "Basic 
proxy-secret");
+                    e.getIn().setHeader("Cookie", 
"SESSIONID=request-session-secret");
+                    e.getIn().setHeader("X-Api-Key", "api-key-secret");
+                    e.getIn().setHeader("X-Trace", "trace-123");
+                });
+
+        assertNull(exchange.getException());
+        assertEquals(getExpectedContent(), 
exchange.getMessage().getBody(String.class));
+
+        String log = String.join(System.lineSeparator(), listener.logged);
+        List<String> lines = List.of(log.split("\\R"));
+        // the request and the response are both logged
+        assertTrue(log.contains("Sending HTTP Request"), log);
+        assertTrue(log.contains("Received HTTP Response"), log);
+
+        // the header names are kept, the credentials are masked
+        assertTrue(lines.contains("Authorization: xxxxx"), log);
+        assertTrue(lines.contains("Proxy-Authorization: xxxxx"), log);
+        assertTrue(lines.contains("Cookie: xxxxx"), log);
+        assertTrue(lines.contains("X-Api-Key: xxxxx"), log);
+        assertTrue(lines.contains("Set-Cookie: xxxxx"), log);
+        assertFalse(log.contains("secret-token"), log);
+        assertFalse(log.contains("proxy-secret"), log);
+        assertFalse(log.contains("request-session-secret"), log);
+        assertFalse(log.contains("api-key-secret"), log);
+        assertFalse(log.contains("response-session-secret"), log);
+
+        // other headers are logged as is
+        assertTrue(lines.contains("X-Trace: trace-123"), log);
+    }
+
+    /**
+     * Captures what the listener logs, instead of writing it to the logger.
+     */
+    private static final class CapturingListener extends 
LoggingHttpActivityListener {
+
+        private final List<String> logged = new CopyOnWriteArrayList<>();
+
+        @Override
+        protected void onActivity(
+                CamelLogger logger, Exchange exchange, HttpHost host, 
HttpRequest request, HttpResponse response,
+                HttpEntity entity, long elapsed) {
+            CamelLogger capturing = new CamelLogger(logger.getLog(), 
logger.getLevel()) {
+                @Override
+                public void log(String message) {
+                    logged.add(message);
+                }
+            };
+            super.onActivity(capturing, exchange, host, request, response, 
entity, elapsed);
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 38b8b68c3feb..986c790c48a2 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -1597,6 +1597,14 @@ is now sent with the first request; before, the request 
failed with `IllegalStat
 when `authDomain` is not set. Before, the endpoint answered only Basic and 
Digest challenges with these credentials,
 and the component did not use them. If the server asks for Basic credentials, 
remove `authMethod=NTLM`.
 
+=== camel-http - logHttpActivity masks the values of sensitive headers
+
+With `logHttpActivity=true`, the `LoggingHttpActivityListener` now always 
masks the value of a request or response
+header that carries a credential, such as `Authorization`, 
`Proxy-Authorization`, `Cookie`, `Set-Cookie` and
+`X-Api-Key`, and logs for example `Authorization: xxxxx`. Before, these values 
were logged in clear text, even
+with `logMask=true`, as the masking formatter only masks `key=value`, XML and 
JSON shapes. The other headers and the
+body are masked as before, only when `logMask=true`.
+
 === camel-platform-http-main, camel-netty-http, camel-undertow - the 
Authorization header is removed after the login is checked
 
 When Camel checks the credential of an incoming request, the `Authorization` 
header of the request is now removed from

Reply via email to