blib/lib/AWS/SNS/Verify.pm

filter: Statementstmt 88.4% 69 / 78 Branchbran 71.4% 10 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 88.2% 15 / 17 Podpod 100% 5 / 5 totaltotal 87.1% 102 / 117 SCAR 23.3
CC
10
Cov
86%
fetch_certificate
6.0
valid_cert_url
5.0
generate_signature_string
4.1
CRAP 10.3
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AWS::SNS::Verify;
4 2 $AWS::SNS::Verify::VERSION = '0.0104';
5 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 has body => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 has message => (
19 is => 'ro',
20 lazy => 1,
21 default => sub {
22 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 return JSON::decode_json($self->body);
24 }
25 2 );
26  
27 has certificate_string => (
28 is => 'ro',
29 lazy => 1,
30 default => sub {
31 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 return $self->fetch_certificate;
33 }
34 2 );
35  
36 has certificate => (
37 is => 'ro',
38 lazy => 1,
39 default => sub {
40 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 3 return Crypt::PK::RSA->new(\$self->certificate_string);
42 }
43 2 );
44  
45 2 has validate_signing_cert_url => (
46 is => 'ro',
47 lazy => 1,
48 default => 1,
49 );
50  
51 sub fetch_certificate {
52 0 ▶ my $self = shift;
Subroutine fetch_certificate: not called CC=2 SCAR=17.9
Pod documented
53 0 my $url = $self->valid_cert_url($self->message->{SigningCertURL});
54 0 my $response = HTTP::Tiny->new->get($url);
55 0 ▶ if ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 0 0
56 0 return $response->{content};
57 }
58 else {
59 0 ouch $response->{status}, $response->{reason}, $response;
60 }
61 }
62  
63 sub generate_signature_string {
64 2 ▶ my $self = shift;
Subroutine generate_signature_string: called CC=4 SCAR=14.1
Pod documented
65 2 my $body = $self->message;
66 2 my @fields;
67 2 ▶ if ($body->{Type} eq 'Notification') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->{'Type'} eq 'Notification') { } 2 0 2
68 2 @fields = (qw(Message MessageId Subject Timestamp TopicArn Type)) ;
69 }
70 else {
71 0 @fields = (qw(Message MessageId SubscribeURL Timestamp Token TopicArn Type));
72 }
73 2 my @parts;
74 2 foreach my $field (@fields) {
75 12 ▶ if (exists $body->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $body->{$field}) 12 0 12
76 12 push @parts, $field;
77 12 push @parts, $body->{$field};
78 }
79 }
80 2 return join("\n", @parts)."\n";
81 }
82  
83 sub decode_signature {
84 2 ▶ my $self = shift;
Subroutine decode_signature: called CC=1 SCAR=0.0
Pod documented
85 2 return decode_base64($self->message->{Signature});
86 }
87  
88 sub verify {
89 3 ▶ my $self = shift;
Subroutine verify: called CC=2 SCAR=6.9
Pod documented
90 3 my $pk = $self->certificate;
91 2 ▶ unless ($pk->verify_message($self->decode_signature, $self->generate_signature_string, 'SHA1', 'v1.5')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk->verify_message($self->decode_signature, $self->generate_signature_string, "SHA1", "v1.5")) 1 1 2
92 1 ouch 'Bad SNS Signature', 'Could not verify the SES message from its signature.', $self;
93 }
94 1 return 1;
95 }
96  
97 # See also:
98 # https://github.com/aws/aws-php-sns-message-validator/blob/master/src/MessageValidator.php#L22
99 sub valid_cert_url {
100 6 ▶ my $self = shift;
Subroutine valid_cert_url: called CC=5 SCAR=16.1
Pod documented
101 6 my ($url_string) = @_;
102 6 ▶ $url_string ||= '';
Condition100%2/2
$url_string ||= ""
l!l
51
MC/DC100%1/1
$url_string ||= ""
$url_string
Truth table100%2/2
A
A= $url_string
Aresult
11
00
103  
104 6 ▶ return $url_string unless $self->validate_signing_cert_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_signing_cert_url 1 5 6
105  
106 5 my $url = URI::URL->new($url_string);
107 5 ▶ unless ( $url->can('host') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url->can("host")) 2 3 5
108 2 ouch 'Bad SigningCertURL', "The SigningCertURL ($url_string) isn't a valid URL", $self;
109 }
110 3 my $host = $url->host;
111  
112 # Match all regional SNS endpoints, e.g.
113 # sns.<region>.amazonaws.com (AWS)
114 # sns.us-gov-west-1.amazonaws.com (AWS GovCloud)
115 # sns.cn-north-1.amazonaws.com.cn (AWS China)
116 3 my $dot = qr/\./;
117 3 my $region = qr/[a-zA-Z0-9-]+/;
118 3 ▶ unless ($host =~ /^ sns $dot $region $dot amazonaws $dot com(\.cn)? $/x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^ sns $dot $region $dot amazonaws $dot com(\.cn)? $/x) 1 2 3
119 1 ouch 'Bad SigningCertURL', "The SigningCertURL ($url_string) isn't an Amazon endpoint", $self;
120 }
121  
122 2 return $url_string;
123 }
124  
125  
126 =head1 NAME
127  
128 AWS::SNS::Verify - Verifies authenticity of SNS messages.
129  
130 =head1 VERSION
131  
132 version 0.0104
133  
134 =head1 SYNOPSIS
135  
136 my $body = request->body; # example fetch raw body from Dancer
137 my $sns = AWS::SNS::Verify->new(body => $body);
138 if ($sns->verify) {
139 return $sns->message;
140 }
141  
142 =head1 DESCRIPTION
143  
144 This module will parse a message from Amazon Simple Notification Service and validate its signature. This way you know the message came from AWS and not some third-party. More info here: L<http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.verify.signature.html>.
145  
146 =head1 METHODS
147  
148 =head2 new
149  
150 Constructor.
151  
152 =over
153  
154 =item body
155  
156 Required. JSON string posted by AWS SNS. Looks like:
157  
158 {
159 "Type" : "Notification",
160 "MessageId" : "a890c547-5d98-55e2-971d-8826fff56413",
161 "TopicArn" : "arn:aws:sns:us-east-1:041977924901:foo",
162 "Subject" : "test subject",
163 "Message" : "test message",
164 "Timestamp" : "2015-02-20T20:59:25.401Z",
165 "SignatureVersion" : "1",
166 "Signature" : "kzi3JBQz64uFAXG9ZuAwPI2gYW5tT7OF83oeHb8v0/XRPsy0keq2NHTCpQVRxCgPOJ/QUB2Yl/L29/W4hiHMo9+Ns0hrqyasgUfjq+XkVR1WDuYLtNaEA1vLnA0H9usSh3eVVlLhpYzoT4GUoGgstRVvFceW2QVF9EYUQyromlcbOVtVpKCEINAvGEEKJNGTXQQUkPUka3YMhHitgQg1WlFBmf+oweSYUEj8+RoguWsn6vluxD0VtIOGOml5jlUecfhDqnetF5pUVYMqCHPfHn6RBguiW+XD6XWsdKKxkjqo90a65Nlb72gPSRw6+sIEIgf4J39WFZK+FCpeSm0qAg==",
167 "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11f4f9e198.pem",
168 "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:041977924901:foo:20b2d060-2a32-4506-9cb0-24b8b9e605e1",
169 "MessageAttributes" : {
170 "AWS.SNS.MOBILE.MPNS.Type" : {"Type":"String","Value":"token"},
171 "AWS.SNS.MOBILE.WNS.Type" : {"Type":"String","Value":"wns/badge"},
172 "AWS.SNS.MOBILE.MPNS.NotificationClass" : {"Type":"String","Value":"realtime"}
173 }
174 }
175  
176 =item certificate_string
177  
178 By default AWS::SNS::Verify will fetch the certificate string by issuing an HTTP GET request to C<SigningCertURL>. The SigningCertURL in the message must be a AWS SNS endpoint.
179  
180 If you wish to use a cached version, then pass it in.
181  
182 =item validate_signing_cert_url (default: true)
183  
184 If you're using a fake SNS server in your local test environment, the SigningCertURL won't be an AWS endpoint. If so, set validate_signing_cert_url to 0.
185  
186 Don't ever do this in any kind of Production environment.
187  
188 =back
189  
190 =head2 verify
191  
192 Returns a 1 on success, or die with an L<Ouch> on a failure.
193  
194 =head2 message
195  
196 Returns a hash reference of the decoded L<body> that was passed in to the constructor.
197  
198 =head2 certificate_string
199  
200 If you want to cache the certificate in a local cache, then get it using this method.
201  
202  
203 =head2 decode_signature
204  
205 You should never need to call this, it decodes the base64 signature.
206  
207 =head2 fetch_certificate
208  
209 You should never need to call this, it fetches the signing certificate.
210  
211  
212 =head2 generate_signature_string
213  
214 You should never need to call this, it generates the signature string required to verify the request.
215  
216 =head2 valid_cert_url
217  
218 You should never need to call this, it checks the validity of the certificate signing URL per L<https://github.com/aws/aws-php-sns-message-validator/blob/master/src/MessageValidator.php#L22>
219  
220 =head1 REQUIREMENTS
221  
222 Requires Perl 5.12 or higher and these modules:
223  
224 =over
225  
226 =item *
227  
228 Ouch
229  
230 =item *
231  
232 JSON
233  
234 =item *
235  
236 HTTP::Tiny
237  
238 =item *
239  
240 MIME::Base64
241  
242 =item *
243  
244 Moo
245  
246 =item *
247  
248 Crypt::OpenSSL::RSA
249  
250 =item *
251  
252 Crypt::OpenSSL::X509
253  
254 =back
255  
256 =head1 SUPPORT
257  
258 =over
259  
260 =item Repository
261  
262 L<http://github.com/rizen/AWS-SNS-Verify>
263  
264 =item Bug Reports
265  
266 L<http://github.com/rizen/AWS-SNS-Verify/issues>
267  
268 =back
269  
270  
271 =head1 AUTHOR
272  
273 JT Smith <jt_at_plainblack_dot_com>
274  
275 =head1 LEGAL
276  
277 AWS::SNS::Verify is Copyright 2015 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
278  
279 =cut
280  
281  
282 2 1;
↑ Summary