Skip to content

Commit

Permalink
Fixed xml external entity (XEE) injection bug
Browse files Browse the repository at this point in the history
reported by @phvietan, fixes issue #10

also:
  - added unit test for support of purl.oclc.org namespace ... see issue #7
  - removed tidyness tests again as it fails on weaver'ed code
  • Loading branch information
MichaelDaum committed Jan 17, 2024
1 parent 409782c commit 1d55f90
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
1 change: 1 addition & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
-sot
-nbbb
-nbbc
-msc=1
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Revision history for Spreadsheet-ParseXLSX

{{$NEXT}}

- Fix xml external entity (XEE) injection bug
- added unit test for support of purl.oclc.org namespace ... see issue #7
- removed tidyness tests again as it fails on weaver'ed code

0.29 2024-01-02

- Merge pull request #1 from theevilapplepie/master: Fix for 'Argument "" isn't numeric in addition (+) at /usr/local/shar…
Expand Down
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ critic_config = .perlcriticrc
[PodCoverageTests]
[PodSyntaxTests]
[MetaResources]
[GitHub::Meta]
[Git::Check]
[Git::Commit]
commit_msg = changelog
Expand Down
44 changes: 24 additions & 20 deletions lib/Spreadsheet/ParseXLSX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ sub parse {
}

if (openhandle($file)) {
bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
my $fh =
ref($file) eq 'File::Temp'
? IO::File->new("<&=" . fileno($file))
Expand Down Expand Up @@ -112,7 +112,7 @@ sub _check_signature {

my $signature = '';
if (openhandle($file)) {
bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh
$file->read($signature, 2);
$file->seek(-2, IO::File::SEEK_CUR);
} elsif (ref($file) eq 'SCALAR') {
Expand Down Expand Up @@ -149,7 +149,7 @@ sub _parse_workbook {

$workbook->{FmtClass} = $formatter || Spreadsheet::ParseExcel::FmtDefault->new;

my $themes = $self->_parse_themes((values %{$files->{themes}})[0]); # XXX
my $themes = $self->_parse_themes((values %{$files->{themes}})[0]); # XXX

$workbook->{Color} = $themes->{Color};

Expand Down Expand Up @@ -400,7 +400,7 @@ sub _parse_sheet {
} elsif ($type eq 'str' || $type eq 'inlineStr') {
$long_type = 'Text';
} else {
die "unimplemented type $type"; # XXX
die "unimplemented type $type"; # XXX
}

my $format_idx = $cell->att('s') || 0;
Expand All @@ -423,7 +423,7 @@ sub _parse_sheet {
my $cell = Spreadsheet::ParseExcel::Cell->new(
Val => $val,
Type => $long_type,
Merged => undef, # fix up later
Merged => undef, # fix up later
Format => $format,
FormatNo => $format_idx,
(
Expand Down Expand Up @@ -525,26 +525,26 @@ sub _parse_sheet_links {

# Add the hyperlink
$cell->{Hyperlink} = [
$hyperlink->att('display') || $cell->{_Value} || undef, # Description
$destination_url, # Target
undef, # Target Frame
$row, # Start Row
$row, # End Row
$col, # Start Column
$col, # End Column
$hyperlink->att('display') || $cell->{_Value} || undef, # Description
$destination_url, # Target
undef, # Target Frame
$row, # Start Row
$row, # End Row
$col, # Start Column
$col, # End Column
];
} else {
# This is an internal hyperlink

# Add the hyperlink
$cell->{Hyperlink} = [
$hyperlink->att('display') || $cell->{_Value} || undef, # Description
$hyperlink->att('location'), # Target
undef, # Target Frame
$row, # Start Row
$row, # End Row
$col, # Start Column
$col, # End Column
$hyperlink->att('display') || $cell->{_Value} || undef, # Description
$hyperlink->att('location'), # Target
undef, # Target Frame
$row, # Start Row
$row, # End Row
$col, # Start Column
$col, # End Column
];
}

Expand Down Expand Up @@ -957,7 +957,10 @@ sub _extract_files {
my $type_base = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';

my $rels = $self->_parse_xml($zip, $self->_rels_for(''),);
my $wb_name = ($rels->find_nodes(qq<//packagerels:Relationship[\@Type="$type_base/officeDocument"]>))[0]->att('Target');
my $node = ($rels->find_nodes(qq<//packagerels:Relationship[\@Type="$type_base/officeDocument"]>))[0];
die "invalid workbook" unless $node;

my $wb_name = $node->att('Target');
$wb_name =~ s{^/}{};
my $wb_xml = $self->_parse_xml($zip, $wb_name);

Expand Down Expand Up @@ -1175,6 +1178,7 @@ sub _new_twig {
'http://schemas.openxmlformats.org/officeDocument/2006/relationships' => 'rels',
'http://schemas.openxmlformats.org/drawingml/2006/main' => 'drawmain',
},
no_xxe => 1,
keep_original_prefix => 1,
%opts,
);
Expand Down
21 changes: 21 additions & 0 deletions t/bug-md-10.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

use Spreadsheet::ParseXLSX;

my $wb;
eval {
$wb = Spreadsheet::ParseXLSX->new->parse('t/data/bug-md-10.xlsx');
};

ok(!defined($wb));
ok($@);
ok($@ =~ /^cannot expand &xxe/);

done_testing;



16 changes: 14 additions & 2 deletions t/bug-md-7.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ use Test::More;

use Spreadsheet::ParseXLSX;

my $wb = Spreadsheet::ParseXLSX->new->parse('t/data/bug-md-7.xlsx');
isa_ok($wb, 'Spreadsheet::ParseExcel::Workbook');
my $wb;

eval {
$wb = Spreadsheet::ParseXLSX->new->parse('t/data/bug-md-7.xlsx');
};

# activate this when #7 is fixed
if (0) {
isa_ok($wb, 'Spreadsheet::ParseExcel::Workbook');
} else {
ok(!defined($wb));
ok($@);
ok($@ =~ /^invalid workbook/);
}

done_testing;

Expand Down
Binary file added t/data/bug-md-10.xlsx
Binary file not shown.
18 changes: 0 additions & 18 deletions t/perltidy.t

This file was deleted.

0 comments on commit 1d55f90

Please sign in to comment.