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

Add option novalidatecert to connect(); closes #63 #94

Closed
wants to merge 10 commits into from
Closed

Add option novalidatecert to connect(); closes #63 #94

wants to merge 10 commits into from

Conversation

trasher
Copy link
Contributor

@trasher trasher commented Jul 9, 2020

Q A
Documentation no
Bugfix no
BC Break no
New Feature yes
RFC no
QA no

Description

Allow connection with self signed SSL certificates; see #63; port of existing zendframework/zend-mail#247.

docs/book/read.md Outdated Show resolved Hide resolved
src/Protocol/Imap.php Outdated Show resolved Hide resolved
@trasher
Copy link
Contributor Author

trasher commented Jul 9, 2020

Thanks @cedric-anne :))

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

First off, thanks for this patch! I'm sure a number of people will appreciate having it in place, and it will make testing locally, where you might be forced to use self-signed certs, far easier!

I think you can improve this substantially by moving the various functionalities around the $novalidatecert property and setter into the ProtocolTrait, as well as the functionality for creating the socket options. I've provided guidance in the comments below.

Additionally, it would be great if you could figure out a way to unit test this, as the new functionality is not covered at all. While I'm reasonably certain it will be fine, a unit test ensures we don't break it in the future.

src/Protocol/Imap.php Outdated Show resolved Hide resolved
@@ -87,8 +104,29 @@ public function connect($host, $port = null, $ssl = false)
}
}

$socket_options = [];
Copy link
Member

Choose a reason for hiding this comment

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

Please use camelCase for variable names for consistency (this is part of our coding standard).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. I've also factorized this piece of code, since this was entirely duplicated in both Imap and Pop3 classes

{
$this->novalidatecert = $novalidatecert;
Copy link
Member

Choose a reason for hiding this comment

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

Please call $this->setNoValidateCert($novalidatecert) to ensure you get proper validation of the value (see my notes on that method as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

{
$this->novalidatecert = $novalidatecert;
Copy link
Member

Choose a reason for hiding this comment

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

Please call $this->setNoValidateCert($novalidatecert) to ensure you get proper validation of the value (see my notes on that method as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done aswell

Comment on lines 71 to 78
public function setNoValidateCert($novalidatecert)
{

if (is_bool($novalidatecert)) {
$this->novalidatecert = $novalidatecert;
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Please see my notes on the same method in the Imap class.

Comment on lines 112 to 134
$socket_options = [];

if ($this->novalidatecert) {
$socket_options = [
'ssl' => [
'verify_peer_name' => false,
'verify_peer' => false,
]
];
}

$socket_context = stream_context_create($socket_options);

ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$this->socket = stream_socket_client(
$host . ":" . $port,
$errno,
$errstr,
self::TIMEOUT_CONNECTION,
STREAM_CLIENT_CONNECT,
$socket_context
);

Copy link
Member

Choose a reason for hiding this comment

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

Please see my notes on this in the Imap class.

Since this code is reproduced in both classes, I'd argue that you should:

  • Move the $novalidatecert property into ProtocolTrait
  • Move the setNoValidateCert() method into ProtocolTrait
  • Create a new method in ProtocolTrait:
/**
 * @return array
 */
private function prepareSocketOptions()
{
    return $this->novalidatecert
        ? [
            'ssl' => [
                'verify_peer_name' => false,
                'verify_peer'            => false,
            ],
        ]
        : [];
}

Then, in the connect() method of each, you can do:

$this->socket = stream_socket_client(
    $host . ':' . $port,
    $errno,
    $errstr,
    self::TIMEOUT_CONNECTION,
    STREAM_CLIENT_CONNECT,
    stream_context_create($this->prepareSocketOptions())
);

(This also means the comments about camelCase names become irrelevant, as the variables are never created.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved then entire socket preparation, and splitted options preparation to another method

src/Protocol/Imap.php Outdated Show resolved Hide resolved
@trasher
Copy link
Contributor Author

trasher commented Jul 10, 2020

Hi @weierophinney thank you for the feedbacks :)

I will fix according to your review comments.
I do not know if this is possible to add tests on that; I guess some case should be added on the existing "Storage" ones; I can try that and we'll see the result.

@michalbundyra michalbundyra linked an issue Jul 15, 2020 that may be closed by this pull request
@trasher
Copy link
Contributor Author

trasher commented Jul 15, 2020

Tests are failing: Laminas\Mail\Protocol\AbstractProtocol and Laminas\Mail\Protocol\ProtocolTrait define the same property ($socket); maybe the socket creation method should be moved in AbstractProtocol.

Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
@trasher
Copy link
Contributor Author

trasher commented Jul 16, 2020

I've added a very basic test; but I do not know if it is possible to do more; and I do not really have time to investigate on that.

@glensc
Copy link
Contributor

glensc commented Jul 16, 2020

@trasher let's hope @weierophinney is happy with the changes. 3️⃣ pairs of 👀 reviewed the code now.

src/Protocol/ProtocolTrait.php Outdated Show resolved Hide resolved
src/Protocol/ProtocolTrait.php Outdated Show resolved Hide resolved
Signed-off-by: Johan Cwiklinski <[email protected]>
Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

Looks good! I've made some minor changes locally (pushing momentarily) to cover some nitpicks I had but didn't want to bother you with.

🚢

weierophinney added a commit that referenced this pull request Jul 28, 2020
Signed-off-by: Matthew Weier O'Phinney <[email protected]>
@weierophinney
Copy link
Member

I rebased locally and made edits... but don't have rights to push back to the origin repo. Rest assured, though - your commits are in!

artemii-karkusha pushed a commit to artemii-karkusha/laminas-mail that referenced this pull request May 24, 2023
Signed-off-by: Matthew Weier O'Phinney <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SSL/TLS] Support for pass the stream context
6 participants