11<?php
2+
23namespace Aws \Sns ;
34
45/**
78 */
89class MessageValidator
910{
11+ /**
12+ * @var callable
13+ */
14+ private $ remoteFileReader ;
15+
1016 /**
1117 * Constructs the Message Validator object and ensures that openssl is
1218 * installed.
1319 *
20+ * @param callable $remoteFileReader
21+ *
1422 * @throws \RuntimeException If openssl is not installed
1523 */
16- public function __construct ()
24+ public function __construct (callable $ remoteFileReader = null )
1725 {
26+ $ this ->remoteFileReader = $ remoteFileReader ?: 'file_get_contents ' ;
27+
1828 if (!extension_loaded ('openssl ' )) {
1929 //@codeCoverageIgnoreStart
2030 throw new \RuntimeException ('The openssl extension is required to '
@@ -40,20 +50,22 @@ public function validate(Message $message)
4050 $ this ->validateUrl ($ certUrl );
4151
4252 // Get the cert itself and extract the public key
43- $ certificate = file_get_contents ( $ certUrl );
53+ $ certificate = call_user_func ( $ this -> remoteFileReader , $ certUrl );
4454 $ key = openssl_get_publickey ($ certificate );
4555 if (!$ key ) {
46- throw new MessageValidatorException ('Cannot get the public key '
47- . 'from the certificate. ' );
56+ throw new MessageValidatorException (
57+ 'Cannot get the public key from the certificate. '
58+ );
4859 }
4960
5061 // Verify the signature of the message
5162 $ content = $ message ->getStringToSign ();
5263 $ signature = base64_decode ($ message ->get ('Signature ' ));
5364
5465 if (!openssl_verify ($ content , $ signature , $ key , OPENSSL_ALGO_SHA1 )) {
55- throw new MessageValidatorException ('The message signature is '
56- . 'invalid. ' );
66+ throw new MessageValidatorException (
67+ 'The message signature is invalid. '
68+ );
5769 }
5870 }
5971
@@ -88,7 +100,9 @@ private function validateUrl($url)
88100 // The cert URL must be https, a .pem, and match the following pattern.
89101 static $ hostPattern = '/^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$/ ' ;
90102 $ parsed = parse_url ($ url );
91- if ($ parsed ['scheme ' ] !== 'https '
103+ if (empty ($ parsed ['scheme ' ])
104+ || empty ($ parsed ['host ' ])
105+ || $ parsed ['scheme ' ] !== 'https '
92106 || substr ($ url , -4 ) !== '.pem '
93107 || !preg_match ($ hostPattern , $ parsed ['host ' ])
94108 ) {
0 commit comments