blib/lib/Catalyst/Plugin/CheckFileUploadTypes.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Catalyst::Plugin::CheckFileUploadTypes;
2 $Catalyst::Plugin::CheckFileUploadTypes::VERSION = '0.20';
3 # ABSTRACT: Check uploaded files are expected and of the correct type
4  
5 1 ▶use Data::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Moose;
9 use namespace::autoclean;
10 with 'Catalyst::ClassData';
11  
12 use MRO::Compat;
13  
14 sub setup {
15 my $c = shift;
16 return $c->maybe::next::method(@_);
17 }
18  
19  
20 sub dispatch {
21 my $c = shift;
22 use Data::Dumper;
23  
24 # If we don't have any uploads, there's nothing more to do:
25 if (!keys %{ $c->req->uploads }) {
26 $c->maybe::next::method(@_);
27 return 1;
28 }
29  
30 # If the action is a Catalyst::Action::REST action, then we want to
31 # attempt to find the _method suffixed action that it will forward to
32 # (e.g. index() -> index_POST()) and if we do, look at the attributes
33 # for that instead - as e.g. an index_GET() isn't going to expect uploads
34 # but the index_POST() may well do.
35 my $action = $c->action;
36 if (ref $action eq 'Catalyst::Action::REST') {
37 my $rest_method = $action->name . '_' . $c->req->method;
38 my $controller = $c->component( $action->class );
39 if (my $rest_action = $controller->action_for($rest_method)) {
40 $action = $rest_action;
41 }
42 }
43  
44  
45  
46  
47 my $expects_uploads = $action->attributes->{ExpectUploads};
48  
49 my %ok_type;
50 for my $type (
51 map { split /[\s,]/ } @{ $action->attributes->{ExpectUploads} }
52 ) {
53 $ok_type{$type}++;
54 }
55  
56 if (!$expects_uploads) {
57 # No uploads expected...
58 $c->log->error("Uploads present, but not expected by action");
59 $c->response->status(400);
60 $c->response->body("File upload not expected");
61 return;
62 } else {
63 # Alright, we do expect uploads; do we care *what*?
64 if (defined $expects_uploads->[0]) {
65 # alright, we need to check the types match.
66 # For every file, determine its type, then for each allowed type,
67 # see if it's a match (and end if so)
68 upload:
69 for my $upload (values %{ $c->req->uploads }) {
70 my $upload_type = $c->_determine_mime_type($upload->fh);
71 $c->log->debug(
72 sprintf "Determined type %s for %s",
73 $upload_type, $upload->filename,
74 );
75 if ($ok_type{$upload_type}) {
76 next upload;
77 } else {
78 $c->log->warn(
79 sprintf "Upload %s with unexpected type %s rejected",
80 $upload->filename,
81 $upload_type,
82 );
83 # FIXME we probably want to make rejections more configurable,
84 # maybe ability to provide coderef to trigger on reject?
85 $c->res->status(400);
86 $c->res->body("Unsupported file content type uploaded");
87 return;
88 }
89 }
90  
91 }
92 }
93  
94 # If we get to here, then we've seen no problems - we've either
95 # short-circuited early because there *weren't* any uploads, or
96 # we've confirmed that the action expects uploads, and if it
97 # specified what types, we're happy that the upload(s) are of
98 # the right type.
99 $c->maybe::next::method(@_);
100  
101 }
102  
103 {
104  
105 my $flm = File::LibMagic->new;
106 sub _determine_mime_type {
107 my ($c, $fh) = @_;
108  
109 my $info = $flm->info_from_handle($fh);
110 # File::LibMagic will have read from the filehandle, seek it back
111 # to the start so we don't confuse things that expect to just
112 # read from it
113 seek($fh, 0, 0);
114  
115 if ($info) {
116 return $info->{mime_type};
117 } else {
118 return 'application/octet-stream';
119 }
120 }
121 }
122  
123 =head1 NAME
124  
125 Catalyst::Plugin::CheckFileUploadTypes - check file uploads are expected and right types
126  
127 =head1 SYNOPSIS
128  
129 use Catalyst qw(CheckFileUploadTypes);
130  
131 # Actions can declare that they expect to receive file uploads:
132 sub upload_file : Local ExpectUploads { ... }
133  
134 # They can also specify that any uploaded files must be of expected types
135 # (determined from file content by File::MMagic, not what the client said)
136 sub upload_file : Local ExpectUploads(image/jpeg image/png) { ... }
137  
138 =head1 DESCRIPTION
139  
140 This plugin allows Catalyst apps to easily accept and validate file uploads.
141  
142 If the action a request is about to be handed to does not declare that it
143 expects file uploads, by setting a C<ExpectUploads> attribute, and the HTTP
144 request contains one or more file uploads, the request will be rejected.
145  
146 The action can also specify the type(s) of files it expects to receive,
147 and the request will be rejected if an uploaded file is of a different
148 type (as determined by L<File::LibMagic> from the file's content, not trusting
149 the file extension or what the client says it is). This avoids uploading
150 executable files / scripts etc to an action which expects image uploads,
151 for instance.
152  
153  
154 =head1 AUTHOR
155  
156 David Precious (BIGPRESH), C<< <davidp@preshweb.co.uk> >>
157  
158 =head1 COPYRIGHT AND LICENCE
159  
160 Copyright (C) 2023-2024 by David Precious
161  
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.
164  
165 =cut
166  
167 1;
↑ Summary