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: Handle non-array JSON in validation #8176

Closed
wants to merge 2 commits into from

Conversation

woodongwong
Copy link
Contributor

@woodongwong woodongwong commented Nov 8, 2023

Description
A check has been added in the JSON validation method to ensure that the data being validated is an array. If the parsed JSON is not an array , it gets defaulted to an empty array to prevent further errors in the validation process.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

A check has been added in the JSON validation method to ensure that the data being validated is an array. If the parsed JSON is not an array , it gets defaulted to an empty array to prevent further errors in the validation process.
@kenjis
Copy link
Member

kenjis commented Nov 8, 2023

Thank you for sending PR.

But why do you need to convert invalid JSON string to an empty array?
If PR #8153 is merged, this PR will be useless.

@woodongwong
Copy link
Contributor Author

@woodongwong
Copy link
Contributor Author

$this->data only makes sense if it is of type array.

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 18, 2023
@kenjis
Copy link
Member

kenjis commented Nov 18, 2023

Thank you for sending this PR.

But we merged #8153, and now $request->getJSON(true) throws an exception.
And this PR does not pass the GitHub Action checks.

   1) system/Validation/Validation.php (unary_operator_spaces, not_operator_with_successor_space, no_whitespace_in_blank_line)
      ---------- begin diff ----------
--- /home/runner/work/CodeIgniter4/CodeIgniter4/system/Validation/Validation.php
+++ /home/runner/work/CodeIgniter4/CodeIgniter4/system/Validation/Validation.php
@@ -496,10 +496,10 @@
         if (strpos($request->getHeaderLine('Content-Type'), 'application/json') !== false) {
             $this->data = $request->getJSON(true);
 
-            if (!is_array($this->data)) {
+            if (! is_array($this->data)) {
                 $this->data = [];
             }
-            
+
             return $this;
         }
 

      ----------- end diff -----------


Found 1 of 877 files that can be fixed in 74.041 seconds, 46.000 MB memory used
Error: Process completed with exit code 8.

https://github.com/codeigniter4/CodeIgniter4/actions/runs/6796578870/job/18817608840?pr=8176

@kenjis
Copy link
Member

kenjis commented Nov 18, 2023

Do you still need this PR?

@kenjis kenjis added the waiting for info Issues or pull requests that need further clarification from the author label Nov 19, 2023
@woodongwong
Copy link
Contributor Author

var_dump(json_decode('2023'));     // int(2023)
var_dump(json_last_error_msg());   // string(8) "No error"

In this scenario, the JSON can be decoded successfully, but it has no significance for validation.

@kenjis kenjis removed the waiting for info Issues or pull requests that need further clarification from the author label Nov 20, 2023
@@ -496,6 +496,10 @@ public function withRequest(RequestInterface $request): ValidationInterface
if (strpos($request->getHeaderLine('Content-Type'), 'application/json') !== false) {
$this->data = $request->getJSON(true);

if (! is_array($this->data)) {
$this->data = [];
}
Copy link
Member

Choose a reason for hiding this comment

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

What should we do when $this->data is not an array?
Is it okay just to throw the value away?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my opinion, if we have decided to use Validation, it must be in the key-value format. If it is a special format, Validation is not applicable. Validation doesn't need to be as comprehensive as JSON schema.

@kenjis
Copy link
Member

kenjis commented Nov 20, 2023

FYI: We do not recommend to use withRequest():
https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#withrequest

@MGatner
Copy link
Member

MGatner commented Nov 20, 2023

I've already shared my opinion that this is an issue due to mixed concerns, but since we are currently supporting key-value request validation I think this is an acceptable "filter".

@kenjis
Copy link
Member

kenjis commented Nov 22, 2023

if we have decided to use Validation, it must be in the key-value format. If it is a special format, Validation is not applicable.

Agreed. That is, if a scalar comes, it should be invalid format.
In terms of security principles, we should not correct invalid values.
We should stop processing.

@MGatner
Copy link
Member

MGatner commented Nov 23, 2023

stop processing

That sounds to me like throwing an exception, rather than passing validation quietly.

@woodongwong
Copy link
Contributor Author

I agree with MGatner's point of view, throwing exceptions is an appropriate way to handle it. Should I throw \CodeIgniter\HTTP\Exceptions\HTTPException? I found that HTTPException carries various exceptions, which is not conducive to catching (another topic).

@kenjis
Copy link
Member

kenjis commented Dec 4, 2023

There are many problems with the current CI4 Exceptions.
Exception classes are not clearly designed.
RuntimeException and LogicException are not properly distinguished.
One exception class is used for multiple purposes.
It is a bit difficult to add appropriate exception class at will.

@woodongwong
Copy link
Contributor Author

I organized the code and created a new PR, #8288 .

@woodongwong woodongwong closed this Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants