Skip to content

Commit 2b9f032

Browse files
authored
Fix non-ASCII characters in headers being rejected (Stirling-Tools#5377) (Stirling-Tools#5699)
1 parent 214dc20 commit 2b9f032

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

app/proprietary/src/main/java/stirling/software/proprietary/security/configuration/SecurityConfiguration.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stirling.software.proprietary.security.configuration;
22

33
import java.util.List;
4+
import java.util.regex.Pattern;
45

56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.beans.factory.annotation.Qualifier;
@@ -28,6 +29,8 @@
2829
import org.springframework.security.web.SecurityFilterChain;
2930
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3031
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
32+
import org.springframework.security.web.firewall.HttpFirewall;
33+
import org.springframework.security.web.firewall.StrictHttpFirewall;
3134
import org.springframework.security.web.savedrequest.NullRequestCache;
3235
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3336
import org.springframework.web.cors.CorsConfiguration;
@@ -136,6 +139,34 @@ public static PasswordEncoder passwordEncoder() {
136139
return new BCryptPasswordEncoder();
137140
}
138141

142+
/**
143+
* Configures HttpFirewall to allow non-ASCII characters in header values. This fixes issues
144+
* with reverse proxies (like Authelia) that may set headers with non-ASCII characters (e.g.,
145+
* "Remote-User: Dvořák").
146+
*
147+
* <p>By default, StrictHttpFirewall rejects header values containing non-ASCII characters. This
148+
* configuration allows valid UTF-8 encoded characters while maintaining security.
149+
*
150+
* @return Configured HttpFirewall that allows non-ASCII characters in headers
151+
*/
152+
@Bean
153+
public HttpFirewall httpFirewall() {
154+
StrictHttpFirewall firewall = new StrictHttpFirewall();
155+
// Allow non-ASCII characters but continue to reject control characters such as newlines.
156+
// Pattern adapted from Spring Security's StrictHttpFirewall documentation.
157+
Pattern allowedChars = Pattern.compile("[\\p{IsAssigned}&&[^\\p{IsControl}]]*");
158+
159+
firewall.setAllowedHeaderValues(
160+
headerValue ->
161+
headerValue != null && allowedChars.matcher(headerValue).matches());
162+
163+
// Apply the same rules to parameter values for consistency.
164+
firewall.setAllowedParameterValues(
165+
parameterValue ->
166+
parameterValue != null && allowedChars.matcher(parameterValue).matches());
167+
return firewall;
168+
}
169+
139170
@Bean
140171
public CorsConfigurationSource corsConfigurationSource() {
141172
List<String> configuredOrigins = null;

0 commit comments

Comments
 (0)