Skip to content

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

Closed
trasher wants to merge 10 commits into
laminas:developfrom
glpi-project:feature/selfsigned-ssl
Closed

Add option novalidatecert to connect(); closes #63#94
trasher wants to merge 10 commits into
laminas:developfrom
glpi-project:feature/selfsigned-ssl

Conversation

@trasher

@trasher trasher commented Jul 9, 2020

Copy link
Copy Markdown
Contributor
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.

Comment thread docs/book/read.md Outdated
Comment thread src/Protocol/Imap.php Outdated
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
@trasher

trasher commented Jul 9, 2020

Copy link
Copy Markdown
Contributor Author

Thanks @cedric-anne :))

@weierophinney weierophinney left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/Protocol/Imap.php Outdated
Comment thread src/Protocol/Imap.php Outdated
}
}

$socket_options = [];

Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread src/Protocol/Imap.php Outdated
public function __construct($host = '', $port = null, $ssl = false)
public function __construct($host = '', $port = null, $ssl = false, $novalidatecert = false)
{
$this->novalidatecert = $novalidatecert;

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread src/Protocol/Pop3.php Outdated
public function __construct($host = '', $port = null, $ssl = false)
public function __construct($host = '', $port = null, $ssl = false, $novalidatecert = false)
{
$this->novalidatecert = $novalidatecert;

Copy link
Copy Markdown
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
Copy Markdown
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 thread src/Protocol/Pop3.php Outdated
Comment on lines +71 to +78
public function setNoValidateCert($novalidatecert)
{

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

Copy link
Copy Markdown
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 thread src/Protocol/Pop3.php Outdated
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
Copy Markdown
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
Copy Markdown
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

Comment thread src/Protocol/Imap.php Outdated
@trasher

trasher commented Jul 10, 2020

Copy link
Copy Markdown
Contributor Author

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.

trasher added 3 commits July 10, 2020 14:18
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
@michalbundyra michalbundyra linked an issue Jul 15, 2020 that may be closed by this pull request
@trasher

trasher commented Jul 15, 2020

Copy link
Copy Markdown
Contributor Author

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.

trasher added 2 commits July 15, 2020 14:17
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Comment thread src/Protocol/ProtocolTrait.php Outdated
Comment thread src/Protocol/ProtocolTrait.php
Comment thread src/Storage/Imap.php
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
trasher added 2 commits July 16, 2020 08:02
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>
@trasher

trasher commented Jul 16, 2020

Copy link
Copy Markdown
Contributor Author

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

glensc commented Jul 16, 2020

Copy link
Copy Markdown
Contributor

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

Comment thread src/Protocol/ProtocolTrait.php Outdated
Comment thread src/Protocol/ProtocolTrait.php Outdated
Signed-off-by: Johan Cwiklinski <jcwiklinski@teclib.com>

@weierophinney weierophinney left a comment

Copy link
Copy Markdown
Member

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 <matthew@weierophinney.net>
@weierophinney

Copy link
Copy Markdown
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 <matthew@weierophinney.net>
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