Skip to content

Commit 8441e75

Browse files
Merge branch '5.6.x' into 5.7.x
Closes gh-12221
2 parents 79483b2 + 53148dc commit 8441e75

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,6 +105,7 @@ private void writeMetadataToResponse(HttpServletResponse response, String regist
105105
String format = "attachment; filename=\"%s\"; filename*=UTF-8''%s";
106106
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format(format, fileName, encodedFileName));
107107
response.setContentLength(metadata.length());
108+
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
108109
response.getWriter().write(metadata);
109110
}
110111

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilterTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,6 +153,21 @@ public void doFilterWhenPathStartsWithRegistrationIdThenServesMetadata() throws
153153
verify(this.repository).findByRegistrationId("registration-id");
154154
}
155155

156+
// gh-12026
157+
@Test
158+
public void doFilterWhenCharacterEncodingThenEncodeSpecialCharactersCorrectly() throws Exception {
159+
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.full().build();
160+
String testMetadataFilename = "test-{registrationId}-metadata.xml";
161+
String generatedMetadata = "<xml>testäöü</xml>";
162+
this.request.setPathInfo("/saml2/service-provider-metadata/registration-id");
163+
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata);
164+
this.filter = new Saml2MetadataFilter((req, id) -> validRegistration, this.resolver);
165+
this.filter.setMetadataFilename(testMetadataFilename);
166+
this.filter.doFilter(this.request, this.response, this.chain);
167+
assertThat(this.response.getCharacterEncoding()).isEqualTo(StandardCharsets.UTF_8.name());
168+
assertThat(new String(this.response.getContentAsByteArray())).isEqualTo(generatedMetadata);
169+
}
170+
156171
@Test
157172
public void setRequestMatcherWhenNullThenIllegalArgument() {
158173
assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRequestMatcher(null));

0 commit comments

Comments
 (0)