Policy.pm

filter: Statementstmt 100% 31 / 31 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 2 totaltotal 88.8% 40 / 45 CC 5 SCAR 11.0
CC
5
Cov
95%
import
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
↑ Summary Policy/Default.pm »
1 ###############################################################################
2 # Purpose : Interface to File I/O policies
3 # Author : John Alden
4 # Created : March 2005
5 # CVS : $Id: Policy.pm,v 1.5 2005/05/18 15:58:21 johna Exp $
6 ###############################################################################
7  
8 package File::Policy;
9  
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use vars qw($VERSION $Aliased);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 $VERSION = sprintf"%d.%03d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
14 3 $Aliased = 0;
15  
16 5 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
17 5 my $package = shift;
18
19 #Allow default implementation to be set in a config module
20 5 my $default_implementation = 'Default';
21 5 eval {
22 5 require File::Policy::Config;
23 2 $default_implementation = &File::Policy::Config::IMPLEMENTATION;
24 };
25
26 5 my $impl = $default_implementation; #Currently no way of overriding default - could be extended later
27 5 ▶ die("Invalid File::Policy package - $impl") unless($impl =~ /^[\w:]+$/); #Sanitize package name
Branch: 1 branch
BranchTrueFalseTotal
unless $impl =~ /^[\w:]+$/ 0 5 5
28 5 $impl = "File::Policy::" . $impl;
29 5 TRACE("File::Policy Implementation: ". $impl);
30
31 #Export symbols to caller
32 5 eval "require $impl";
33 5 ▶ die("Unable to compile $impl - $@") if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
34 5 ++ local $Exporter::ExportLevel; #Bypass this module in caller()
35 5 $impl->import(@_);
36
37 #Alias symbols into this package too for fully qualified access
38 5 ▶ unless($Aliased) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($File::Policy::Aliased) 3 2 5
39 3 foreach(qw(check_safe get_log_dir get_temp_dir)) {
40 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 9 *{"File::Policy::$_"} = *{"${impl}::$_"};
42 }
43 3 $Aliased = 1;
44 }
45 }
46  
47 #Stubs compatible with Log::Trace
48 5 ▶sub TRACE{}
Subroutine TRACE: called CC=1 SCAR=0.0
Pod undocumented
49 0 ▶sub DUMP{}
Subroutine DUMP: not called CC=1 SCAR=0.0
Pod undocumented
50  
51 3 1;
52  
53 =head1 NAME
54  
55 File::Policy - Site policy for file I/O functions
56  
57 =head1 SYNOPSIS
58  
59 use File::Policy;
60 use File::Policy qw/check_safe/; # to import a specific subroutine
61 use File::Policy qw/:all/; # to import all subroutines
62  
63 #Checking I/O policy
64 check_safe($filename, 'r');
65 check_safe($filename, 'w');
66  
67 #Preferred directory locations
68 $logdir = get_log_dir();
69 $tmpdir = get_temp_dir();
70  
71 =head1 DESCRIPTION
72  
73 This defines the policy for file I/O with modules such as File::Slurp::WithinPolicy.
74 The purpose is to allow systems administrators to define locations and restrictions
75 for applications' file I/O and give app developers a policy to follow. Note that the
76 module doesn't ENFORCE the policy - application developers can choose to ignore it
77 (and systems administrators can choose not to install their applications if they do!).
78  
79 You may control which policy gets applied by creating a File::Policy::Config module
80 with an IMPLEMENTATION constant. You may write your own policy as a module within the File::Policy:: namespace.
81  
82 By default (if no File::Policy::Config is present), the File::Policy::Default policy gets applied which doesn't impose
83 any restrictions and provides reasonable default locations for temporary and log files.
84  
85 The motivation behind this module was a standard, flexible approach to allow a site wide file policy to be defined. This will be most useful in large environments where a few sysadmins are responsible for code written by many other people. Simply ensuring that submitted code calls check_safe() ensures file access is sane, reducing the amount of effort required to do a security audit.
86  
87 If your code is not security audit'd, or you are the only developer at your site, this might be overkill. However you may consider it good practise regardless and protection against paths in your code getting corrupted accidently or maliciously in the future.
88  
89 There are two major benefits of using this module. One, sites that do implement a policy can more easily integrate your code in a standard way. If you have a file policy at your site, you can apply different policies (via File::Policy::Config) in different environments (production, integration test, development) and the appropriate policy is automatically applied without having to change your code or configs.
90  
91 =head1 FUNCTIONS
92  
93 =over 4
94  
95 =item check_safe
96  
97 check_safe( FILENAME , MODE );
98  
99 Checks FILENAME is safe - dies if not. MODE is r (read) or w (write).
100  
101 =item get_temp_dir
102  
103 $temporary_directory = get_temp_dir();
104  
105 Returns the path to temporary directory. Note that any return value will have been cleared of a trailing slash.
106  
107 =item get_log_dir
108  
109 $log_directory = get_log_dir();
110  
111 Returns the path to log directory. Note that any return value will have been cleared of a trailing slash.
112  
113 =back
114  
115 =head1 DEFINING YOUR OWN POLICY
116  
117 To implement your own custom policy
118  
119 cp File/Policy/Default.pm File/Policy/YourPolicy.pm
120
121 and modify YourPolicy accordingly. Then, create File/Policy/Config.pm contaning:
122  
123 use constant IMPLEMENTATION => 'YourPolicy';
124  
125 Now having used File::Policy, calling check_safe in your scripts will enforce your policy
126 (as well as give you access to log and temp paths in locations you recommend).
127  
128 =head1 SEE ALSO
129  
130 L<File::Policy::Default>, L<Safe>
131  
132 =head1 VERSION
133  
134 $Revision: 1.5 $ on $Date: 2005/05/18 15:58:21 $ by $Author: johna $
135  
136 =head1 AUTHOR
137  
138 John Alden <cpan _at_ bbc _dot_ co _dot_ uk>
139  
140 =head1 COPYRIGHT
141  
142 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
143  
144 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
145  
146 =cut
↑ Summary Policy/Default.pm »