Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation custom error with asterisk field #6352

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
c0a782b
fix: add judge if validation fields have asterisk when user want to s…
ping-yee Aug 5, 2022
340c752
test: add test for validation error with asterisk field.
ping-yee Aug 5, 2022
9fc2b7a
cs: run cs-fix and static analysis
ping-yee Aug 5, 2022
4441fd1
feat: add public method to regenerate CSRF token
kenjis Jul 28, 2022
23d71d6
docs: add changelog and note
kenjis Aug 1, 2022
f1db96b
docs: the session_id acquisition method that is not described
naente Aug 6, 2022
4a6527a
fix: add a new param in processRules and getErrorMessage function.
ping-yee Aug 6, 2022
a8453e2
add: validation testing add label field
ping-yee Aug 6, 2022
3648faa
fix: cs-fixer
ping-yee Aug 6, 2022
4632602
Merge pull request from GHSA-6gch-wjxj-hc2w
MGatner Aug 6, 2022
cb71f73
Prep for 4.2.3 release
MGatner Aug 6, 2022
01dac95
Merge pull request #6349 from naente/develop
MGatner Aug 6, 2022
1be14e7
Update CHANGELOG.md
MGatner Aug 6, 2022
d77883e
Merge pull request #6353 from codeigniter4/release-4.2.3
MGatner Aug 7, 2022
9c0c695
Merge pull request #6354 from codeigniter4/develop
MGatner Aug 7, 2022
68ad806
docs: add @property to Session
kenjis Aug 7, 2022
55f3c23
docs: add Prefious and Next button
kenjis Aug 7, 2022
dc84e94
docs: add upgrade_423 in upgrading.rst
kenjis Aug 7, 2022
ab6aa3c
docs: remove space
kenjis Aug 7, 2022
a42651b
docs: add missing command prompt
kenjis Aug 7, 2022
b2aac31
docs: remove out-of-dated? description
kenjis Aug 7, 2022
d75dabd
docs: remove `--prefer-source`
kenjis Aug 7, 2022
fa50250
docs: improve description
kenjis Aug 7, 2022
d8fe98d
Merge pull request #6355 from kenjis/phpdocs-add-property-to-Session
samsonasik Aug 7, 2022
b667425
docs: remove unused directories in Structure
kenjis Aug 7, 2022
db1833a
docs: add missing tests directory
kenjis Aug 7, 2022
d0f0881
docs: remove `two`
kenjis Aug 7, 2022
23a18eb
Merge pull request #6356 from kenjis/fix-docs-upgrade_423.rst
kenjis Aug 7, 2022
776926f
chore: convert warning into error
kenjis Aug 8, 2022
b471880
docs: refactor sample code
kenjis Aug 8, 2022
09bcef4
chore: tweak options
kenjis Aug 8, 2022
416da02
fix: change the declaration of methods according to PSR-12.
ping-yee Aug 8, 2022
2da8ef8
Merge pull request #6357 from kenjis/fix-docs-installing_composer.rst
kenjis Aug 9, 2022
d4b0483
docs: fix @param type
kenjis Aug 9, 2022
6d51b2c
fix: Email class may not log an error when it fails to send
kenjis Aug 9, 2022
4511b3f
docs: improve explanation
kenjis Aug 9, 2022
cf662be
docs: add missing deprecation
kenjis Aug 9, 2022
1086703
Merge pull request #6359 from kenjis/make-sphinx-warning-to-error
kenjis Aug 9, 2022
4fdffaa
Merge pull request #6363 from kenjis/fix-docs-Forge-createTable
kenjis Aug 10, 2022
9204889
Merge pull request #6362 from kenjis/fix-email-logging
kenjis Aug 10, 2022
cb7b565
add : add a description to the changelog.
ping-yee Aug 11, 2022
7c12af8
docs: fix duplicate object description of ...
kenjis Aug 11, 2022
6d3a24b
Merge pull request #6369 from kenjis/fix-docs-sphinx-errors
kenjis Aug 11, 2022
34a1f5b
docs: update explanation for private methods
kenjis Aug 11, 2022
f9d93ce
docs: add about initController()
kenjis Aug 11, 2022
00c6596
Merge pull request #6368 from kenjis/fix-docs-controllers.rst
kenjis Aug 11, 2022
eb6e943
rebase: reslove conflicts
ping-yee Aug 12, 2022
fe010ba
fix: add judge if validation fields have asterisk when user want to s…
ping-yee Aug 5, 2022
dba4bee
test: add test for validation error with asterisk field.
ping-yee Aug 5, 2022
713d6bb
cs: run cs-fix and static analysis
ping-yee Aug 5, 2022
748bad7
fix: add a new param in processRules and getErrorMessage function.
ping-yee Aug 6, 2022
3320fdb
add: validation testing add label field
ping-yee Aug 6, 2022
c5d8cb2
fix: cs-fixer
ping-yee Aug 6, 2022
9bdf852
fix: change the declaration of methods according to PSR-12.
ping-yee Aug 8, 2022
3b9e662
Merge branch 'fix-validation-custom-error' of https://github.com/ping…
ping-yee Aug 12, 2022
fa442b4
add : add a description to the changelog in v4.2.4.
ping-yee Aug 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-userguide.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ jobs:
- uses: ammaraskar/[email protected]
with:
docs-folder: user_guide_src
build-command: 'make html SPHINXOPTS="-W --keep-going -w /tmp/sphinx-log"'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [v4.2.3](https://github.com/codeigniter4/CodeIgniter4/tree/v4.2.3) (2022-08-06)
[Full Changelog](https://github.com/codeigniter4/CodeIgniter4/compare/v4.2.2...v4.2.3)

* SECURITY: Improve CSRF protection (for Shield CSRF security fix)

## [v4.2.2](https://github.com/codeigniter4/CodeIgniter4/tree/v4.2.2) (2022-08-05)
[Full Changelog](https://github.com/codeigniter4/CodeIgniter4/compare/v4.2.1...v4.2.2)

Expand Down
6 changes: 4 additions & 2 deletions admin/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Documentation guide based on the releases of `4.0.5` and `4.1.0` on January 31, 2021.
>
> Updated for `4.1.6` on December 24, 2021.
> Updated for `4.2.3` on August 6, 2022.
>
> -MGatner

Expand All @@ -29,8 +29,9 @@ git clone [email protected]:codeigniter4/CodeIgniter4.git
git clone [email protected]:codeigniter4/userguide.git
```
* Vet the **admin/** folders for any removed hidden files (Action deploy scripts *do not remove these*)
* Merge any Security Advisory PRs in private forks

## CodeIgniter4
## Process

> Note: Most changes that need noting in the User Guide and docs should have been included
> with their PR, so this process assumes you will not be generating much new content.
Expand Down Expand Up @@ -75,6 +76,7 @@ composer create-project codeigniter4/appstarter release-test
cd release-test
composer test && composer info codeigniter4/framework
```
* publish any Security Advisories that were resolved from private forks

## User Guide

Expand Down
2 changes: 1 addition & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CodeIgniter
/**
* The current version of CodeIgniter Framework
*/
public const CI_VERSION = '4.2.2';
public const CI_VERSION = '4.2.3';

/**
* App startup time.
Expand Down
53 changes: 38 additions & 15 deletions system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ class Email
*/
protected $debugMessage = [];

/**
* Raw debug messages
*
* @var string[]
*/
private array $debugMessageRaw = [];

/**
* Recipients
*
Expand Down Expand Up @@ -434,16 +441,17 @@ public function initialize($config)
*/
public function clear($clearAttachments = false)
{
$this->subject = '';
$this->body = '';
$this->finalBody = '';
$this->headerStr = '';
$this->replyToFlag = false;
$this->recipients = [];
$this->CCArray = [];
$this->BCCArray = [];
$this->headers = [];
$this->debugMessage = [];
$this->subject = '';
$this->body = '';
$this->finalBody = '';
$this->headerStr = '';
$this->replyToFlag = false;
$this->recipients = [];
$this->CCArray = [];
$this->BCCArray = [];
$this->headers = [];
$this->debugMessage = [];
$this->debugMessageRaw = [];

$this->setHeader('Date', $this->setDate());

Expand Down Expand Up @@ -1658,7 +1666,12 @@ protected function spoolEmail()
}

if (! $success) {
$this->setErrorMessage(lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol))));
$message = lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol)));

log_message('error', 'Email: ' . $message);
log_message('error', $this->printDebuggerRaw());

$this->setErrorMessage($message);

return false;
}
Expand Down Expand Up @@ -1937,7 +1950,8 @@ protected function sendCommand($cmd, $data = '')

$reply = $this->getSMTPData();

$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
$this->debugMessageRaw[] = $cmd . ': ' . $reply;

if ($resp === null || ((int) static::substr($reply, 0, 3) !== $resp)) {
$this->setErrorMessage(lang('Email.SMTPError', [$reply]));
Expand Down Expand Up @@ -2090,8 +2104,8 @@ protected function getHostname()
}

/**
* @param array $include List of raw data chunks to include in the output
* Valid options are: 'headers', 'subject', 'body'
* @param array|string $include List of raw data chunks to include in the output
* Valid options are: 'headers', 'subject', 'body'
*
* @return string
*/
Expand Down Expand Up @@ -2119,12 +2133,21 @@ public function printDebugger($include = ['headers', 'subject', 'body'])
return $msg . ($rawData === '' ? '' : '<pre>' . $rawData . '</pre>');
}

/**
* Returns raw debug messages
*/
private function printDebuggerRaw(): string
{
return implode("\n", $this->debugMessageRaw);
}

/**
* @param string $msg
*/
protected function setErrorMessage($msg)
{
$this->debugMessage[] = $msg . '<br />';
$this->debugMessage[] = $msg . '<br />';
$this->debugMessageRaw[] = $msg;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ private function restoreHash(): void
}

/**
* Generates (Regenerate) the CSRF Hash.
* Generates (Regenerates) the CSRF Hash.
*/
protected function generateHash(): string
public function generateHash(): string
{
$this->hash = bin2hex(random_bytes(static::CSRF_HASH_BYTES));

Expand Down
2 changes: 2 additions & 0 deletions system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*
* Session configuration is done through session variables and cookie related
* variables in app/config/App.php
*
* @property string $session_id
*/
class Session implements SessionInterface
{
Expand Down
30 changes: 23 additions & 7 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
if (strpos($field, '*') !== false) {
// Process multiple fields
foreach ($values as $dotField => $value) {
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data);
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data, $field);
}
} else {
// Process single field
Expand Down Expand Up @@ -201,10 +201,17 @@ public function check($value, string $rule, array $errors = []): bool
*
* @param array|string $value
* @param array|null $rules
* @param array $data
* @param array $data The array of data to validate, with `DBGroup`.
* @param string|null $originalField The original asterisk field name like "foo.*.bar".
*/
protected function processRules(string $field, ?string $label, $value, $rules = null, ?array $data = null): bool
{
protected function processRules(
string $field,
?string $label,
$value,
$rules = null,
?array $data = null,
?string $originalField = null
): bool {
if ($data === null) {
throw new InvalidArgumentException('You must supply the parameter: data.');
}
Expand Down Expand Up @@ -333,7 +340,8 @@ protected function processRules(string $field, ?string $label, $value, $rules =
$field,
$label,
$param,
(string) $value
(string) $value,
$originalField
);

return false;
Expand Down Expand Up @@ -706,13 +714,21 @@ public function setError(string $field, string $error): ValidationInterface
*
* @param string|null $value The value that caused the validation to fail.
*/
protected function getErrorMessage(string $rule, string $field, ?string $label = null, ?string $param = null, ?string $value = null): string
{
protected function getErrorMessage(
string $rule,
string $field,
?string $label = null,
?string $param = null,
?string $value = null,
?string $originalField = null
): string {
$param ??= '';

// Check if custom message has been defined by user
if (isset($this->customErrors[$field][$rule])) {
$message = lang($this->customErrors[$field][$rule]);
} elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
$message = lang($this->customErrors[$originalField][$rule]);
} else {
// Try to grab a localized version of the message...
// lang() will return the rule name back if not found,
Expand Down
26 changes: 26 additions & 0 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,32 @@ public function testRegenerateWithFalseSecurityRegenerateProperty()
$this->assertSame($oldHash, $newHash);
}

public function testRegenerateWithFalseSecurityRegeneratePropertyManually()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';

$config = new SecurityConfig();
$config->regenerate = false;
Factories::injectMock('config', 'Security', $config);

$security = new MockSecurity(new MockAppConfig());
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);

$oldHash = $security->getHash();
$security->verify($request);
$security->generateHash();
$newHash = $security->getHash();

$this->assertNotSame($oldHash, $newHash);
}

public function testRegenerateWithTrueSecurityRegenerateProperty()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Validation/StrictRules/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,28 @@ public function testRunGroupWithCustomErrorMessage(): void
], $this->validation->getErrors());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6245
*/
public function testRunWithCustomErrorsAndAsteriskField(): void
{
$data = [
'foo' => [
['bar' => null],
['bar' => null],
],
];
$this->validation->setRules(
['foo.*.bar' => ['label' => 'foo bar', 'rules' => 'required']],
['foo.*.bar' => ['required' => 'Required']]
);
$this->validation->run($data);
$this->assertSame([
'foo.0.bar' => 'Required',
'foo.1.bar' => 'Required',
Comment on lines +379 to +380
Copy link
Member

@kenjis kenjis Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you really okay with key like 'foo.0.bar'?
It seems a bit difficult to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But as I known, validation service change the asterisk field to like 'foo.0.bar'.
As Issue #6245 show that.

], $this->validation->getErrors());
}

/**
* @dataProvider rulesSetupProvider
*
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See all the changes.
.. toctree::
:titlesonly:

v4.2.4
v4.2.3
v4.2.2
v4.2.1
Expand Down
6 changes: 1 addition & 5 deletions user_guide_src/source/changelogs/v4.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ BREAKING
- A bug that caused pages to be cached before after filters were executed when using page caching has been fixed. Adding response headers or changing the response body in after filters now caches them correctly.
- Due to a bug fix, now :php:func:`random_string` with the first parameter ``'crypto'`` throws ``InvalidArgumentException`` if the second parameter ``$len`` is an odd number.

Enhancements
************

none.

Changes
*******

Expand All @@ -37,6 +32,7 @@ Deprecations

- The parameters of ``Services::request()`` are deprecated.
- The first parameter ``$cacheConfig`` of ``CodeIgniter::gatherOutput()`` is deprecated.
- The second parameter ``$ifNotExists`` of ``Forge::_createTable()`` is deprecated.

Bugs Fixed
**********
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/changelogs/v4.2.3.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Version 4.2.3
#############

Release Date: Unreleased
Release Date: August 6, 2022

**4.2.3 release of CodeIgniter4**

Expand All @@ -17,7 +17,7 @@ none.
Enhancements
************

none.
- Now ``Security::generateHash()`` is public, and can be used to regenerate CSRF token manually when ``Config\Security::$regenerate`` is false.

Changes
*******
Expand Down
35 changes: 35 additions & 0 deletions user_guide_src/source/changelogs/v4.2.4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Version 4.2.4
#############

Release Date: Unreleased

**4.2.4 release of CodeIgniter4**

.. contents::
:local:
:depth: 2

BREAKING
********

- The method signature of ``Validation.php::processRules()`` and ``Validation.php::getErrorMessage()`` have been changed. Both of these methods add new ``$originalField`` parameter.

Enhancements
************

none.

Changes
*******

none.

Deprecations
************

none.

Bugs Fixed
**********

See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.
2 changes: 1 addition & 1 deletion user_guide_src/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
version = '4.2'

# The full version, including alpha/beta/rc tags.
release = '4.2.2'
release = '4.2.3'

# -- General configuration ---------------------------------------------------

Expand Down
Loading