Skip to content

Commit

Permalink
NIFI-14103 Corrected thread safety for Proxied Entity Encoder (#9591)
Browse files Browse the repository at this point in the history
- Created new CharsetEncoder for each method invocation
  • Loading branch information
exceptionfactory authored Dec 20, 2024
1 parent 8ab4ae0 commit 5c3499a
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.nifi.security.proxied.entity;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand All @@ -34,7 +35,7 @@ public class StandardProxiedEntityEncoder implements ProxiedEntityEncoder {

private static final String ESCAPED_LT = "\\\\<";

private static final CharsetEncoder headerValueCharsetEncoder = StandardCharsets.US_ASCII.newEncoder();
private static final Charset headerValueCharset = StandardCharsets.US_ASCII;

private static final Base64.Encoder headerValueEncoder = Base64.getEncoder();

Expand Down Expand Up @@ -73,7 +74,9 @@ private String getSanitizedIdentity(final String identity) {
} else {
final String escaped = identity.replaceAll(LT, ESCAPED_LT).replaceAll(GT, ESCAPED_GT);

if (headerValueCharsetEncoder.canEncode(escaped)) {
// Create method-local CharsetEncoder for thread-safe state handling
final CharsetEncoder charsetEncoder = headerValueCharset.newEncoder();
if (charsetEncoder.canEncode(escaped)) {
// Strings limited to US-ASCII characters can be transmitted as HTTP header values without encoding
sanitized = escaped;
} else {
Expand Down

0 comments on commit 5c3499a

Please sign in to comment.