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

IJHack / QtPass / 23798624303

31 Mar 2026 01:00PM UTC coverage: 18.508%. Remained the same
23798624303

push

github

web-flow
docs: add comprehensive docstrings to C++ source files (#866)

* docs: add docstrings to remaining cpp files

Added docstrings to:
- pass.cpp: executeWrapper, init, boundedRandom, generateRandomPassword
- util.cpp: endsWithGpg, protocolRegex, newLinesRegex
- usersdialog.cpp: passesFilter, isUserExpired, buildUserText, applyUserStyling
- passworddialog.cpp: setPass

* docs: add docstrings to qtpass.cpp and improve existing docstrings

* fix: use trailing return type for isUserExpired per clang-tidy

0 of 1 new or added line in 1 file covered. (0.0%)

7 existing lines in 4 files now uncovered.

933 of 5041 relevant lines covered (18.51%)

7.68 hits per line

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

33.88
/src/pass.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "pass.h"
4
#include "helpers.h"
5
#include "qtpasssettings.h"
6
#include "util.h"
7
#include <QDir>
8
#include <QRandomGenerator>
9
#include <QRegularExpression>
10
#include <utility>
11

12
#ifdef QT_DEBUG
13
#include "debughelper.h"
14
#endif
15

16
using Enums::GIT_INIT;
17
using Enums::GIT_PULL;
18
using Enums::GIT_PUSH;
19
using Enums::GPG_GENKEYS;
20
using Enums::PASS_COPY;
21
using Enums::PASS_INIT;
22
using Enums::PASS_INSERT;
23
using Enums::PASS_MOVE;
24
using Enums::PASS_OTP_GENERATE;
25
using Enums::PASS_REMOVE;
26
using Enums::PASS_SHOW;
27

28
/**
29
 * @brief Pass::Pass wrapper for using either pass or the pass imitation
30
 */
31
Pass::Pass() : wrapperRunning(false), env(QProcess::systemEnvironment()) {
11✔
32
  connect(&exec,
11✔
33
          static_cast<void (Executor::*)(int, int, const QString &,
34
                                         const QString &)>(&Executor::finished),
35
          this, &Pass::finished);
11✔
36

37
  // TODO(bezet): stop using process
38
  // connect(&process, SIGNAL(error(QProcess::ProcessError)), this,
39
  //        SIGNAL(error(QProcess::ProcessError)));
40

41
  connect(&exec, &Executor::starting, this, &Pass::startingExecuteWrapper);
11✔
42
  env.append("WSLENV=PASSWORD_STORE_DIR/p");
11✔
43
}
11✔
44

45
/**
46
 * @brief Executes a wrapper command.
47
 * @param id Process ID
48
 * @param app Application to execute
49
 * @param args Arguments
50
 * @param readStdout Whether to read stdout
51
 * @param readStderr Whether to read stderr
52
 */
UNCOV
53
void Pass::executeWrapper(PROCESS id, const QString &app,
×
54
                          const QStringList &args, bool readStdout,
55
                          bool readStderr) {
56
  executeWrapper(id, app, args, QString(), readStdout, readStderr);
×
57
}
×
58

59
void Pass::executeWrapper(PROCESS id, const QString &app,
×
60
                          const QStringList &args, QString input,
61
                          bool readStdout, bool readStderr) {
62
#ifdef QT_DEBUG
63
  dbg() << app << args;
64
#endif
65
  exec.execute(id, QtPassSettings::getPassStore(), app, args, std::move(input),
×
66
               readStdout, readStderr);
67
}
×
68

69
/**
70
 * @brief Initializes the pass wrapper environment.
71
 */
72
void Pass::init() {
1✔
73
#ifdef __APPLE__
74
  // If it exists, add the gpgtools to PATH
75
  if (QFile("/usr/local/MacGPG2/bin").exists())
76
    env.replaceInStrings("PATH=", "PATH=/usr/local/MacGPG2/bin:");
77
  // Add missing /usr/local/bin
78
  if (env.filter("/usr/local/bin").isEmpty())
79
    env.replaceInStrings("PATH=", "PATH=/usr/local/bin:");
80
#endif
81

82
  if (!QtPassSettings::getGpgHome().isEmpty()) {
2✔
83
    QDir absHome(QtPassSettings::getGpgHome());
×
84
    absHome.makeAbsolute();
×
85
    env << "GNUPGHOME=" + absHome.path();
×
86
  }
×
87
}
1✔
88

89
/**
90
 * @brief Pass::Generate use either pwgen or internal password
91
 * generator
92
 * @param length of the desired password
93
 * @param charset to use for generation
94
 * @return the password
95
 */
96
auto Pass::Generate_b(unsigned int length, const QString &charset) -> QString {
1,004✔
97
  QString passwd;
1,004✔
98
  if (QtPassSettings::isUsePwgen()) {
1,004✔
99
    // --secure goes first as it overrides --no-* otherwise
100
    QStringList args;
×
101
    args.append("-1");
×
102
    if (!QtPassSettings::isLessRandom()) {
×
103
      args.append("--secure");
×
104
    }
105
    args.append(QtPassSettings::isAvoidCapitals() ? "--no-capitalize"
×
106
                                                  : "--capitalize");
107
    args.append(QtPassSettings::isAvoidNumbers() ? "--no-numerals"
×
108
                                                 : "--numerals");
109
    if (QtPassSettings::isUseSymbols()) {
×
110
      args.append("--symbols");
×
111
    }
112
    args.append(QString::number(length));
×
113
    // TODO(bezet): try-catch here(2 statuses to merge o_O)
114
    if (Executor::executeBlocking(QtPassSettings::getPwgenExecutable(), args,
×
115
                                  &passwd) == 0) {
116
      static const QRegularExpression literalNewLines{"[\\n\\r]"};
×
117
      passwd.remove(literalNewLines);
×
118
    } else {
119
      passwd.clear();
×
120
#ifdef QT_DEBUG
121
      qDebug() << __FILE__ << ":" << __LINE__ << "\t"
122
               << "pwgen fail";
123
#endif
124
      // TODO(bezet): emit critical ?
125
    }
126
  } else {
127
    // Validate charset - if CUSTOM is selected but chars are empty,
128
    // fall back to ALLCHARS to prevent weak passwords (issue #780)
129
    QString effectiveCharset = charset;
130
    if (effectiveCharset.isEmpty()) {
1,004✔
131
      effectiveCharset = QtPassSettings::getPasswordConfiguration()
2✔
132
                             .Characters[PasswordConfiguration::ALLCHARS];
133
    }
134
    if (effectiveCharset.length() > 0) {
1,004✔
135
      passwd = generateRandomPassword(effectiveCharset, length);
2,008✔
136
    } else {
137
      emit critical(
×
138
          tr("No characters chosen"),
×
139
          tr("Can't generate password, there are no characters to choose from "
×
140
             "set in the configuration!"));
141
    }
142
  }
143
  return passwd;
1,004✔
144
}
145

146
/**
147
 * @brief Pass::gpgSupportsEd25519 check if GPG supports ed25519 (ECC)
148
 * GPG 2.1+ supports ed25519 which is much faster for key generation
149
 * @return true if ed25519 is supported
150
 */
151
bool Pass::gpgSupportsEd25519() {
3✔
152
  QString out, err;
3✔
153
  if (Executor::executeBlocking(QtPassSettings::getGpgExecutable(),
12✔
154
                                {"--version"}, &out, &err) != 0) {
155
    return false;
156
  }
157
  QRegularExpression versionRegex(R"(gpg \(GnuPG\) (\d+)\.(\d+))");
×
158
  QRegularExpressionMatch match = versionRegex.match(out);
×
159
  if (!match.hasMatch()) {
×
160
    return false;
161
  }
162
  int major = match.captured(1).toInt();
×
163
  int minor = match.captured(2).toInt();
×
164
  return major > 2 || (major == 2 && minor >= 1);
×
165
}
3✔
166

167
/**
168
 * @brief Pass::getDefaultKeyTemplate return default key generation template
169
 * Uses ed25519 if supported, otherwise falls back to RSA
170
 * @return GPG batch template string
171
 */
172
QString Pass::getDefaultKeyTemplate() {
1✔
173
  if (gpgSupportsEd25519()) {
1✔
174
    return QStringLiteral("%echo Generating a default key\n"
×
175
                          "Key-Type: EdDSA\n"
176
                          "Key-Curve: Ed25519\n"
177
                          "Subkey-Type: ECDH\n"
178
                          "Subkey-Curve: Curve25519\n"
179
                          "Name-Real: \n"
180
                          "Name-Comment: QtPass\n"
181
                          "Name-Email: \n"
182
                          "Expire-Date: 0\n"
183
                          "%no-protection\n"
184
                          "%commit\n"
185
                          "%echo done");
186
  }
187
  return QStringLiteral("%echo Generating a default key\n"
1✔
188
                        "Key-Type: RSA\n"
189
                        "Subkey-Type: RSA\n"
190
                        "Name-Real: \n"
191
                        "Name-Comment: QtPass\n"
192
                        "Name-Email: \n"
193
                        "Expire-Date: 0\n"
194
                        "%no-protection\n"
195
                        "%commit\n"
196
                        "%echo done");
197
}
198

199
/**
200
 * @brief Pass::GenerateGPGKeys internal gpg keypair generator . .
201
 * @param batch GnuPG style configuration string
202
 */
203
void Pass::GenerateGPGKeys(QString batch) {
×
204
  executeWrapper(GPG_GENKEYS, QtPassSettings::getGpgExecutable(),
×
205
                 {"--gen-key", "--no-tty", "--batch"}, std::move(batch));
206
}
×
207

208
/**
209
 * @brief Pass::listKeys list users
210
 * @param keystrings
211
 * @param secret list private keys
212
 * @return QList<UserInfo> users
213
 */
214
auto Pass::listKeys(QStringList keystrings, bool secret) -> QList<UserInfo> {
×
215
  QList<UserInfo> users;
×
216
  QStringList args = {"--no-tty", "--with-colons", "--with-fingerprint"};
×
217
  args.append(secret ? "--list-secret-keys" : "--list-keys");
×
218

219
  for (const QString &keystring : AS_CONST(keystrings)) {
×
220
    if (!keystring.isEmpty()) {
×
221
      args.append(keystring);
222
    }
223
  }
224
  QString p_out;
×
225
  if (Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
226
                                &p_out) != 0) {
227
    return users;
228
  }
229
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
230
  const QStringList keys =
231
      p_out.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
×
232
#else
233
  const QStringList keys =
234
      p_out.split(Util::newLinesRegex(), QString::SkipEmptyParts);
235
#endif
236
  UserInfo current_user;
×
237
  for (const QString &key : keys) {
×
238
    QStringList props = key.split(':');
×
239
    if (props.size() < 10) {
×
240
      continue;
241
    }
242
    if (props[0] == (secret ? "sec" : "pub")) {
×
243
      if (!current_user.key_id.isEmpty()) {
×
244
        users.append(current_user);
245
      }
246
      current_user = UserInfo();
×
247
      current_user.key_id = props[4];
×
248
      current_user.name = props[9].toUtf8();
×
249
      current_user.validity = props[1][0].toLatin1();
×
250
      current_user.created.setSecsSinceEpoch(props[5].toUInt());
×
251
      current_user.expiry.setSecsSinceEpoch(props[6].toUInt());
×
252
    } else if (current_user.name.isEmpty() && props[0] == "uid") {
×
253
      current_user.name = props[9];
×
254
    } else if ((props[0] == "fpr") && props[9].endsWith(current_user.key_id)) {
×
255
      current_user.key_id = props[9];
×
256
    }
257
  }
258
  if (!current_user.key_id.isEmpty()) {
×
259
    users.append(current_user);
260
  }
261
  return users;
262
}
×
263

264
/**
265
 * @brief Pass::listKeys list users
266
 * @param keystring
267
 * @param secret list private keys
268
 * @return QList<UserInfo> users
269
 */
270
auto Pass::listKeys(const QString &keystring, bool secret) -> QList<UserInfo> {
×
271
  return listKeys(QStringList(keystring), secret);
×
272
}
273

274
/**
275
 * @brief Pass::processFinished reemits specific signal based on what process
276
 * has finished
277
 * @param id    id of Pass process that was scheduled and finished
278
 * @param exitCode  return code of a process
279
 * @param out   output generated by process(if capturing was requested, empty
280
 *              otherwise)
281
 * @param err   error output generated by process(if capturing was requested,
282
 *              or error occurred)
283
 */
284
void Pass::finished(int id, int exitCode, const QString &out,
×
285
                    const QString &err) {
286
  auto pid = static_cast<PROCESS>(id);
287
  if (exitCode != 0) {
×
288
    emit processErrorExit(exitCode, err);
×
289
    return;
×
290
  }
291
  switch (pid) {
×
292
  case GIT_INIT:
×
293
    emit finishedGitInit(out, err);
×
294
    break;
×
295
  case GIT_PULL:
×
296
    emit finishedGitPull(out, err);
×
297
    break;
×
298
  case GIT_PUSH:
×
299
    emit finishedGitPush(out, err);
×
300
    break;
×
301
  case PASS_SHOW:
×
302
    emit finishedShow(out);
×
303
    break;
×
304
  case PASS_OTP_GENERATE:
×
305
    emit finishedOtpGenerate(out);
×
306
    break;
×
307
  case PASS_INSERT:
×
308
    emit finishedInsert(out, err);
×
309
    break;
×
310
  case PASS_REMOVE:
×
311
    emit finishedRemove(out, err);
×
312
    break;
×
313
  case PASS_INIT:
×
314
    emit finishedInit(out, err);
×
315
    break;
×
316
  case PASS_MOVE:
×
317
    emit finishedMove(out, err);
×
318
    break;
×
319
  case PASS_COPY:
×
320
    emit finishedCopy(out, err);
×
321
    break;
×
322
  case GPG_GENKEYS:
×
323
    emit finishedGenerateGPGKeys(out, err);
×
324
    break;
×
325
  default:
326
#ifdef QT_DEBUG
327
    dbg() << "Unhandled process type" << pid;
328
#endif
329
    break;
330
  }
331
}
332

333
/**
334
 * @brief Pass::updateEnv update the execution environment (used when
335
 * switching profiles)
336
 */
337
void Pass::updateEnv() {
×
338
  // put PASSWORD_STORE_SIGNING_KEY in env
339
  QStringList envSigningKey = env.filter("PASSWORD_STORE_SIGNING_KEY=");
×
340
  QString currentSigningKey = QtPassSettings::getPassSigningKey();
×
341
  if (envSigningKey.isEmpty()) {
×
342
    if (!currentSigningKey.isEmpty()) {
×
343
      // dbg()<< "Added
344
      // PASSWORD_STORE_SIGNING_KEY with" + currentSigningKey;
345
      env.append("PASSWORD_STORE_SIGNING_KEY=" + currentSigningKey);
×
346
    }
347
  } else {
348
    if (currentSigningKey.isEmpty()) {
×
349
      // dbg() << "Removed
350
      // PASSWORD_STORE_SIGNING_KEY";
351
      env.removeAll(envSigningKey.first());
352
    } else {
353
      // dbg()<< "Update
354
      // PASSWORD_STORE_SIGNING_KEY with " + currentSigningKey;
355
      env.replaceInStrings(envSigningKey.first(),
×
356
                           "PASSWORD_STORE_SIGNING_KEY=" + currentSigningKey);
×
357
    }
358
  }
359
  // put PASSWORD_STORE_DIR in env
360
  QStringList store = env.filter("PASSWORD_STORE_DIR=");
×
361
  if (store.isEmpty()) {
×
362
    // dbg()<< "Added
363
    // PASSWORD_STORE_DIR";
364
    env.append("PASSWORD_STORE_DIR=" + QtPassSettings::getPassStore());
×
365
  } else {
366
    // dbg()<< "Update
367
    // PASSWORD_STORE_DIR with " + passStore;
368
    env.replaceInStrings(store.first(), "PASSWORD_STORE_DIR=" +
×
369
                                            QtPassSettings::getPassStore());
×
370
  }
371
  exec.setEnvironment(env);
×
372
}
×
373

374
/**
375
 * @brief Pass::getGpgIdPath return gpgid file path for some file (folder).
376
 * @param for_file which file (folder) would you like the gpgid file path for.
377
 * @return path to the gpgid file.
378
 */
379
auto Pass::getGpgIdPath(const QString &for_file) -> QString {
8✔
380
  QString passStore =
381
      QDir::fromNativeSeparators(QtPassSettings::getPassStore());
16✔
382
  QString normalizedFile = QDir::fromNativeSeparators(for_file);
8✔
383
  QString fullPath = normalizedFile.startsWith(passStore)
8✔
384
                         ? normalizedFile
8✔
385
                         : passStore + "/" + normalizedFile;
6✔
386
  QDir gpgIdDir(QFileInfo(fullPath).absoluteDir());
8✔
387
  bool found = false;
388
  while (gpgIdDir.exists() && gpgIdDir.absolutePath().startsWith(passStore)) {
10✔
389
    if (QFile(gpgIdDir.absoluteFilePath(".gpg-id")).exists()) {
2✔
390
      found = true;
391
      break;
392
    }
393
    if (!gpgIdDir.cdUp()) {
×
394
      break;
395
    }
396
  }
397
  QString gpgIdPath(found ? gpgIdDir.absoluteFilePath(".gpg-id")
8✔
398
                          : QtPassSettings::getPassStore() + ".gpg-id");
22✔
399

400
  return gpgIdPath;
8✔
401
}
8✔
402

403
/**
404
 * @brief Pass::getRecipientList return list of gpg-id's to encrypt for
405
 * @param for_file which file (folder) would you like recepients for
406
 * @return recepients gpg-id contents
407
 */
408
auto Pass::getRecipientList(const QString &for_file) -> QStringList {
5✔
409
  QFile gpgId(getGpgIdPath(for_file));
5✔
410
  if (!gpgId.open(QIODevice::ReadOnly | QIODevice::Text)) {
5✔
411
    return {};
×
412
  }
413
  QStringList recipients;
5✔
414
  while (!gpgId.atEnd()) {
14✔
415
    QString recipient(gpgId.readLine());
18✔
416
    recipient = recipient.split("#")[0].trimmed();
18✔
417
    if (!recipient.isEmpty()) {
9✔
418
      recipients += recipient;
419
    }
420
  }
421
  return recipients;
422
}
5✔
423

424
/**
425
 * @brief Pass::getRecipientString formated string for use with GPG
426
 * @param for_file which file (folder) would you like recepients for
427
 * @param separator formating separator eg: " -r "
428
 * @param count
429
 * @return recepient string
430
 */
431
auto Pass::getRecipientString(const QString &for_file, const QString &separator,
2✔
432
                              int *count) -> QStringList {
433
  Q_UNUSED(separator)
434
  QStringList recipients = Pass::getRecipientList(for_file);
2✔
435
  if (count) {
2✔
436
    *count = recipients.size();
1✔
437
  }
438
  return recipients;
2✔
439
}
440

441
/* Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
442
 */
443

444
/**
445
 * @brief Generates a random number bounded by the given value.
446
 * @param bound Upper bound (exclusive)
447
 * @return Random number in range [0, bound)
448
 */
449
auto Pass::boundedRandom(quint32 bound) -> quint32 {
1,160✔
450
  if (bound < 2) {
1,160✔
451
    return 0;
452
  }
453

454
  quint32 randval;
455
  const quint32 max_mod_bound = (1 + ~bound) % bound;
1,160✔
456

457
  do {
458
    randval = QRandomGenerator::system()->generate();
459
  } while (randval < max_mod_bound);
1,160✔
460

461
  return randval % bound;
1,160✔
462
}
463

464
/**
465
 * @brief Generates a random password from the given charset.
466
 * @param charset Characters to use in the password
467
 * @param length Desired password length
468
 * @return Generated password string
469
 */
470
auto Pass::generateRandomPassword(const QString &charset, unsigned int length)
1,004✔
471
    -> QString {
472
  QString out;
1,004✔
473
  for (unsigned int i = 0; i < length; ++i) {
2,164✔
474
    out.append(charset.at(static_cast<int>(
1,160✔
475
        boundedRandom(static_cast<quint32>(charset.length())))));
1,160✔
476
  }
477
  return out;
1,004✔
478
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc