Skip to content

Commit 346f6d4

Browse files
authored
Fix base64 transforms (#35693)
* Fix base64 transforms * changelog * Fix flags and tests * Improve documentation strings
1 parent cdfe455 commit 346f6d4

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fixed base64 transforms to better conform with the modsecurity runtime

projects/packages/waf/src/class-waf-transforms.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
* Waf_Transforms class
1212
*/
1313
class Waf_Transforms {
14+
15+
/**
16+
* Decode a Base64-encoded string. This runs the decode without strict mode, to match Modsecurity's 'base64DecodeExt' transform function.
17+
*
18+
* @param string $value value to be decoded.
19+
* @return string
20+
*/
21+
public function base64_decode_ext( $value ) {
22+
return base64_decode( $value );
23+
}
24+
1425
/**
1526
* Characters to match when trimming a string.
1627
* Emulates `std::isspace` used by ModSecurity.
@@ -20,13 +31,13 @@ class Waf_Transforms {
2031
const TRIM_CHARS = " \n\r\t\v\f";
2132

2233
/**
23-
* Decode a Base64-encoded string.
34+
* Decode a Base64-encoded string. This runs the decode with strict mode, to match Modsecurity's 'base64Decode' transform function.
2435
*
2536
* @param string $value value to be decoded.
2637
* @return string
2738
*/
2839
public function base64_decode( $value ) {
29-
return base64_decode( $value );
40+
return base64_decode( $value, true );
3041
}
3142

3243
/**

projects/packages/waf/tests/php/unit/test-waf-transforms.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ public function transformDataProvider() {
5151
yield array(
5252
'base64_decode',
5353
array(
54-
'' => '',
55-
'VGVzdENhc2U=' => 'TestCase',
56-
'VGVzdENhc2Ux' => 'TestCase1',
57-
'VGVzdENhc2UxMg==' => 'TestCase12',
54+
'' => '',
55+
'VGVzdENhc2U=' => 'TestCase',
56+
'VGVzdENhc2Ux' => 'TestCase1',
57+
'VGVzdENhc))((((2UxMg==' => false,
58+
),
59+
);
60+
61+
yield array(
62+
'base64_decode_ext',
63+
array(
64+
'' => '',
65+
'VGVzdENhc2U=' => 'TestCase',
66+
'VGVzdENhc2Ux' => 'TestCase1',
67+
'VGVzdENhc))((((2UxMg==' => 'TestCase12',
5868
),
5969
);
6070

0 commit comments

Comments
 (0)