Skip to content

Commit 7f67d77

Browse files
committed
Add ServiceNameFormatter: canonical mdui:DisplayName sanitization
Previously existed as three independently-drifting copies (Stepup-Gateway, Stepup-gssp-bundle, Stepup-Webauthn), one of which truncated to 41 chars instead of 40. Both Gateway and gssp-bundle already depend on this bundle, so this is the natural shared home per the RFC (OpenConext/Stepup-Gateway#587).
1 parent 02251e3 commit 7f67d77

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2026 SURFnet bv
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Surfnet\SamlBundle\SAML2\Extensions;
20+
21+
/**
22+
* Sanitizes an mdui:DisplayName value (and its xml:lang attribute) per the
23+
* "Showing Service name during authentication" RFC (OpenConext/Stepup-Gateway#587):
24+
* the name is forwarded into SMS/push notifications with much tighter limits than
25+
* SAML 2.0's own string bounds, so it's truncated, whitespace-normalized, and
26+
* stripped of control/format characters before being used anywhere.
27+
*
28+
* This is the single, canonical implementation: it previously existed as separate,
29+
* independently-drifting copies in Stepup-Gateway, Stepup-gssp-bundle, and Stepup-Webauthn
30+
* (one of which had an off-by-one truncation bug the others didn't).
31+
*/
32+
final class ServiceNameFormatter
33+
{
34+
private const MAX_CHARACTERS = 40;
35+
private const TRUNCATION_SUFFIX = "\u{2026}";
36+
37+
public static function format(string $raw): string
38+
{
39+
$value = preg_replace('/[\p{Cc}\p{Cf}]/u', '', $raw) ?? $raw;
40+
$value = preg_replace('/\s+/u', ' ', $value) ?? $value;
41+
$value = trim($value);
42+
43+
if (mb_strlen($value) > self::MAX_CHARACTERS) {
44+
$value = mb_substr($value, 0, self::MAX_CHARACTERS - 1) . self::TRUNCATION_SUFFIX;
45+
}
46+
47+
return $value;
48+
}
49+
50+
// xml:lang has no length/whitespace rules to apply, but still needs control/format
51+
// characters stripped before it lands in an outgoing attribute.
52+
public static function sanitizeLang(string $lang): string
53+
{
54+
return preg_replace('/[\p{Cc}\p{Cf}]/u', '', $lang) ?? $lang;
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* Copyright 2026 SURFnet bv
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Surfnet\SamlBundle\Tests\Component\Extensions;
20+
21+
use PHPUnit\Framework\TestCase;
22+
use Surfnet\SamlBundle\SAML2\Extensions\ServiceNameFormatter;
23+
24+
class ServiceNameFormatterTest extends TestCase
25+
{
26+
public function test_it_leaves_a_short_value_unchanged(): void
27+
{
28+
$this->assertSame('My Service', ServiceNameFormatter::format('My Service'));
29+
}
30+
31+
public function test_it_truncates_to_39_characters_and_appends_an_ellipsis(): void
32+
{
33+
$value = str_repeat('A', 50);
34+
35+
$result = ServiceNameFormatter::format($value);
36+
37+
$this->assertSame(str_repeat('A', 39) . "\u{2026}", $result);
38+
$this->assertSame(40, mb_strlen($result));
39+
}
40+
41+
public function test_it_trims_and_collapses_whitespace(): void
42+
{
43+
$this->assertSame('My Service', ServiceNameFormatter::format(" My \tService\n "));
44+
}
45+
46+
public function test_it_strips_control_and_format_characters(): void
47+
{
48+
$this->assertSame('My Service', ServiceNameFormatter::format("My\x01 Service"));
49+
}
50+
51+
public function test_sanitize_lang_strips_control_and_format_characters(): void
52+
{
53+
$this->assertSame('en', ServiceNameFormatter::sanitizeLang("e\x01n"));
54+
}
55+
56+
public function test_sanitize_lang_leaves_a_clean_value_unchanged(): void
57+
{
58+
$this->assertSame('en-GB', ServiceNameFormatter::sanitizeLang('en-GB'));
59+
}
60+
}

0 commit comments

Comments
 (0)