1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# Copyright (C) 2011 Niels Thykier <niels@thykier.net>
# Copyright (C) 2019-2020 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
package Lintian::Processable;
use v5.20;
use warnings;
use utf8;
use warnings::register;
use Const::Fast;
use File::Basename;
use Path::Tiny;
use Moo::Role;
use MooX::Aliases;
use namespace::clean;
const my $EMPTY => q{};
const my $COLON => q{:};
const my $SLASH => q{/};
const my $UNDERSCORE => q{_};
const my $EVIL_CHARACTERS => qr{[/&|;\$"'<>]};
=encoding utf-8
=head1 NAME
Lintian::Processable -- An (abstract) object that Lintian can process
=head1 SYNOPSIS
use Lintian::Processable;
=head1 DESCRIPTION
Instances of this perl class are objects that Lintian can process (e.g.
deb files). Multiple objects can then be combined into
L<groups|Lintian::Group>, which Lintian will process
together.
=head1 INSTANCE METHODS
=over 4
=item name
Returns the name of the package.
=item type
Returns the type of package (e.g. binary, source, udeb ...)
=item hints
=item $proc->version
Returns the version of the package.
=item $proc->path
Returns the path to the packaged version of actual package. This path
is used in case the data needs to be extracted from the package.
=item basename
Returns the basename of the package path.
=item $proc->architecture
Returns the architecture(s) of the package. May return multiple values
from changes processables. For source processables it is "source".
=item $proc->source_name
Returns the name of the source package.
=item $proc->source_version
Returns the version of the source package.
=item $proc->tainted
Returns a truth value if one or more fields in this Processable is
tainted. On a best effort basis tainted fields will be sanitized
to less dangerous (but possibly invalid) values.
=item fields
Lintian::Deb822::Section with primary field values.
=item $proc->pooldir
Returns a reference to lab this Processable is in.
=item $proc->basedir
Returns the base directory of this package inside the lab.
=cut
has path => (
is => 'rw',
default => $EMPTY,
trigger => sub {
my ($self, $path) = @_;
my $basename = basename($path);
$self->basename($basename);
}
);
has basename => (is => 'rw', default => $EMPTY);
has type => (is => 'rw', default => $EMPTY);
has hints => (is => 'rw', default => sub { [] });
has architecture => (
is => 'rw',
coerce => sub {
my ($value) = @_;
return clean_field($value);
},
default => $EMPTY
);
has name => (
is => 'rw',
coerce => sub {
my ($value) = @_;
return clean_field($value);
},
default => $EMPTY
);
has source_name => (
is => 'rw',
coerce => sub {
my ($value) = @_;
return clean_field($value);
},
default => $EMPTY
);
has source_version =>(
is => 'rw',
coerce => sub {
my ($value) = @_;
return clean_field($value);
},
default => $EMPTY
);
has version => (
is => 'rw',
coerce => sub {
my ($value) = @_;
return clean_field($value);
},
default => $EMPTY
);
has tainted => (is => 'rw', default => 0);
has fields => (is => 'rw', default => sub { Lintian::Deb822::Section->new; });
has pooldir => (is => 'rw', default => $EMPTY);
has basedir => (
is => 'rw',
lazy => 1,
trigger => sub {
my ($self, $folder) = @_;
return
unless length $folder;
# create directory
path($folder)->mkpath
unless -e $folder;
},
default => sub {
my ($self) = @_;
my $path
= $self->source_name
. $SLASH
. $self->name
. $UNDERSCORE
. $self->version;
$path .= $UNDERSCORE . $self->architecture
unless $self->type eq 'source';
$path .= $UNDERSCORE . $self->type;
# architectures can contain spaces in changes files
$path =~ s/\s/-/g;
# colon can be a path separator
$path =~ s/:/_/g;
my $basedir = $self->pooldir . "/$path";
return $basedir;
}
);
=item C<identifier>
Produces an identifier for this processable. The identifier is
based on the type, name, version and architecture of the package.
=cut
sub identifier {
my ($self) = @_;
my $id = $self->type . $COLON . $self->name . $UNDERSCORE . $self->version;
# add architecture unless it is source
$id .= $UNDERSCORE . $self->architecture
unless $self->type eq 'source';
$id =~ s/\s+/_/g;
return $id;
}
=item clean_field
Cleans a field of evil characters to prevent traversal or worse.
=cut
sub clean_field {
my ($value) = @_;
# make sure none of the fields can cause traversal
my $clean = $value;
$clean =~ s/${$EVIL_CHARACTERS}/_/g;
return $clean;
}
=item guess_name
=cut
sub guess_name {
my ($self, $path) = @_;
my $guess = path($path)->basename;
# drop extension, to catch fields-general-missing.deb
$guess =~ s/\.[^.]*$//;
# drop everything after the first underscore, if any
$guess =~ s/_.*$//;
# 'path/lintian_2.5.2_amd64.changes' became 'lintian'
return $guess;
}
=back
=head1 AUTHOR
Originally written by Niels Thykier <niels@thykier.net> for Lintian.
Substantial portions written by Russ Allbery <rra@debian.org> for Lintian.
=head1 SEE ALSO
lintian(1)
L<Lintian::Processable::Installable>
L<Lintian::Processable::Buildinfo>
L<Lintian::Processable::Changes>
L<Lintian::Processable::Source>
L<Lintian::Group>
=cut
1;
# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et
|