Skip to content

Commit

Permalink
Merge pull request #14 from perl5-dbi/dbd_file-nodir
Browse files Browse the repository at this point in the history
DBD-File prevent non-existing folder in f_dir on connect RT 99508
  • Loading branch information
timbunce committed Oct 23, 2014
2 parents 06787b3 + 222f720 commit 42ee410
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -16,6 +16,9 @@ DBI::Changes - List of significant changes to the DBI
Fixed DBI::DBD::SqlEngine to complain loudly when prerequite
driver_prefix is not fulfilled (RT#93204) [Jens Rehsack]
Fixed redundant sprintf argument warning RT#97062 [Reini Urban]
Fixed security issue where DBD::File drivers would open files
from folders other than specifically passed using the
f_dir attribute RT#99508 [H.Merijn Brand]

Changed delete $h->{$key} to work for keys with 'private_' prefix
per request in RT#83156. local $h->{$key} works as before.
Expand Down
35 changes: 33 additions & 2 deletions lib/DBD/File.pm
Expand Up @@ -35,7 +35,7 @@ use base qw( DBI::DBD::SqlEngine );
use Carp;
use vars qw( @ISA $VERSION $drh );

$VERSION = "0.42";
$VERSION = "0.43";

$drh = undef; # holds driver handle(s) once initialized

Expand Down Expand Up @@ -85,6 +85,8 @@ use warnings;

use vars qw( @ISA $imp_data_size );

use Carp;

@DBD::File::dr::ISA = qw( DBI::DBD::SqlEngine::dr );
$DBD::File::dr::imp_data_size = 0;

Expand All @@ -100,6 +102,31 @@ sub dsn_quote
# XXX rewrite using TableConfig ...
sub default_table_source { "DBD::File::TableSource::FileSystem" }

sub connect
{
my ($drh, $dbname, $user, $auth, $attr) = @_;

# We do not (yet) care about conflicting attributes here
# my $dbh = DBI->connect ("dbi:CSV:f_dir=test", undef, undef, { f_dir => "text" });
# will test here that both test and text should exist
if (my $attr_hash = (DBI->parse_dsn ($dbname))[3]) {
if (defined $attr_hash->{f_dir} && ! -d $attr_hash->{f_dir}) {
my $msg = "No such directory '$attr_hash->{f_dir}";
$drh->set_err (2, $msg);
$attr_hash->{RaiseError} and croak $msg;
return;
}
}
if ($attr and defined $attr->{f_dir} && ! -d $attr->{f_dir}) {
my $msg = "No such directory '$attr->{f_dir}";
$drh->set_err (2, $msg);
$attr->{RaiseError} and croak $msg;
return;
}

return $drh->SUPER::connect ($dbname, $user, $auth, $attr);
} # connect

sub disconnect_all
{
} # disconnect_all
Expand Down Expand Up @@ -130,7 +157,7 @@ sub data_sources
{
my ($dbh, $attr, @other) = @_;
ref ($attr) eq "HASH" or $attr = {};
exists $attr->{f_dir} or $attr->{f_dir} = $dbh->{f_dir};
exists $attr->{f_dir} or $attr->{f_dir} = $dbh->{f_dir};
exists $attr->{f_dir_search} or $attr->{f_dir_search} = $dbh->{f_dir_search};
return $dbh->SUPER::data_sources ($attr, @other);
} # data_source
Expand Down Expand Up @@ -343,6 +370,10 @@ sub data_sources
? $attr->{f_dir}
: File::Spec->curdir ();
defined $dir or return; # Stream-based databases do not have f_dir
unless (-d $dir && -r $dir && -x $dir) {
$drh->set_err ($DBI::stderr, "Cannot use directory $dir from f_dir");
return;
}
my %attrs;
$attr and %attrs = %$attr;
delete $attrs{f_dir};
Expand Down
25 changes: 25 additions & 0 deletions t/49dbd_file.t
Expand Up @@ -207,6 +207,31 @@ ok ($dbh = DBI->connect ("dbi:File:", undef, undef, {
ok ($dbh->do ("drop table $tbl"), "table drop");
is (-s $tbl_file, undef, "Test table removed"); # -s => size test

# ==================== Nonexisting top-dir ========================
my %drh = DBI->installed_drivers;
my $qer = qr{\bNo such directory};
foreach my $tld ("./non-existing", "nonexisting_folder", "/Fr-dle/hurd0k/ok$$") {
is (DBI->connect ("dbi:File:", undef, undef, {
f_dir => $tld,

RaiseError => 0,
PrintError => 0,
}), undef, "Should not be able to open a DB to $tld");
like ($DBI::errstr, $qer, "Error message");
$drh{File}->set_err (undef, "");
is ($DBI::errstr, undef, "Cleared error");
my $dbh;
eval { $dbh = DBI->connect ("dbi:File:", undef, undef, {
f_dir => $tld,

RaiseError => 1,
PrintError => 0,
})};
is ($dbh, undef, "connect () should die on $tld with RaiseError");
like ($@, $qer, "croak message");
like ($DBI::errstr, $qer, "Error message");
}

done_testing ();

sub DBD::File::Table::fetch_row ($$)
Expand Down

0 comments on commit 42ee410

Please sign in to comment.