Skip to content

Commit a9ece43

Browse files
Merge branch '5.8.x'
Closes gh-12223
2 parents 177e11f + 9b6be3a commit a9ece43

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private void writeMetadataToResponse(HttpServletResponse response, String regist
8989
String format = "attachment; filename=\"%s\"; filename*=UTF-8''%s";
9090
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format(format, fileName, encodedFileName));
9191
response.setContentLength(metadata.length());
92+
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
9293
response.getWriter().write(metadata);
9394
}
9495

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.
@@ -152,6 +152,21 @@ public void doFilterWhenPathStartsWithRegistrationIdThenServesMetadata() throws
152152
verify(this.repository).findByRegistrationId("registration-id");
153153
}
154154

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

0 commit comments

Comments
 (0)