~ubuntu-branches/ubuntu/edgy/bugzilla/edgy

« back to all changes in this revision

Viewing changes to Bugzilla/Util.pm

  • Committer: Bazaar Package Importer
  • Author(s): Alexis Sukrieh
  • Date: 2005-10-03 16:51:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051003165101-38n0y5qofd68vole
Tags: 2.18.4-1
* New upstream minor release
  + Fixed a security issue: It was possible to bypass the "user
    visibility groups" restrictions if user-matching was turned on
    in "substring" mode.
  + Fixed a security issue: config.cgi exposed information to users who
    weren't logged in, even when "requirelogin" was turned on in Bugzilla.
  (closes: #331206)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: perl; indent-tabs-mode: nil -*-
 
2
#
 
3
# The contents of this file are subject to the Mozilla Public
 
4
# License Version 1.1 (the "License"); you may not use this file
 
5
# except in compliance with the License. You may obtain a copy of
 
6
# the License at http://www.mozilla.org/MPL/
 
7
#
 
8
# Software distributed under the License is distributed on an "AS
 
9
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
# implied. See the License for the specific language governing
 
11
# rights and limitations under the License.
 
12
#
 
13
# The Original Code is the Bugzilla Bug Tracking System.
 
14
#
 
15
# The Initial Developer of the Original Code is Netscape Communications
 
16
# Corporation. Portions created by Netscape are
 
17
# Copyright (C) 1998 Netscape Communications Corporation. All
 
18
# Rights Reserved.
 
19
#
 
20
# Contributor(s): Terry Weissman <terry@mozilla.org>
 
21
#                 Dan Mosedale <dmose@mozilla.org>
 
22
#                 Jacob Steenhagen <jake@bugzilla.org>
 
23
#                 Bradley Baetz <bbaetz@student.usyd.edu.au>
 
24
#                 Christopher Aillon <christopher@aillon.com>
 
25
 
 
26
package Bugzilla::Util;
 
27
 
 
28
use strict;
 
29
 
 
30
use base qw(Exporter);
 
31
@Bugzilla::Util::EXPORT = qw(is_tainted trick_taint detaint_natural
 
32
                             detaint_signed
 
33
                             html_quote url_quote value_quote xml_quote
 
34
                             css_class_quote
 
35
                             lsearch max min
 
36
                             trim format_time);
 
37
 
 
38
use Bugzilla::Config;
 
39
 
 
40
# This is from the perlsec page, slightly modifed to remove a warning
 
41
# From that page:
 
42
#      This function makes use of the fact that the presence of
 
43
#      tainted data anywhere within an expression renders the
 
44
#      entire expression tainted.
 
45
# Don't ask me how it works...
 
46
sub is_tainted {
 
47
    return not eval { my $foo = join('',@_), kill 0; 1; };
 
48
}
 
49
 
 
50
sub trick_taint {
 
51
    require Carp;
 
52
    Carp::confess("Undef to trick_taint") unless defined $_[0];
 
53
    my ($match) = $_[0] =~ /^(.*)$/s;
 
54
    $_[0] = $match;
 
55
    return (defined($_[0]));
 
56
}
 
57
 
 
58
sub detaint_natural {
 
59
    my ($match) = $_[0] =~ /^(\d+)$/;
 
60
    $_[0] = $match;
 
61
    return (defined($_[0]));
 
62
}
 
63
 
 
64
sub detaint_signed {
 
65
    my ($match) = $_[0] =~ /^([-+]?\d+)$/;
 
66
    $_[0] = $match;
 
67
    # Remove any leading plus sign.
 
68
    if (defined($_[0]) && $_[0] =~ /^\+(\d+)$/) {
 
69
        $_[0] = $1;
 
70
    }
 
71
    return (defined($_[0]));
 
72
}
 
73
 
 
74
sub html_quote {
 
75
    my ($var) = (@_);
 
76
    $var =~ s/\&/\&amp;/g;
 
77
    $var =~ s/</\&lt;/g;
 
78
    $var =~ s/>/\&gt;/g;
 
79
    $var =~ s/\"/\&quot;/g;
 
80
    return $var;
 
81
}
 
82
 
 
83
# This orignally came from CGI.pm, by Lincoln D. Stein
 
84
sub url_quote {
 
85
    my ($toencode) = (@_);
 
86
    $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
 
87
    return $toencode;
 
88
}
 
89
 
 
90
sub css_class_quote {
 
91
    my ($toencode) = (@_);
 
92
    $toencode =~ s/ /_/g;
 
93
    $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("&#x%x;",ord($1))/eg;
 
94
    return $toencode;
 
95
}
 
96
 
 
97
sub value_quote {
 
98
    my ($var) = (@_);
 
99
    $var =~ s/\&/\&amp;/g;
 
100
    $var =~ s/</\&lt;/g;
 
101
    $var =~ s/>/\&gt;/g;
 
102
    $var =~ s/\"/\&quot;/g;
 
103
    # See bug http://bugzilla.mozilla.org/show_bug.cgi?id=4928 for 
 
104
    # explanaion of why bugzilla does this linebreak substitution. 
 
105
    # This caused form submission problems in mozilla (bug 22983, 32000).
 
106
    $var =~ s/\r\n/\&#013;/g;
 
107
    $var =~ s/\n\r/\&#013;/g;
 
108
    $var =~ s/\r/\&#013;/g;
 
109
    $var =~ s/\n/\&#013;/g;
 
110
    return $var;
 
111
}
 
112
 
 
113
sub xml_quote {
 
114
    my ($var) = (@_);
 
115
    $var =~ s/\&/\&amp;/g;
 
116
    $var =~ s/</\&lt;/g;
 
117
    $var =~ s/>/\&gt;/g;
 
118
    $var =~ s/\"/\&quot;/g;
 
119
    $var =~ s/\'/\&apos;/g;
 
120
    return $var;
 
121
}
 
122
 
 
123
sub lsearch {
 
124
    my ($list,$item) = (@_);
 
125
    my $count = 0;
 
126
    foreach my $i (@$list) {
 
127
        if ($i eq $item) {
 
128
            return $count;
 
129
        }
 
130
        $count++;
 
131
    }
 
132
    return -1;
 
133
}
 
134
 
 
135
sub max {
 
136
    my $max = shift(@_);
 
137
    foreach my $val (@_) {
 
138
        $max = $val if $val > $max;
 
139
    }
 
140
    return $max;
 
141
}
 
142
 
 
143
sub min {
 
144
    my $min = shift(@_);
 
145
    foreach my $val (@_) {
 
146
        $min = $val if $val < $min;
 
147
    }
 
148
    return $min;
 
149
}
 
150
 
 
151
sub trim {
 
152
    my ($str) = @_;
 
153
    if ($str) {
 
154
      $str =~ s/^\s+//g;
 
155
      $str =~ s/\s+$//g;
 
156
    }
 
157
    return $str;
 
158
}
 
159
 
 
160
sub format_time {
 
161
    my ($time) = @_;
 
162
 
 
163
    my ($year, $month, $day, $hour, $min, $sec);
 
164
    if ($time =~ m/^\d{14}$/) {
 
165
        # We appear to have a timestamp direct from MySQL
 
166
        $year  = substr($time,0,4);
 
167
        $month = substr($time,4,2);
 
168
        $day   = substr($time,6,2);
 
169
        $hour  = substr($time,8,2);
 
170
        $min   = substr($time,10,2);
 
171
    }
 
172
    elsif ($time =~ m/^(\d{4})[-\.](\d{2})[-\.](\d{2}) (\d{2}):(\d{2})(:(\d{2}))?$/) {
 
173
        $year  = $1;
 
174
        $month = $2;
 
175
        $day   = $3;
 
176
        $hour  = $4;
 
177
        $min   = $5;
 
178
        $sec   = $7;
 
179
    }
 
180
    else {
 
181
        warn "Date/Time format ($time) unrecogonzied";
 
182
    }
 
183
 
 
184
    if (defined $year) {
 
185
        $time = "$year-$month-$day $hour:$min";
 
186
        if (defined $sec) {
 
187
            $time .= ":$sec";
 
188
        }
 
189
        $time .= " " . &::Param('timezone') if &::Param('timezone');
 
190
    }
 
191
    return $time;
 
192
}
 
193
 
 
194
1;
 
195
 
 
196
__END__
 
197
 
 
198
=head1 NAME
 
199
 
 
200
Bugzilla::Util - Generic utility functions for bugzilla
 
201
 
 
202
=head1 SYNOPSIS
 
203
 
 
204
  use Bugzilla::Util;
 
205
 
 
206
  # Functions for dealing with variable tainting
 
207
  $rv = is_tainted($var);
 
208
  trick_taint($var);
 
209
  detaint_natural($var);
 
210
  detaint_signed($var);
 
211
 
 
212
  # Functions for quoting
 
213
  html_quote($var);
 
214
  url_quote($var);
 
215
  value_quote($var);
 
216
  xml_quote($var);
 
217
 
 
218
  # Functions for searching
 
219
  $loc = lsearch(\@arr, $val);
 
220
  $val = max($a, $b, $c);
 
221
  $val = min($a, $b, $c);
 
222
 
 
223
  # Functions for trimming variables
 
224
  $val = trim(" abc ");
 
225
 
 
226
  # Functions for formatting time
 
227
  format_time($time);
 
228
 
 
229
=head1 DESCRIPTION
 
230
 
 
231
This package contains various utility functions which do not belong anywhere
 
232
else.
 
233
 
 
234
B<It is not intended as a general dumping group for something which
 
235
people feel might be useful somewhere, someday>. Do not add methods to this
 
236
package unless it is intended to be used for a significant number of files,
 
237
and it does not belong anywhere else.
 
238
 
 
239
=head1 FUNCTIONS
 
240
 
 
241
This package provides several types of routines:
 
242
 
 
243
=head2 Tainting
 
244
 
 
245
Several functions are available to deal with tainted variables. B<Use these
 
246
with care> to avoid security holes.
 
247
 
 
248
=over 4
 
249
 
 
250
=item C<is_tainted>
 
251
 
 
252
Determines whether a particular variable is tainted
 
253
 
 
254
=item C<trick_taint($val)>
 
255
 
 
256
Tricks perl into untainting a particular variable.
 
257
 
 
258
Use trick_taint() when you know that there is no way that the data
 
259
in a scalar can be tainted, but taint mode still bails on it.
 
260
 
 
261
B<WARNING!! Using this routine on data that really could be tainted defeats
 
262
the purpose of taint mode.  It should only be used on variables that have been
 
263
sanity checked in some way and have been determined to be OK.>
 
264
 
 
265
=item C<detaint_natural($num)>
 
266
 
 
267
This routine detaints a natural number. It returns a true value if the
 
268
value passed in was a valid natural number, else it returns false. You
 
269
B<MUST> check the result of this routine to avoid security holes.
 
270
 
 
271
=item C<detaint_signed($num)>
 
272
 
 
273
This routine detaints a signed integer. It returns a true value if the
 
274
value passed in was a valid signed integer, else it returns false. You
 
275
B<MUST> check the result of this routine to avoid security holes.
 
276
 
 
277
=back
 
278
 
 
279
=head2 Quoting
 
280
 
 
281
Some values may need to be quoted from perl. However, this should in general
 
282
be done in the template where possible.
 
283
 
 
284
=over 4
 
285
 
 
286
=item C<html_quote($val)>
 
287
 
 
288
Returns a value quoted for use in HTML, with &, E<lt>, E<gt>, and E<34> being
 
289
replaced with their appropriate HTML entities.
 
290
 
 
291
=item C<url_quote($val)>
 
292
 
 
293
Quotes characters so that they may be included as part of a url.
 
294
 
 
295
=item C<css_class_quote($val)>
 
296
 
 
297
Quotes characters so that they may be used as CSS class names. Spaces
 
298
are replaced by underscores.
 
299
 
 
300
=item C<value_quote($val)>
 
301
 
 
302
As well as escaping html like C<html_quote>, this routine converts newlines
 
303
into &#013;, suitable for use in html attributes.
 
304
 
 
305
=item C<xml_quote($val)>
 
306
 
 
307
This is similar to C<html_quote>, except that ' is escaped to &apos;. This
 
308
is kept separate from html_quote partly for compatibility with previous code
 
309
(for &apos;) and partly for future handling of non-ASCII characters.
 
310
 
 
311
=back
 
312
 
 
313
=head2 Searching
 
314
 
 
315
Functions for searching within a set of values.
 
316
 
 
317
=over 4
 
318
 
 
319
=item C<lsearch($list, $item)>
 
320
 
 
321
Returns the position of C<$item> in C<$list>. C<$list> must be a list
 
322
reference.
 
323
 
 
324
If the item is not in the list, returns -1.
 
325
 
 
326
=item C<max($a, $b, ...)>
 
327
 
 
328
Returns the maximum from a set of values.
 
329
 
 
330
=item C<min($a, $b, ...)>
 
331
 
 
332
Returns the minimum from a set of values.
 
333
 
 
334
=back
 
335
 
 
336
=head2 Trimming
 
337
 
 
338
=over 4
 
339
 
 
340
=item C<trim($str)>
 
341
 
 
342
Removes any leading or trailing whitespace from a string. This routine does not
 
343
modify the existing string.
 
344
 
 
345
=back
 
346
 
 
347
=head2 Formatting Time
 
348
 
 
349
=over 4
 
350
 
 
351
=item C<format_time($time)>
 
352
 
 
353
Takes a time and appends the timezone as defined in editparams.cgi.  This routine
 
354
will be expanded in the future to adjust for user preferences regarding what
 
355
timezone to display times in.  In the future, it may also allow for the time to be
 
356
shown in different formats.
 
357
 
 
358
=back
 
359