diff options
author | Mike Frysinger <vapier@google.com> | 2019-07-31 23:32:58 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2020-02-04 20:34:01 +0000 |
commit | 04122b7261319dae3abcaf0eb63af7ed937dc463 (patch) | |
tree | 4e8092cae702cd7b667b4cd95f1cfc5dbba221f3 /tests/test_manifest_xml.py | |
parent | f5525fb310f0aae2783d9ccf647cac967efb2600 (diff) | |
download | git-repo-04122b7261319dae3abcaf0eb63af7ed937dc463.tar.gz |
manifest: add basic path checks for <copyfile> & <linkfile>
Reject paths in <copyfile> & <linkfile> that point outside of their
respective scopes. This validates paths while parsing the manifest
as this should be quick & cheap: we don't access the filesystem as
this code runs before we've synced.
Bug: https://crbug.com/gerrit/11218
Change-Id: I8e17bb91f3f5b905a9d76391b29fbab4cb77aa58
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/232932
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Diffstat (limited to 'tests/test_manifest_xml.py')
-rw-r--r-- | tests/test_manifest_xml.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py new file mode 100644 index 00000000..ecc84ad7 --- /dev/null +++ b/tests/test_manifest_xml.py | |||
@@ -0,0 +1,83 @@ | |||
1 | # -*- coding:utf-8 -*- | ||
2 | # | ||
3 | # Copyright (C) 2019 The Android Open Source Project | ||
4 | # | ||
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | # you may not use this file except in compliance with the License. | ||
7 | # You may obtain a copy of the License at | ||
8 | # | ||
9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | # | ||
11 | # Unless required by applicable law or agreed to in writing, software | ||
12 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | # See the License for the specific language governing permissions and | ||
15 | # limitations under the License. | ||
16 | |||
17 | """Unittests for the manifest_xml.py module.""" | ||
18 | |||
19 | from __future__ import print_function | ||
20 | |||
21 | import unittest | ||
22 | |||
23 | import error | ||
24 | import manifest_xml | ||
25 | |||
26 | |||
27 | class ManifestValidateFilePaths(unittest.TestCase): | ||
28 | """Check _ValidateFilePaths helper. | ||
29 | |||
30 | This doesn't access a real filesystem. | ||
31 | """ | ||
32 | |||
33 | def check_both(self, *args): | ||
34 | manifest_xml.XmlManifest._ValidateFilePaths('copyfile', *args) | ||
35 | manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args) | ||
36 | |||
37 | def test_normal_path(self): | ||
38 | """Make sure good paths are accepted.""" | ||
39 | self.check_both('foo', 'bar') | ||
40 | self.check_both('foo/bar', 'bar') | ||
41 | self.check_both('foo', 'bar/bar') | ||
42 | self.check_both('foo/bar', 'bar/bar') | ||
43 | |||
44 | def test_symlink_targets(self): | ||
45 | """Some extra checks for symlinks.""" | ||
46 | def check(*args): | ||
47 | manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args) | ||
48 | |||
49 | # We allow symlinks to end in a slash since we allow them to point to dirs | ||
50 | # in general. Technically the slash isn't necessary. | ||
51 | check('foo/', 'bar') | ||
52 | |||
53 | def test_bad_paths(self): | ||
54 | """Make sure bad paths (src & dest) are rejected.""" | ||
55 | PATHS = ( | ||
56 | '..', | ||
57 | '../', | ||
58 | './', | ||
59 | 'foo/', | ||
60 | './foo', | ||
61 | '../foo', | ||
62 | 'foo/./bar', | ||
63 | 'foo/../../bar', | ||
64 | '/foo', | ||
65 | './../foo', | ||
66 | '.git/foo', | ||
67 | # Check case folding. | ||
68 | '.GIT/foo', | ||
69 | 'blah/.git/foo', | ||
70 | '.repo/foo', | ||
71 | '.repoconfig', | ||
72 | # Block ~ due to 8.3 filenames on Windows filesystems. | ||
73 | '~', | ||
74 | 'foo~', | ||
75 | 'blah/foo~', | ||
76 | # Block Unicode characters that get normalized out by filesystems. | ||
77 | u'foo\u200Cbar', | ||
78 | ) | ||
79 | for path in PATHS: | ||
80 | self.assertRaises( | ||
81 | error.ManifestInvalidPathError, self.check_both, path, 'a') | ||
82 | self.assertRaises( | ||
83 | error.ManifestInvalidPathError, self.check_both, 'a', path) | ||