Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements PathValidator; storeRelativePath() does the canonical
6 : : * comparison the other checks build on.
7 : : */
8 : : #include "pathvalidator.h"
9 : : #include <QDir>
10 : : #include <QFileInfo>
11 : : #include <QStringList>
12 : :
13 : 169 : auto PathValidator::isPathInStore(const QString &storeRoot,
14 : : const QString &candidate) -> bool {
15 : 169 : return storeRelativePath(storeRoot, candidate).has_value();
16 : : }
17 : :
18 : 281 : auto PathValidator::storeRelativePath(const QString &storeRoot,
19 : : const QString &candidate)
20 : : -> std::optional<QString> {
21 [ + + + + ]: 281 : if (storeRoot.isEmpty() || candidate.isEmpty()) {
22 : 3 : return std::nullopt;
23 : : }
24 : 278 : const QString rootCanon = QDir(storeRoot).canonicalPath();
25 [ + + ]: 278 : if (rootCanon.isEmpty()) {
26 : 1 : return std::nullopt;
27 : : }
28 : :
29 : 277 : QString resolved;
30 : 554 : const QFileInfo fi(QDir::cleanPath(QFileInfo(candidate).absoluteFilePath()));
31 [ + + + ]: 277 : if (fi.exists()) {
32 : 322 : resolved = fi.canonicalFilePath();
33 : : } else {
34 : : // Walk up to the nearest existing ancestor so we can canonicalise it
35 : : // (resolves any symlinks on the way to the store root), then re-append
36 : : // the leaf components below that ancestor.
37 : 116 : QDir parent = fi.dir();
38 : 116 : QStringList tail;
39 : 116 : tail.prepend(fi.fileName());
40 [ + + + ]: 1133 : while (!parent.exists()) {
41 : 1017 : tail.prepend(parent.dirName());
42 : : // Up by name, not cdUp(): cdUp() refuses to move into a folder that
43 : : // does not exist either, so a new entry two missing folders deep
44 : : // ("a/b/entry") used to count as outside the store. The filesystem
45 : : // root always exists, so the loop ends there; the guard is defensive
46 : : // against a path that can neither exist nor ascend.
47 : 1017 : const QString up = QFileInfo(parent.absolutePath()).path();
48 [ + - + ]: 2034 : if (up == parent.absolutePath()) {
49 : 0 : return std::nullopt;
50 : : }
51 : 1017 : parent.setPath(up);
52 : : }
53 : 116 : const QString parentCanon = parent.canonicalPath();
54 [ - + ]: 116 : if (parentCanon.isEmpty()) {
55 : 0 : return std::nullopt;
56 : : }
57 : 232 : resolved = QDir::cleanPath(parentCanon + QLatin1Char('/') +
58 [ + + ]: 232 : tail.join(QLatin1Char('/')));
59 : 116 : }
60 : :
61 [ - + ]: 277 : if (resolved.isEmpty()) {
62 : 0 : return std::nullopt;
63 : : }
64 [ + + ]: 277 : if (resolved == rootCanon) {
65 : 50 : return QString();
66 : : }
67 [ + + + + ]: 454 : if (!resolved.startsWith(rootCanon + QLatin1Char('/'))) {
68 : 23 : return std::nullopt;
69 : : }
70 : 408 : return resolved.mid(rootCanon.size() + 1);
71 : 277 : }
72 : :
73 : 604 : auto PathValidator::isTargetSpelled(const QString &storeRoot,
74 : : const QString &path) -> bool {
75 [ + - + + ]: 1208 : if (storeRoot.isEmpty() || !QDir::isAbsolutePath(path)) {
76 : 2 : return false;
77 : : }
78 : : #ifdef Q_OS_WIN
79 : : constexpr auto cs = Qt::CaseInsensitive;
80 : : #else
81 : : constexpr auto cs = Qt::CaseSensitive;
82 : : #endif
83 : 602 : const QString root = QDir::cleanPath(storeRoot);
84 : 602 : const QString canonical = QDir(root).canonicalPath();
85 [ + - + + ]: 602 : if (canonical.isEmpty() || canonical.compare(root, cs) == 0) {
86 : 595 : return false;
87 : : }
88 : 12 : const auto atOrUnder = [](const QString &p, const QString &dir) {
89 : : const QString prefix =
90 [ - + ]: 12 : dir.endsWith(QLatin1Char('/')) ? dir : dir + QLatin1Char('/');
91 [ + + + + : 24 : return p.compare(dir, cs) == 0 || p.startsWith(prefix, cs);
+ ]
92 : : };
93 : 7 : const QString cleaned = QDir::cleanPath(path);
94 [ + + + + : 7 : return !atOrUnder(cleaned, root) && atOrUnder(cleaned, canonical);
+ + ]
95 : : }
|