Dpkg_Source_Patch.t 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/perl
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. use strict;
  16. use warnings;
  17. use Test::More tests => 9;
  18. use Test::Dpkg qw(:paths);
  19. use File::Path qw(make_path);
  20. BEGIN {
  21. use_ok('Dpkg::Source::Patch');
  22. }
  23. my $datadir = test_get_data_path('t/Dpkg_Source_Patch');
  24. my $tmpdir = 't.tmp/Dpkg_Source_Patch';
  25. sub test_patch_escape {
  26. my ($name, $symlink, $patchname, $desc) = @_;
  27. make_path("$tmpdir/$name-tree");
  28. make_path("$tmpdir/$name-out");
  29. symlink "../$name-out", "$tmpdir/$name-tree/$symlink";
  30. my $patch = Dpkg::Source::Patch->new(filename => "$datadir/$patchname");
  31. eval {
  32. $patch->apply("$tmpdir/$name-tree", verbose => 0);
  33. };
  34. ok(rmdir "$tmpdir/$name-out", $desc);
  35. }
  36. # This is CVE-2014-0471 with GNU patch >= 2.7
  37. test_patch_escape('c-style-parsed', "\tmp", 'c-style.patch',
  38. 'Patch cannot escape using known c-style encoded filename');
  39. # This is CVE-2014-0471 with GNU patch < 2.7
  40. test_patch_escape('c-style-unknown', '\\tmp', 'c-style.patch',
  41. 'Patch cannot escape using unknown c-style encoded filename');
  42. # This is CVE-2014-3865
  43. test_patch_escape('index-alone', 'symlink', 'index-alone.patch',
  44. 'Patch cannot escape using Index: w/o ---/+++ header');
  45. test_patch_escape('index-+++', 'symlink', 'index-+++.patch',
  46. 'Patch cannot escape using Index: w/ only +++ header');
  47. test_patch_escape('index-inert', 'symlink', 'index-inert.patch',
  48. 'Patch should not fail to apply using an inert Index:');
  49. ok(-e "$tmpdir/index-inert-tree/inert-file",
  50. 'Patch with inert Index: applies correctly');
  51. # This is CVE-2014-3864
  52. test_patch_escape('partial', 'symlink', 'partial.patch',
  53. 'Patch cannot escape using partial +++ header');
  54. test_patch_escape('ghost-hunk', 'symlink', 'ghost-hunk.patch',
  55. 'Patch cannot escape using a disabling hunk');
  56. 1;