• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 43610178115

14 Nov 2026 03:26PM UTC coverage: 96.333% (+0.008%) from 96.325%
43610178115

Pull #2163

github

web-flow
Merge edab2033b into 894b79c9b
Pull Request #2163: Add password and Add folder: refuse hidden names and places behind links

20 of 20 new or added lines in 3 files covered. (100.0%)

1 existing line in 1 file now uncovered.

8380 of 8699 relevant lines covered (96.33%)

470.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.62
/src/pathvalidator.cpp
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
auto PathValidator::isPathInStore(const QString &storeRoot,
81 ✔
14
                                  const QString &candidate) -> bool {
15
  return storeRelativePath(storeRoot, candidate).has_value();
81 ✔
16
}
17

18
auto PathValidator::storeRelativePath(const QString &storeRoot,
136 ✔
19
                                      const QString &candidate)
20
    -> std::optional<QString> {
21
  if (storeRoot.isEmpty() || candidate.isEmpty()) {
136 ✔
22
    return std::nullopt;
3 ✔
23
  }
24
  const QString rootCanon = QDir(storeRoot).canonicalPath();
133 ✔
25
  if (rootCanon.isEmpty()) {
133 ✔
26
    return std::nullopt;
1 ✔
27
  }
28

29
  QString resolved;
132 ✔
30
  const QFileInfo fi(QDir::cleanPath(QFileInfo(candidate).absoluteFilePath()));
264 ✔
31
  if (fi.exists()) {
132 ✔
32
    resolved = fi.canonicalFilePath();
134 ✔
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();
65 ✔
38
    QStringList tail;
65 ✔
39
    tail.prepend(fi.fileName());
65 ✔
40
    while (!parent.exists()) {
80 ✔
41
      tail.prepend(parent.dirName());
15 ✔
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();
15 ✔
48
      if (up == parent.absolutePath()) {
30 ✔
UNCOV
49
        return std::nullopt;
×
50
      }
51
      parent.setPath(up);
15 ✔
52
    }
53
    const QString parentCanon = parent.canonicalPath();
65 ✔
54
    if (parentCanon.isEmpty()) {
65 ✔
55
      return std::nullopt;
×
56
    }
57
    resolved = QDir::cleanPath(parentCanon + QLatin1Char('/') +
130 ✔
58
                               tail.join(QLatin1Char('/')));
130 ✔
59
  }
65 ✔
60

61
  if (resolved.isEmpty()) {
132 ✔
62
    return std::nullopt;
×
63
  }
64
  if (resolved == rootCanon) {
132 ✔
65
    return QString();
31 ✔
66
  }
67
  if (!resolved.startsWith(rootCanon + QLatin1Char('/'))) {
202 ✔
68
    return std::nullopt;
23 ✔
69
  }
70
  return resolved.mid(rootCanon.size() + 1);
156 ✔
71
}
132 ✔
72

73
auto PathValidator::isTargetSpelled(const QString &storeRoot,
300 ✔
74
                                    const QString &path) -> bool {
75
  if (storeRoot.isEmpty() || !QDir::isAbsolutePath(path)) {
600 ✔
76
    return false;
2 ✔
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);
298 ✔
84
  const QString canonical = QDir(root).canonicalPath();
298 ✔
85
  if (canonical.isEmpty() || canonical.compare(root, cs) == 0) {
298 ✔
86
    return false;
291 ✔
87
  }
88
  const auto atOrUnder = [cs](const QString &p, const QString &dir) {
12 ✔
89
    const QString prefix =
90
        dir.endsWith(QLatin1Char('/')) ? dir : dir + QLatin1Char('/');
12 ✔
91
    return p.compare(dir, cs) == 0 || p.startsWith(prefix, cs);
24 ✔
92
  };
93
  const QString cleaned = QDir::cleanPath(path);
7 ✔
94
  return !atOrUnder(cleaned, root) && atOrUnder(cleaned, canonical);
7 ✔
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc