Michael Weiss created CAMEL-27429:
-------------------------------------
Summary: camel-http: allow configuring the masking regex for
`logHttpActivity` (Authorization header is logged in clear text)
Key: CAMEL-27429
URL: https://issues.apache.org/jira/browse/CAMEL-27429
Project: Camel
Issue Type: Improvement
Components: camel-http
Affects Versions: 4.23.0
Reporter: Michael Weiss
h2. Summary
{{logHttpActivity}} (introduced in Camel 4.10 via CAMEL-21468) logs the raw
HTTP request/response, including all headers. With {{logMask=true}} the values
are passed through the {{MaskingFormatter}}, but the default
{{DefaultMaskingFormatter}} only understands {{key=value}}, XML and JSON
shapes. HTTP header lines use the {{Name: value}} shape, so *{{Authorization:
Bearer ...}} is written to the log in clear text*.
Today the only way to mask it is to write and register a custom
{{MaskingFormatter}} Java class ({{CamelCustomLogMask}} bean) and reference it
from every route. That is a lot of ceremony for a one-line regex, and it is
easy to forget - which means credentials end up in log files.
*Request:* expose the masking regex as a plain configuration parameter on the
HTTP component / activity listener, so no Java code is required.
h2. Component / version
* Component: {{camel-http}} (also relevant: {{camel-http-common}},
{{camel-support}})
* Version: 4.10.0 and later (verified against {{main}})
* Affected classes:
** {{org.apache.camel.component.http.LoggingHttpActivityListener}}
** {{org.apache.camel.support.processor.DefaultMaskingFormatter}}
** {{org.apache.camel.util.SensitiveUtils}}
h2. Current behaviour
{{LoggingHttpActivityListener#onActivity}} writes each header as:
{code:java}
lines.add(String.format("%s: %s", h.getName(), getValue(h.isSensitive(),
h.getValue())));
{code}
{{getValue}} only masks when the header is flagged sensitive by Apache
HttpClient ({{Header#isSensitive()}}, which is almost never set) or when
{{logMask=true}}:
{code:java}
private String getValue(boolean sensitive, Object value) {
String v = value != null ? value.toString() : null;
if (v != null && (sensitive || logMask != null && logMask)) {
v = maskingFormatter.format(v);
}
return v;
}
{code}
{{DefaultMaskingFormatter}} then builds its patterns as:
{code:java}
regex.insert(0, "([\\w]*(?:");
regex.append(")[\\w]*[\\s]*?=[\\s]*?['\"]?)([^'\",]+)");
{code}
i.e. it requires an {{=}} between key and value. A header value such as
{{Bearer eyJhbGciOiJ...}} contains no {{=}}, so nothing is replaced.
Note that {{authorization}} *is* already present in
{{SensitiveUtils#getSensitiveKeys()}}, but that list is only used for the
{{key=value}} / XML / JSON patterns - it is never applied to HTTP header names.
Resulting log output (real-world example):
{noformat}
Sending HTTP Request (host: httpbin.org:80 route: httpCallWithMaskedLogging
exchangeId: ...)
POST /post HTTP/1.1
Authorization: Bearer eyJhbGciOiJ...super-secret-token...
Content-Type: application/json
{noformat}
h2. Proposed solution
Add a regex-based masking option to the HTTP activity logging, configurable
without writing Java code.
h3. 1. New option on LoggingHttpActivityListener
||Option||Type||Default||Description||
|{{logMaskPattern}}|{{String}}|{{null}} (opt-in)|Regular expression applied to
header values and the body before logging. Matching parts are replaced with the
mask string ({{xxxxx}}).|
Semantics:
* Applied to every header value and to the logged body when {{logMask=true}}
(or independently of {{logMask}}, if that is preferred - see open questions).
* Uses {{Pattern.compile(logMaskPattern)}}; the pattern may carry its own flags
(e.g. {{(?i)}}), so no extra flag option is needed.
* Replacement uses the existing {{MaskingFormatter}} mask string ({{xxxxx}})
and supports capture-group references, so {{$1 xxxxx}} keeps the auth scheme.
* Runs *before* the existing {{MaskingFormatter}} so both mechanisms compose.
h3. 2. Expose it as a component/endpoint parameter
So it can be set once for all HTTP calls, or per endpoint:
{code}
# global, all http endpoints
camel.component.http.logHttpActivity = true
camel.component.http.logMask = true
camel.component.http.logMaskPattern =
(?i)^(Bearer|Basic|Digest|Token|ApiKey|AWS|SSWS)\s+\S+.*$
{code}
{code:yaml}
- route:
id: httpCallWithMaskedLogging
from:
uri: "timer:trigger"
parameters:
period: "5000"
repeatCount: 1
steps:
- setHeader:
name: Authorization
constant: "Bearer eyJhbGciOiJ...super-secret-token..."
- to:
uri: "http://httpbin.org/post"
parameters:
httpMethod: POST
logHttpActivity: true
logMask: true
logMaskPattern:
"(?i)^(Bearer|Basic|Digest|Token|ApiKey|AWS|SSWS)\\s+\\S+.*$"
{code}
Expected output:
{noformat}
Sending HTTP Request (host: httpbin.org:80 route: httpCallWithMaskedLogging
exchangeId: ...)
POST /post HTTP/1.1
Authorization: Bearer xxxxx
Content-Type: application/json
{noformat}
h3. 3. (Complementary) mask by header name
Independently of the regex, {{LoggingHttpActivityListener}} could mask the
value of any header whose name is a known sensitive key:
{code:java}
boolean sensitive = h.isSensitive() ||
SensitiveUtils.containsSensitive(h.getName());
{code}
{{SensitiveUtils.containsSensitive("Authorization")}} already returns {{true}},
so {{Authorization}}, {{Proxy-Authorization}}, {{Cookie}}-style headers would
be masked out of the box. This is a safe default and would have prevented the
leak without any configuration. It should be considered in addition to (not
instead of) the configurable regex, because custom auth schemes and vendor
headers still need a pattern.
h2. Acceptance criteria
* {{logMaskPattern}} is available as a component option
({{camel.component.http.logMaskPattern}}) and as an endpoint option.
* {{logMaskPattern}} is available on {{LoggingHttpActivityListener}} and can be
set via {{camel.component.http.httpActivityListener}} bean properties.
* When set, matching header values and body content are replaced with the mask
string in the activity log; non-matching content is unchanged.
* The pattern is applied to request *and* response headers/bodies.
* Invalid regex fails fast at startup with a clear error message.
* Existing behaviour is unchanged when {{logMaskPattern}} is not set.
* Documentation for {{logHttpActivity}} / {{LoggingHttpActivityListener}} is
updated with an {{Authorization}} masking example.
* Unit test covering {{Authorization: Bearer ...}} masking, plus a test that a
non-matching header is left untouched.
h2. Alternatives considered
# *Custom MaskingFormatter bean (current workaround).* Works, but requires a
Java class per project and a bean reference in every route/listener:
{code:java}
public class AuthHeaderMaskingFormatter implements MaskingFormatter {
private static final Pattern AUTH_PATTERN =
Pattern.compile("(?i)^(Bearer|Basic|Digest|Token|ApiKey|AWS|SSWS)\\s+\\S+.*$");
private final DefaultMaskingFormatter delegate = new
DefaultMaskingFormatter();
@Override
public String format(String source) {
if (source == null) {
return null;
}
return delegate.format(AUTH_PATTERN.matcher(source).replaceAll("$1
xxxxx"));
}
}
{code}
This is exactly the boilerplate the feature request aims to remove.
# *Extend DefaultMaskingFormatter with a Name: value pattern.* Would fix the
default, but still gives no way to express custom auth schemes without code.
# *camel.main.additionalSensitiveKeywords.* Only adds keywords to the
{{key=value}} patterns; does not help for {{Name: value}} header lines and
cannot express a regex.
h2. Open questions
* Should {{logMaskPattern}} imply {{logMask=true}}, or require both to be set
explicitly? (Explicit is safer/less surprising, but more verbose.)
* Should the pattern be applied to the whole rendered line ({{Authorization:
Bearer ...}}) or only to the value? Applying it to the value keeps the user
regex simple and avoids matching header names.
* Should there be a built-in default pattern for well-known auth schemes, or
should the default stay {{null}} to avoid changing existing log output?
h2. References
* CAMEL-21468 - _camel-http - Add option to log raw request and raw response_
(introduced {{logHttpActivity}} in 4.10.0)
* CAMEL-23936 - gzip {{Content-Encoding}} lost when {{logHttpActivity}}
rebuilds the entity
* {{LoggingHttpActivityListener}} -
{{components/camel-http/src/main/java/org/apache/camel/component/http/LoggingHttpActivityListener.java}}
* {{DefaultMaskingFormatter}} -
{{core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultMaskingFormatter.java}}
* {{SensitiveUtils}} -
{{core/camel-util/src/main/java/org/apache/camel/util/SensitiveUtils.java}}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)