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

IJHack / QtPass / 45639919625

25 Nov 2026 02:58PM UTC coverage: 94.813% (+0.03%) from 94.781%
45639919625

push

github

web-flow
Signed lists: verify a small .gpg-id.sig, and stop at the first bad one (#2336)

GpgIdSigner::verify() accepted a .gpg-id.sig of up to 64 KiB and ran
gpg --verify without --batch, on the GUI thread. gpg checks every
signature packet in the file against the key it names: someone who can
write to the store, with a key built to be slow (a huge public
exponent) in the victim's keyring, froze QtPass for minutes on each
save or Users open.

The cap is now 4 KiB: a detached signature is well under 1 KiB, one by
a 16384-bit RSA key about 3 KiB armored, so two slow signatures fit,
not dozens. --batch makes gpg stop at the first bad signature. The
signature is read once with Util::readRegularFile() (regular, bounded,
opened without blocking) and gpg checks a copy of those bytes in a
scratch folder, so a FIFO or anything else put under the name between
the check and gpg opening it never reaches gpg.

Scan: ANT-2026-XQUAU1HG

Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

14 of 16 new or added lines in 1 file covered. (87.5%)

10036 of 10585 relevant lines covered (94.81%)

21801.68 hits per line

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

93.33
/src/gpgidsigner.cpp
1
// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
/**
4
 * @file
5
 * @brief Implements GpgIdSigner on top of gpg's `--status-fd` output.
6
 */
7
#include "gpgidsigner.h"
8

9
#include "executor.h"
10
#include "util.h"
11
#include <QCoreApplication>
12
#include <QFile>
13
#include <QRegularExpression>
14
#include <QTemporaryDir>
15
#include <algorithm>
16
#include <utility>
17

18
#include "qtpasslogging.h"
19

20
GpgIdSigner::GpgIdSigner(QString gpgExecutable, QStringList signingKeys,
675 ✔
21
                         Exec exec)
675 ✔
22
    : m_gpg(std::move(gpgExecutable)), m_keys(std::move(signingKeys)),
23
      m_exec(std::move(exec)) {
24
  if (!m_exec) {
675 ✔
25
    m_exec = [](const QString &app, const QStringList &args,
81 ✔
26
                const QString &input, QString *out, QString *err) {
27
      return Executor::executeBlocking(app, args, input, out, err);
9 ✔
28
    };
81 ✔
29
  }
30
}
675 ✔
31

32
auto GpgIdSigner::keysFromSetting(const QString &passSigningKey)
662 ✔
33
    -> QStringList {
34
  QStringList keys = passSigningKey.split(QLatin1Char(' '), Qt::SkipEmptyParts);
662 ✔
35
  // gpg prints fingerprints in upper case in its status lines; accept them
36
  // typed either way.
37
  for (QString &key : keys) {
1,505 ✔
38
    key = key.toUpper();
181 ✔
39
  }
40
  return keys;
662 ✔
41
}
42

43
auto GpgIdSigner::run(const QStringList &args, QString *out, QString *err,
217 ✔
44
                      const QString &input) const -> int {
45
  return m_exec(m_gpg, args, input, out, err);
217 ✔
46
}
47

48
auto GpgIdSigner::haveSecretKey() const -> bool {
52 ✔
49
  if (!enabled()) {
52 ✔
50
    return false;
51
  }
52
  // Only the first key signs (see sign()), so only its secret key matters:
53
  // a usable second key would let Init start and fail at the signature.
54
  const QString &key = m_keys.first();
55
  QString out;
51 ✔
56
  const int rc =
57
      run({QStringLiteral("--with-colons"), QStringLiteral("--fixed-list-mode"),
408 ✔
58
           QStringLiteral("--list-secret-keys"), QStringLiteral("--"), key},
51 ✔
59
          &out);
60
  if (rc != 0) {
51 ✔
61
    qCDebug(lcQtPass) << "GPG list-secret-keys failed with code:" << rc;
4 ✔
62
    return false;
4 ✔
63
  }
64
  // Records, one per line: a key (sec, or ssb for a subkey) and later its
65
  // own fpr (an rvk may come between). A user ID is a record of its own,
66
  // with its colons and line breaks escaped, so nothing a key's owner
67
  // wrote can pass for either; a search of the whole output for the
68
  // fingerprint could be fooled by one. A key signs when it can (s in its
69
  // own capabilities), is not revoked, expired or invalid, and its secret
70
  // part is here (field 15 is # for a stub).
71
  // Configured as a subkey, that subkey must sign. Configured as a primary
72
  // key, gpg's --local-user picks a signing key from its whole block, so
73
  // the primary or any of its subkeys will do.
74
  struct Key {
309 ✔
75
    bool signs = false;
76
    bool valid = false;
77
    bool primary = false;
78
    QString fpr;
79
  };
80
  QList<QList<Key>> blocks;
47 ✔
81
  bool awaitingFpr = false;
82
  const QStringList lines = out.split(QLatin1Char('\n'));
47 ✔
83
  for (const QString &line : lines) {
219 ✔
84
    const QStringList fields = line.trimmed().split(QLatin1Char(':'));
172 ✔
85
    const QString &type = fields.first();
86
    const bool primary = type == QLatin1String("sec");
172 ✔
87
    if (primary || type == QLatin1String("ssb")) {
172 ✔
88
      if (primary) {
57 ✔
89
        blocks.append(QList<Key>());
90 ✔
90
      }
91
      if (blocks.isEmpty()) {
57 ✔
92
        awaitingFpr = false;
93
        continue;
×
94
      }
95
      const QString validity = fields.value(1);
57 ✔
96
      Key k;
57 ✔
97
      k.primary = primary;
57 ✔
98
      k.valid = validity != QLatin1String("r") &&
57 ✔
99
                validity != QLatin1String("e") &&
57 ✔
100
                validity != QLatin1String("i");
52 ✔
101
      k.signs = k.valid && fields.value(11).contains(QLatin1Char('s')) &&
163 ✔
102
                fields.value(14) != QLatin1String("#");
106 ✔
103
      blocks.last().append(k);
104
      awaitingFpr = true;
105
    } else if (type == QLatin1String("fpr") && awaitingFpr) {
115 ✔
106
      blocks.last().last().fpr = fields.value(9);
114 ✔
107
      awaitingFpr = false;
108
    }
109
  }
110
  for (const QList<Key> &block : std::as_const(blocks)) {
57 ✔
111
    // A revoked or expired primary key takes its subkeys with it.
112
    if (block.isEmpty() || !block.first().valid) {
45 ✔
113
      continue;
4 ✔
114
    }
115
    for (const Key &k : block) {
52 ✔
116
      if (k.fpr.compare(key, Qt::CaseInsensitive) != 0) {
46 ✔
117
        continue;
7 ✔
118
      }
119
      if (k.signs) {
39 ✔
120
        return true;
121
      }
122
      if (k.primary && std::any_of(block.cbegin(), block.cend(),
7 ✔
123
                                   [](const Key &sub) { return sub.signs; })) {
4 ✔
124
        return true;
125
      }
126
    }
127
  }
128
  return false;
129
}
130

131
auto GpgIdSigner::sign(const QString &gpgIdFile, const QByteArray &contents,
39 ✔
132
                       QString *error) const -> bool {
133
  if (!enabled()) {
39 ✔
134
    return true;
135
  }
136
  if (m_keys.size() > 1) {
38 ✔
137
    qCDebug(lcQtPass)
4 ✔
138
        << "Multiple signing keys configured; using only the first key:"
×
139
        << m_keys.first();
×
140
  }
141
  // The executor feeds stdin as UTF-8 text (see verify()).
142
  const QString text = QString::fromUtf8(contents);
143
  if (text.toUtf8() != contents) {
38 ✔
144
    if (error)
1 ✔
145
      *error = QCoreApplication::translate(
2 ✔
146
          "GpgIdSigner", "the recipient list is not valid UTF-8");
147
    return false;
1 ✔
148
  }
149
  QTemporaryDir scratch;
37 ✔
150
  if (!scratch.isValid()) {
37 ✔
151
    if (error)
×
152
      *error = scratch.errorString();
×
153
    return false;
×
154
  }
155
  const QString output = scratch.filePath(QStringLiteral("sig"));
74 ✔
156
  // --local-user, not --default-key: without that secret key gpg would
157
  // print "all values passed to '--default-key' ignored", sign with
158
  // whatever secret key it has and exit 0.
159
  const int rc = run(
333 ✔
160
      {QStringLiteral("--local-user"), m_keys.first(), QStringLiteral("--yes"),
37 ✔
161
       QStringLiteral("--detach-sign"), QStringLiteral("--output"),
37 ✔
162
       Executor::translatePathForWsl(output, m_gpg), QStringLiteral("-")},
37 ✔
163
      nullptr, error, text);
164
  if (rc != 0) {
37 ✔
165
    qCDebug(lcQtPass) << "GPG signing failed with code:" << rc;
5 ✔
166
    return false;
5 ✔
167
  }
168
  QFile signature(output);
32 ✔
169
  if (!signature.open(QIODevice::ReadOnly) || signature.size() <= 0) {
32 ✔
170
    if (error)
1 ✔
171
      *error =
172
          QCoreApplication::translate("GpgIdSigner", "gpg wrote no signature");
2 ✔
173
    return false;
1 ✔
174
  }
175
  const QByteArray bytes = signature.readAll();
31 ✔
176
  signature.close();
31 ✔
177
  // Checked before it is placed: a signature by any key but the configured
178
  // ones (an older gpg, a wrapper, a smartcard picking its own) is not
179
  // written, and the one there, if any, stays.
180
  if (!verify(contents, output)) {
31 ✔
181
    if (error)
4 ✔
182
      *error = QCoreApplication::translate(
8 ✔
183
          "GpgIdSigner",
184
          "the new signature does not verify as one of the signing keys");
185
    return false;
4 ✔
186
  }
187
  return Util::writeFileReplacing(gpgIdFile + QStringLiteral(".sig"), bytes,
54 ✔
188
                                  true, error);
189
}
106 ✔
190

191
auto GpgIdSigner::validSigFingerprints(const QString &statusOutput)
136 ✔
192
    -> QStringList {
193
  static const QRegularExpression re(
194
      // 40 hex for v4 keys, 64 for v5/v6 (gpg 2.5+), either position.
195
      R"(^\[GNUPG:\] VALIDSIG ([A-F0-9]{40}(?:[A-F0-9]{24})?) .* )"
12 ✔
196
      R"(([A-F0-9]{40}(?:[A-F0-9]{24})?)\r?$)",
197
      QRegularExpression::MultilineOption);
154 ✔
198
  // gpg reports a signature by a revoked key as REVKEYSIG plus KEYREVOKED,
199
  // and still prints VALIDSIG and exits 0. A signing key revoked because it
200
  // was compromised must not vouch for a recipient list, so a signature
201
  // whose block (from one NEWSIG to the next) says so does not count. One
202
  // by an expired key does, as gpg calls it a good signature: expiry is not
203
  // compromise, and pass accepts it too.
204
  static const QRegularExpression revoked(
205
      QStringLiteral(R"(^\[GNUPG:\] (?:REVKEYSIG|KEYREVOKED)\b)"),
6 ✔
206
      QRegularExpression::MultilineOption);
148 ✔
207
  static const QRegularExpression newSig(
208
      QStringLiteral(R"(^\[GNUPG:\] NEWSIG\b)"),
6 ✔
209
      QRegularExpression::MultilineOption);
148 ✔
210
  const QRegularExpressionMatch m = re.match(statusOutput);
136 ✔
211
  if (!m.hasMatch()) {
136 ✔
212
    return {};
5 ✔
213
  }
214
  const qsizetype from =
215
      statusOutput.lastIndexOf(newSig, m.capturedStart()) < 0
131 ✔
216
          ? 0
131 ✔
217
          : statusOutput.lastIndexOf(newSig, m.capturedStart());
27 ✔
218
  const qsizetype to = statusOutput.indexOf(newSig, m.capturedEnd());
131 ✔
219
  const QString block = statusOutput.mid(from, to < 0 ? -1 : to - from);
131 ✔
220
  if (revoked.match(block).hasMatch()) {
131 ✔
221
    return {};
3 ✔
222
  }
223
  return {m.captured(1), m.captured(2)};
384 ✔
224
}
264 ✔
225

226
auto GpgIdSigner::verify(const QByteArray &contents,
150 ✔
227
                         const QString &signatureFile) const -> bool {
228
  if (!enabled()) {
150 ✔
229
    return true;
230
  }
231
  // The executor feeds stdin as UTF-8 text. A .gpg-id is one: key IDs,
232
  // fingerprints, e-mail addresses, comments. Bytes that would not survive
233
  // the round trip cannot be verified faithfully, so they are not verified
234
  // at all rather than as something else.
235
  const QString text = QString::fromUtf8(contents);
236
  if (text.toUtf8() != contents) {
149 ✔
237
    qCDebug(lcQtPass) << "Refusing to verify" << signatureFile
1 ✔
238
                      << ": the signed file is not valid UTF-8";
×
239
    return false;
1 ✔
240
  }
241
  // Same for the signature: a valid pair replayed from elsewhere through two
242
  // links is not this folder's list.
243
  if (Util::isLinkedFolder(signatureFile)) {
148 ✔
244
    qCDebug(lcQtPass) << "Refusing to verify" << signatureFile
1 ✔
245
                      << ": the signature file is a link";
×
246
    return false;
1 ✔
247
  }
248
  // Read as a regular file of bounded size, as the list itself is, and
249
  // give gpg a copy of those bytes: a FIFO planted under the name would
250
  // block gpg, and the GUI thread waiting for it, a sparse file of
251
  // gigabytes would be read whole, and either could be swapped in between
252
  // a check of the name and gpg opening it.
253
  const std::optional<QByteArray> signature =
254
      Util::readRegularFile(signatureFile, kMaxSignatureBytes);
147 ✔
255
  if (!signature) {
147 ✔
256
    qCDebug(lcQtPass) << "Refusing to verify" << signatureFile
18 ✔
NEW
257
                      << ": not a regular file of at most" << kMaxSignatureBytes
×
NEW
258
                      << "bytes";
×
259
    return false;
18 ✔
260
  }
261
  QTemporaryDir scratch;
129 ✔
262
  if (!scratch.isValid()) {
129 ✔
263
    return false;
264
  }
265
  const QString copy = scratch.filePath(QStringLiteral("sig"));
258 ✔
266
  {
267
    QFile file(copy);
129 ✔
268
    if (!file.open(QIODevice::WriteOnly) ||
258 ✔
269
        file.write(*signature) != signature->size()) {
129 ✔
270
      return false;
271
    }
272
  }
129 ✔
273
  QString out;
129 ✔
274
  const QString sig = Executor::translatePathForWsl(copy, m_gpg);
129 ✔
275
  // "-" makes gpg read the signed data from stdin: the bytes we hold.
276
  // --batch stops at the first bad signature; without it gpg checked every
277
  // packet in the file, each against the key it names, however slow.
278
  const int rc = run({QStringLiteral("--batch"), QStringLiteral("--verify"),
1,161 ✔
279
                      QStringLiteral("--status-fd=1"), QStringLiteral("--"),
129 ✔
280
                      sig, QStringLiteral("-")},
129 ✔
281
                     &out, nullptr, text);
282
  if (rc != 0) {
129 ✔
283
    qCDebug(lcQtPass) << "GPG verify failed with code:" << rc;
3 ✔
284
    return false;
3 ✔
285
  }
286
  const QStringList fingerprints = validSigFingerprints(out);
126 ✔
287
  for (const QString &key : m_keys) {
134 ✔
288
    if (fingerprints.contains(key)) {
127 ✔
289
      return true;
290
    }
291
  }
292
  return false;
293
}
129 ✔
294

295
auto GpgIdSigner::verifyFile(const QString &gpgIdFile,
111 ✔
296
                             QByteArray *contents) const -> bool {
297
  // A link under the name is not the store's .gpg-id, whatever it points
298
  // to and however well that is signed.
299
  if (Util::isLinkedFolder(gpgIdFile)) {
111 ✔
300
    contents->clear();
1 ✔
301
    return false;
1 ✔
302
  }
303
  // Read as the regular file it is and bounded: a FIFO would block, a
304
  // sparse file of gigabytes be read whole, before any signature is checked.
305
  std::optional<QByteArray> bytes =
306
      Util::readRegularFile(gpgIdFile, Util::kMaxGpgIdBytes);
110 ✔
307
  if (!bytes) {
110 ✔
308
    contents->clear();
7 ✔
309
    return false;
310
  }
311
  *contents = *std::move(bytes);
312
  return verify(*contents, gpgIdFile + QStringLiteral(".sig"));
206 ✔
313
}
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