-
Notifications
You must be signed in to change notification settings - Fork 948
Easily mask sensitive headers from request logs by adding HeadersSanitizer
#5188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3c163cb
Add HeadersSanitizer to allow users to easily mask headers
seonWKim a52c45e
Nit
seonWKim 0904f65
Apply comments
seonWKim b3dfd7d
Fix HashMap to LinkedHashMap to maintain order of headers
seonWKim b4cb62b
Fix style issues
minwoox c5b8961
Address the comment from @ikhoon
minwoox d7dbffc
BiFunction was replaced with HeadersSanitizer
ikhoon d2bd0a7
Update the default sensitive headers
ikhoon cecbc3b
add reference
ikhoon c1161fc
final.final.final
ikhoon 042141a
Javadoc for the default sensitive headers
ikhoon b7697b5
Introduce `HeaderMaskingFunction`
ikhoon ad0ab4f
Rename maskingHeaders to sensitiveHeaders
ikhoon 6690425
Fix test failures
ikhoon c15e7bf
Remove public
ikhoon a837197
Update core/src/main/java/com/linecorp/armeria/common/logging/Abstrac…
ikhoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
core/src/main/java/com/linecorp/armeria/common/logging/AbstractHeadersSanitizerBuilder.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.common.logging; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
import com.linecorp.armeria.common.HttpHeaderNames; | ||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
import io.netty.util.AsciiString; | ||
|
||
/** | ||
* A skeletal builder implementation for {@link HeadersSanitizer}. | ||
*/ | ||
abstract class AbstractHeadersSanitizerBuilder<T> { | ||
|
||
// Referenced from: | ||
// - https://docs.rs/tower-http/latest/tower_http/sensitive_headers/index.html | ||
// - https://techdocs.akamai.com/edge-diagnostics/reference/sensitive-headers | ||
// - https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html#_cookies_and_sensitive_headers | ||
private static final Set<AsciiString> DEFAULT_SENSITIVE_HEADERS = | ||
ImmutableSet.of(HttpHeaderNames.AUTHORIZATION, HttpHeaderNames.COOKIE, | ||
HttpHeaderNames.SET_COOKIE, HttpHeaderNames.PROXY_AUTHORIZATION); | ||
|
||
@Nullable | ||
private Set<AsciiString> sensitiveHeaders; | ||
|
||
private HeaderMaskingFunction maskingFunction = HeaderMaskingFunction.of(); | ||
|
||
/** | ||
* Adds the headers to mask before logging. | ||
*/ | ||
public AbstractHeadersSanitizerBuilder<T> sensitiveHeaders(CharSequence... headers) { | ||
requireNonNull(headers, "headers"); | ||
return sensitiveHeaders(ImmutableSet.copyOf(headers)); | ||
} | ||
|
||
/** | ||
* Sets the headers to mask before logging. | ||
*/ | ||
public AbstractHeadersSanitizerBuilder<T> sensitiveHeaders(Iterable<? extends CharSequence> headers) { | ||
requireNonNull(headers, "headers"); | ||
if (sensitiveHeaders == null) { | ||
sensitiveHeaders = new HashSet<>(); | ||
} | ||
headers.forEach(header -> sensitiveHeaders.add(AsciiString.of(header).toLowerCase())); | ||
return this; | ||
} | ||
|
||
final Set<AsciiString> sensitiveHeaders() { | ||
if (sensitiveHeaders != null) { | ||
return ImmutableSet.copyOf(sensitiveHeaders); | ||
} | ||
return DEFAULT_SENSITIVE_HEADERS; | ||
} | ||
|
||
/** | ||
* Sets the {@link Function} to use to maskFunction headers before logging. | ||
* The default maskingFunction is {@link HeaderMaskingFunction#of()} | ||
* | ||
* <pre>{@code | ||
* builder.maskingFunction((name, value) -> { | ||
* if (name.equals(HttpHeaderNames.AUTHORIZATION)) { | ||
* return "****"; | ||
* } else if (name.equals(HttpHeaderNames.COOKIE)) { | ||
* return name.substring(0, 4) + "****"; | ||
* } else { | ||
* return value; | ||
* } | ||
* } | ||
* }</pre> | ||
*/ | ||
public AbstractHeadersSanitizerBuilder<T> maskingFunction(HeaderMaskingFunction maskingFunction) { | ||
this.maskingFunction = requireNonNull(maskingFunction, "maskingFunction"); | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the {@link Function} to use to mask headers before logging. | ||
*/ | ||
final HeaderMaskingFunction maskingFunction() { | ||
return maskingFunction; | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
core/src/main/java/com/linecorp/armeria/common/logging/HeaderMaskingFunction.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.common.logging; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
import io.netty.util.AsciiString; | ||
|
||
/** | ||
* A function that masks the specified header value. | ||
*/ | ||
@FunctionalInterface | ||
public interface HeaderMaskingFunction { | ||
|
||
/** | ||
* Returns the default {@link HeaderMaskingFunction} that masks the given value with {@code ****}. | ||
*/ | ||
static HeaderMaskingFunction of() { | ||
return (name, value) -> "****"; | ||
} | ||
|
||
/** | ||
* Masks the specified {@code value} of the specified {@code name}. | ||
* If {@code null} is returned, the specified {@code value} will be removed from the log. | ||
*/ | ||
@Nullable | ||
String mask(AsciiString name, String value); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.