QtPass
2.0.0
Developer documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
pathvalidator.cpp
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
8
#include "
pathvalidator.h
"
9
#include <QDir>
10
#include <QFileInfo>
11
#include <QStringList>
12
13
auto
PathValidator::isPathInStore
(
const
QString &storeRoot,
14
const
QString &candidate) ->
bool
{
15
return
storeRelativePath
(storeRoot, candidate).has_value();
16
}
17
18
auto
PathValidator::storeRelativePath
(
const
QString &storeRoot,
19
const
QString &candidate)
20
-> std::optional<QString> {
21
if
(storeRoot.isEmpty() || candidate.isEmpty()) {
22
return
std::nullopt;
23
}
24
const
QString rootCanon = QDir(storeRoot).canonicalPath();
25
if
(rootCanon.isEmpty()) {
26
return
std::nullopt;
27
}
28
29
QString resolved;
30
const
QFileInfo fi(QDir::cleanPath(QFileInfo(candidate).absoluteFilePath()));
31
if
(fi.exists()) {
32
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
QDir parent = fi.dir();
38
QStringList tail;
39
tail.prepend(fi.fileName());
40
while
(!parent.exists()) {
41
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
const
QString up = QFileInfo(parent.absolutePath()).path();
48
if
(up == parent.absolutePath()) {
49
return
std::nullopt;
50
}
51
parent.setPath(up);
52
}
53
const
QString parentCanon = parent.canonicalPath();
54
if
(parentCanon.isEmpty()) {
55
return
std::nullopt;
56
}
57
resolved = QDir::cleanPath(parentCanon + QLatin1Char(
'/'
) +
58
tail.join(QLatin1Char(
'/'
)));
59
}
60
61
if
(resolved.isEmpty()) {
62
return
std::nullopt;
63
}
64
if
(resolved == rootCanon) {
65
return
QString();
66
}
67
if
(!resolved.startsWith(rootCanon + QLatin1Char(
'/'
))) {
68
return
std::nullopt;
69
}
70
return
resolved.mid(rootCanon.size() + 1);
71
}
72
73
auto
PathValidator::isTargetSpelled
(
const
QString &storeRoot,
74
const
QString &path) ->
bool
{
75
if
(storeRoot.isEmpty() || !QDir::isAbsolutePath(path)) {
76
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
const
QString root = QDir::cleanPath(storeRoot);
84
const
QString canonical = QDir(root).canonicalPath();
85
if
(canonical.isEmpty() || canonical.compare(root, cs) == 0) {
86
return
false
;
87
}
88
const
auto
atOrUnder = [](
const
QString &p,
const
QString &dir) {
89
const
QString prefix =
90
dir.endsWith(QLatin1Char(
'/'
)) ? dir : dir + QLatin1Char(
'/'
);
91
return
p.compare(dir, cs) == 0 || p.startsWith(prefix, cs);
92
};
93
const
QString cleaned = QDir::cleanPath(path);
94
return
!atOrUnder(cleaned, root) && atOrUnder(cleaned, canonical);
95
}
PathValidator::storeRelativePath
static auto storeRelativePath(const QString &storeRoot, const QString &candidate) -> std::optional< QString >
Where candidate lies in the store, judged as isPathInStore() judges it.
Definition
pathvalidator.cpp:18
PathValidator::isPathInStore
static auto isPathInStore(const QString &storeRoot, const QString &candidate) -> bool
Check that a candidate path resolves inside the password store root, after following symlinks and res...
Definition
pathvalidator.cpp:13
PathValidator::isTargetSpelled
static auto isTargetSpelled(const QString &storeRoot, const QString &path) -> bool
Whether path is spelled through the target of a store root that is itself a link, rather than from th...
Definition
pathvalidator.cpp:73
pathvalidator.h
PathValidator: is a path inside the password store, and how is it spelled relative to the store root.
src
pathvalidator.cpp
Generated by
1.18.0