Skip to content

Commit 476bf41

Browse files
authored
Merge pull request #200 from BeAPI/fix/output-escaping-hardening
fix(security): harden output escaping (Svg, Link, ButtonSeoClick)
2 parents d35f8a2 + e8e0602 commit 476bf41

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

‎inc/Helpers/Formatting/Escape.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
* @return mixed Return value escape
1111
*/
1212
function escape_attribute_value( string $value, string $escape ) {
13+
// Fail closed: an unknown callback must never let a raw value reach an attribute.
1314
if ( ! function_exists( $escape ) ) {
14-
return $value;
15+
return esc_attr( $value );
1516
}
1617

1718
return $escape( $value );
@@ -26,8 +27,9 @@ function escape_attribute_value( string $value, string $escape ) {
2627
* @return mixed Return value escape
2728
*/
2829
function escape_content_value( string $value, string $escape ) {
30+
// Fail closed: fall back to the post-content KSES filter for an unknown callback.
2931
if ( ! function_exists( $escape ) ) {
30-
return $value;
32+
return wp_kses_post( $value );
3133
}
3234

3335
return $escape( $value );

‎inc/Helpers/Formatting/Link.php‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ function get_the_link( array $attributes, array $settings = [] ): string {
172172
]
173173
);
174174

175+
// wp_parse_args() is shallow: merge the escape map so a partial caller map keeps the URL escaping defaults.
176+
$settings['escape'] = array_merge(
177+
[
178+
'href' => 'esc_url',
179+
'data-href' => 'esc_url',
180+
],
181+
(array) $settings['escape']
182+
);
183+
175184
$settings = apply_filters( 'bea_theme_framework_link_settings', $settings, $attributes );
176185

177186
/**************************************** MODE BUTTON ****************************************/
@@ -182,8 +191,12 @@ function get_the_link( array $attributes, array $settings = [] ): string {
182191
$attributes['type'] = 'button';
183192
$attributes['role'] = 'link';
184193
$attributes['data-href'] = $attributes['href'];
185-
$attributes['data-rel'] = $attributes['rel'];
186-
$attributes['data-target'] = $attributes['target'];
194+
$attributes['data-rel'] = $attributes['rel'] ?? '';
195+
$attributes['data-target'] = $attributes['target'] ?? '';
196+
// The renamed attribute carries a URL: keep URL escaping unless the caller set a valid one for it.
197+
if ( empty( $settings['escape']['data-href'] ) || ! function_exists( $settings['escape']['data-href'] ) ) {
198+
$settings['escape']['data-href'] = 'esc_url';
199+
}
187200
unset( $attributes['href'], $attributes['rel'], $attributes['target'] );
188201
}
189202

‎inc/Services/Svg.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public function get_the_icon( string $icon_class, array $additionnal_classes = [
6060
return sprintf(
6161
'<svg class="%s" aria-hidden="true" focusable="false"><use href="%s#%s"></use></svg>',
6262
implode( ' ', $classes ),
63-
add_query_arg( [ 'v' => $hash_sprite ], $icon_url ),
64-
$parts['icon_slug']
63+
esc_url( add_query_arg( [ 'v' => $hash_sprite ], $icon_url ) ),
64+
esc_attr( $parts['icon_slug'] )
6565
);
6666
}
6767

‎src/js/common/classes/ButtonSeoClick.js‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,35 @@ class ButtonSeoClick extends AbstractDomElement {
1717
}
1818
}
1919

20+
/**
21+
* Only allow navigation to http(s), mailto and tel URLs, or to relative ones.
22+
* Blocks javascript:, data: and other schemes.
23+
*
24+
* @param {string|null} url
25+
* @return {boolean} Whether the URL can be navigated to.
26+
*/
27+
function isSafeUrl(url) {
28+
if (!url) {
29+
return false;
30+
}
31+
32+
try {
33+
const { protocol } = new URL(url, window.location.href);
34+
return ['http:', 'https:', 'mailto:', 'tel:'].includes(protocol);
35+
} catch (error) {
36+
return false;
37+
}
38+
}
39+
2040
function onClickButton(e) {
21-
const target = e.currentTarget.getAttribute('data-target');
2241
const href = e.currentTarget.getAttribute('data-href');
2342

43+
if (!isSafeUrl(href)) {
44+
return;
45+
}
46+
47+
const target = e.currentTarget.getAttribute('data-target');
48+
2449
if (target === '_blank') {
2550
openUrlInNewTab(href);
2651
} else {

0 commit comments

Comments
 (0)