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: [CURLRequest] skip hostname checks if options 'verify' false #8258

Merged

Conversation

NicolaeIotu
Copy link
Contributor

@NicolaeIotu NicolaeIotu commented Nov 26, 2023

Supersedes #8257

Description
When CURLRequest options 'verify' is set to false, some CURLOPT_SSL_... options should be disabled in such a way as to allow requests to pass through in case the destination is for example on private networks.

Avoids SSL errors: SSL: certificate subject name 'CA' does not match target host name 'localhost'

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

@kenjis kenjis changed the title add: skip hostname checks if CURLRequest options 'verify' false fix: [CURLRequest] skip hostname checks if options 'verify' false Nov 26, 2023
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 26, 2023
@kenjis
Copy link
Member

kenjis commented Nov 26, 2023

Thank you!
But some checks were not successful.
Please fix the code to pass all checks.

@kenjis kenjis added the docs needed Pull requests needing documentation write-ups and/or revisions. label Nov 27, 2023
$curlOptions[CURLOPT_SSL_VERIFYPEER] = 1;
$curlOptions[CURLOPT_CAINFO] = $file;
if ($config['verify'] === 'yes') {
$curlOptions[CURLOPT_SSL_VERIFYPEER] = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Why do you set 1?
It seems this parameter should be boolean.

CURLOPT_SSL_VERIFYPEER
false to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
https://www.php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters

@kenjis kenjis added the tests needed Pull requests that need tests label Nov 27, 2023
Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

There is no test case for options 'verify' false.
Please add it.

@kenjis kenjis removed the docs needed Pull requests needing documentation write-ups and/or revisions. label Nov 27, 2023
@NicolaeIotu NicolaeIotu requested a review from kenjis November 28, 2023 07:52
Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

LGTM!

@kenjis kenjis removed the tests needed Pull requests that need tests label Nov 28, 2023
@kenjis
Copy link
Member

kenjis commented Nov 28, 2023

Thank you!
But why did you send to 4.5?
This is a bug fix, so you can send to develop.
Do you think this is a breaking change?

@NicolaeIotu
Copy link
Contributor Author

I don't think it's breaking anything. It's just that that the label bug was added afterwards.
So I have to close this PR and open a new one while choosing branch develop? Is this ok?

@kenjis kenjis changed the base branch from 4.5 to develop November 28, 2023 08:16
@kenjis kenjis changed the base branch from develop to 4.5 November 28, 2023 08:16
@kenjis
Copy link
Member

kenjis commented Nov 28, 2023

I think this is a bug fix, so I added the label bug.
I'm okay that you change to develop.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#if-you-sent-to-the-wrong-branch

…alse.

When CURLRequest options 'verify' is set to false, some CURLOPT_SSL_... options should be disabled in such a way as to allow requests to pass through in case the destination is for example on private networks.

Avoids SSL errors: SSL: certificate subject name 'CA' does not match target host name 'localhost'
@NicolaeIotu NicolaeIotu force-pushed the add-curlrequest-curlopt_ssl_verifyhost branch from 8207e01 to e159bfd Compare November 28, 2023 08:29
@NicolaeIotu NicolaeIotu changed the base branch from 4.5 to develop November 28, 2023 08:34
@NicolaeIotu
Copy link
Contributor Author

Completed the switch to branch develop.

kenjis
kenjis previously approved these changes Nov 28, 2023
$curlOptions[CURLOPT_CAINFO] = $file;
$curlOptions[CURLOPT_SSL_VERIFYPEER] = 1;
$curlOptions[CURLOPT_CAINFO] = $file;
if ($config['verify'] === 'yes') {
Copy link
Member

Choose a reason for hiding this comment

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

There might be something I don't understand, but how can we have yes as a value here?

The user guide says about verify option:

This option describes the SSL certificate verification behavior. If the verify option is true, it enables the SSL certificate verification and uses the default CA bundle provided by the operating system. If set to false it will disable the certificate verification (this is insecure, and allows man-in-the-middle attacks!). You can set it to a string that contains the path to a CA bundle to enable verification with a custom certificate. The default value is true:

Dunno why, but we use ssl_key instead of verify when it's a string. The ssl_key option is not documented at all.

I feel like something has to be changed here. Either the code or the documentation.

Copy link
Member

@kenjis kenjis Nov 28, 2023

Choose a reason for hiding this comment

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

I missed it! Thank you.
yes is wrong.

Copy link
Member

Choose a reason for hiding this comment

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

Dunno why, but we use ssl_key instead of verify when it's a string.

This seems a bug.
We don't support ssl_key.
See https://docs.guzzlephp.org/en/stable/request-options.html#ssl-key

Copy link
Member

Choose a reason for hiding this comment

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

Yeah... since we should be compatible with guzzle, we should use verify.

When fixed, this should be noted in the changelog with some additional description, because some users could dig into the code and set ssl_key just to make it work.

@kenjis
Copy link
Member

kenjis commented Nov 28, 2023

@NicolaeIotu We don't use git merge in PR branches. Please use git rebase if you can.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch

@kenjis kenjis dismissed their stale review November 28, 2023 21:02

yes is invalid.

@NicolaeIotu
Copy link
Contributor Author

So in case verify it's a string then it can only be "the path to a CA bundle".
Also will try to make git rebase.

@kenjis
Copy link
Member

kenjis commented Nov 30, 2023

There is still two merge commits. Can you run git rebase to remove the commits?

@NicolaeIotu NicolaeIotu force-pushed the add-curlrequest-curlopt_ssl_verifyhost branch from 779d5c7 to e159bfd Compare November 30, 2023 10:50
@NicolaeIotu
Copy link
Contributor Author

I'm doing some git action to revert my mistakes. Please ignore for a while.

…RLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
@NicolaeIotu
Copy link
Contributor Author

I think it's ok now.

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

We need a changelog update, which will inform users about these fixes: https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/changelogs/v4.4.4.rst

Both: Bugs and Breaking sections should be filled.

@@ -548,17 +548,21 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])

// SSL Verification
if (isset($config['verify'])) {
if (is_string($config['verify'])) {
$file = realpath($config['ssl_key']) ?: $config['ssl_key'];
$configVerify = $config['verify'];
Copy link
Member

Choose a reason for hiding this comment

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

Please don't introduce an additional variable, it makes the code less readable - use $config['verify'].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to clarify. Array element $config['verify'] is accessed 8 times. Isn't this less efficient than using a local variable?

Copy link
Member

Choose a reason for hiding this comment

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

No, why would it be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the element of the array it's not changing value, you get a performance increase if assigning its value to a local variable and using that instead of accessing the array every time especially if the key is not an integer and the value of the element is required many times.

This is pretty much valid for all languages when data type is using hash tables and others alike.

In this case there is a performance increase if using the local variable, but it may be more important to make the code readable.

I forgot to ask. The breaking change is that $config['verify'] cannot take the value 'yes' anymore. This is the only breaking change?

Copy link
Member

Choose a reason for hiding this comment

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

If the element of the array it's not changing value, you get a performance increase...

In this case, I'm pretty sure we would not see the difference. That's why I choose the readability.

I forgot to ask. The breaking change is that $config['verify'] cannot take the value 'yes' anymore. This is the only breaking change?

Was using the value yes was documented somewhere? If not this can be skipped.
The breaking change is not using the ssl_key array key anymore.

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

The bug description should also go to the user_guide_src/source/changelogs/v4.4.4.rst. You can check the previous versions for a reference.

CHANGELOG.md Outdated
Comment on lines 3 to 9
## [v4.4.4](https://github.com/codeigniter4/CodeIgniter4/tree/v4.4.4) (Unreleased)
[Full Changelog](https://github.com/codeigniter4/CodeIgniter4/compare/v4.4.3...v4.4.4)

### Fixed Bugs

* fix: [CURLRequest] skip hostname checks if options 'verify' false by @NicolaeIotu in https://github.com/codeigniter4/CodeIgniter4/pull/8258

Copy link
Member

Choose a reason for hiding this comment

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

I think that this file is handled automatically when we prepare the release. @kenjis can confirm or deny.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

File user_guide_src/source/changelogs/v4.4.4.rst was updated.
Now I'll revert the changes to CHANGELOG.md.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

user_guide_src/source/changelogs/v4.4.4.rst Outdated Show resolved Hide resolved
@kenjis kenjis added the breaking change Pull requests that may break existing functionalities label Dec 1, 2023
@kenjis
Copy link
Member

kenjis commented Dec 1, 2023

The last thing. Add short instruction how to change when upgrading in
https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/installation/upgrade_444.rst#breaking-changes

Although confusing with changelog, the changelog describes what has changed and the upgrading guide describes what users need to do when upgrading.

@kenjis kenjis merged commit 54cbc32 into codeigniter4:develop Dec 2, 2023
62 checks passed
@kenjis
Copy link
Member

kenjis commented Dec 2, 2023

@NicolaeIotu Thank you!

@NicolaeIotu
Copy link
Contributor Author

@kenjis Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Pull requests that may break existing functionalities 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