Skip to content

Commit 36334ed

Browse files
committed
feat: add SecurityException static consructors
1 parent cdaae63 commit 36334ed

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

system/Filters/InvalidChars.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use CodeIgniter\HTTP\RequestInterface;
1515
use CodeIgniter\HTTP\ResponseInterface;
16-
use RuntimeException;
16+
use CodeIgniter\Security\Exceptions\SecurityException;
1717

1818
/**
1919
* InvalidChars filter.
@@ -89,9 +89,7 @@ protected function checkEncoding($value)
8989
return $value;
9090
}
9191

92-
throw new RuntimeException(
93-
'Invalid UTF-8 characters in ' . $this->source . ': ' . $value
94-
);
92+
throw SecurityException::forInvalidUTF8Chars($this->source, $value);
9593
}
9694

9795
/**
@@ -113,8 +111,6 @@ protected function checkControl($value)
113111
return $value;
114112
}
115113

116-
throw new RuntimeException(
117-
'Invalid Control characters in ' . $this->source . ': ' . $value
118-
);
114+
throw SecurityException::forInvalidControlChars($this->source, $value);
119115
}
120116
}

system/Security/Exceptions/SecurityException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ public static function forDisallowedAction()
2020
return new static(lang('Security.disallowedAction'), 403);
2121
}
2222

23+
public static function forInvalidUTF8Chars(string $source, string $string)
24+
{
25+
return new static(
26+
'Invalid UTF-8 characters in ' . $source . ': ' . $string,
27+
400
28+
);
29+
}
30+
31+
public static function forInvalidControlChars(string $source, string $string)
32+
{
33+
return new static(
34+
'Invalid Control characters in ' . $source . ': ' . $string,
35+
400
36+
);
37+
}
38+
2339
/**
2440
* @deprecated Use `CookieException::forInvalidSameSite()` instead.
2541
*

tests/system/Filters/InvalidCharsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use CodeIgniter\HTTP\IncomingRequest;
1616
use CodeIgniter\HTTP\URI;
1717
use CodeIgniter\HTTP\UserAgent;
18+
use CodeIgniter\Security\Exceptions\SecurityException;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use CodeIgniter\Test\Mock\MockAppConfig;
20-
use RuntimeException;
2121

2222
/**
2323
* @internal
@@ -92,7 +92,7 @@ public function testBeforeValidString()
9292

9393
public function testBeforeInvalidUTF8StringCausesException()
9494
{
95-
$this->expectException(RuntimeException::class);
95+
$this->expectException(SecurityException::class);
9696
$this->expectExceptionMessage('Invalid UTF-8 characters in post:');
9797

9898
$sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS');
@@ -106,7 +106,7 @@ public function testBeforeInvalidUTF8StringCausesException()
106106

107107
public function testBeforeInvalidControllCharCausesException()
108108
{
109-
$this->expectException(RuntimeException::class);
109+
$this->expectException(SecurityException::class);
110110
$this->expectExceptionMessage('Invalid Control characters in cookie:');
111111

112112
$stringWithNullChar = "String contains null char and line break.\0\n";
@@ -147,7 +147,7 @@ public function stringWithLineBreakAndTabProvider()
147147
*/
148148
public function testCheckControlStringWithControlCharsCausesException($input)
149149
{
150-
$this->expectException(RuntimeException::class);
150+
$this->expectException(SecurityException::class);
151151
$this->expectExceptionMessage('Invalid Control characters in get:');
152152

153153
$_GET['val'] = $input;

0 commit comments

Comments
 (0)