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

IJHack / QtPass / 24656984597

20 Apr 2026 08:42AM UTC coverage: 27.369% (+0.007%) from 27.362%
24656984597

push

github

web-flow
Merge pull request #1096 from IJHack/fix/coverity-scan-issues

fix: address Coverity scan issues

4 of 6 new or added lines in 2 files covered. (66.67%)

9 existing lines in 1 file now uncovered.

1597 of 5835 relevant lines covered (27.37%)

18.01 hits per line

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

67.86
/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 "gpgkeystate.h"
5
#include "helpers.h"
6
#include "qtpasssettings.h"
7
#include "util.h"
8
#include <QCoreApplication>
9
#include <QDebug>
10
#include <QDir>
11
#include <QFileInfo>
12
#include <QProcess>
13
#include <QRandomGenerator>
14
#include <QRegularExpression>
15
#include <algorithm>
16
#include <utility>
17

18
#ifdef QT_DEBUG
19
#include "debughelper.h"
20
#endif
21

22
using Enums::GIT_INIT;
23
using Enums::GIT_PULL;
24
using Enums::GIT_PUSH;
25
using Enums::GPG_GENKEYS;
26
using Enums::PASS_COPY;
27
using Enums::PASS_GREP;
28
using Enums::PASS_INIT;
29
using Enums::PASS_INSERT;
30
using Enums::PASS_MOVE;
31
using Enums::PASS_OTP_GENERATE;
32
using Enums::PASS_REMOVE;
33
using Enums::PASS_SHOW;
34

35
namespace {
36
auto fallbackCharset(const QString &input, const QString &fallback) -> QString {
37
  return input.isEmpty() ? fallback : input;
1,021✔
38
}
39

40
auto effectiveCharset(const PasswordConfiguration &passConfig) -> QString {
16✔
41
  int sel = passConfig.selected;
16✔
42
  if (sel < 0 || sel >= PasswordConfiguration::CHARSETS_COUNT)
16✔
43
    sel = PasswordConfiguration::ALLCHARS;
44
  return fallbackCharset(
45
      passConfig.Characters[sel],
16✔
46
      passConfig.Characters[PasswordConfiguration::ALLCHARS]);
16✔
47
}
48
} // namespace
49

50
/**
51
 * @brief Pass::Pass wrapper for using either pass or the pass imitation
52
 */
53
Pass::Pass() : wrapperRunning(false), env(QProcess::systemEnvironment()) {
29✔
54
  connect(&exec,
29✔
55
          static_cast<void (Executor::*)(int, int, const QString &,
56
                                         const QString &)>(&Executor::finished),
57
          this, &Pass::finished);
29✔
58

59
  // This was previously using direct QProcess signals.
60
  // The code now uses Executor instead of raw QProcess for better control.
61
  // connect(&process, SIGNAL(error(QProcess::ProcessError)), this,
62
  //        SIGNAL(error(QProcess::ProcessError)));
63

64
  connect(&exec, &Executor::starting, this, &Pass::startingExecuteWrapper);
29✔
65
  // Merge our vars into WSLENV rather than blindly appending a duplicate entry
66
  const QStringList wslenvVars = {
67
      QStringLiteral("PASSWORD_STORE_DIR/p"),
58✔
68
      QStringLiteral("PASSWORD_STORE_GENERATED_LENGTH/w"),
29✔
69
      QStringLiteral("PASSWORD_STORE_CHARACTER_SET/w")};
145✔
70
  const QString wslenvPrefix = QStringLiteral("WSLENV=");
29✔
71
  auto it =
72
      std::find_if(env.begin(), env.end(), [&wslenvPrefix](const QString &s) {
58✔
73
        return s.startsWith(wslenvPrefix);
3,720✔
74
      });
75
  if (it == env.end()) {
29✔
76
    env.append(wslenvPrefix + wslenvVars.join(':'));
58✔
77
  } else {
78
    QStringList parts =
79
        it->mid(wslenvPrefix.size()).split(':', Qt::SkipEmptyParts);
×
80
    for (const QString &v : wslenvVars) {
×
81
      if (!parts.contains(v))
×
82
        parts.append(v);
83
    }
84
    *it = wslenvPrefix + parts.join(':');
×
85
  }
86
}
29✔
87

88
/**
89
 * @brief Executes a wrapper command.
90
 * @param id Process ID
91
 * @param app Application to execute
92
 * @param args Arguments
93
 * @param readStdout Whether to read stdout
94
 * @param readStderr Whether to read stderr
95
 */
96
void Pass::executeWrapper(PROCESS id, const QString &app,
×
97
                          const QStringList &args, bool readStdout,
98
                          bool readStderr) {
99
  executeWrapper(id, app, args, QString(), readStdout, readStderr);
×
100
}
×
101

102
void Pass::executeWrapper(PROCESS id, const QString &app,
17✔
103
                          const QStringList &args, QString input,
104
                          bool readStdout, bool readStderr) {
105
#ifdef QT_DEBUG
106
  dbg() << app << args;
107
#endif
108
  exec.execute(id, QtPassSettings::getPassStore(), app, args, std::move(input),
34✔
109
               readStdout, readStderr);
110
}
17✔
111

112
/**
113
 * @brief Initializes the pass wrapper environment.
114
 */
115
void Pass::init() {
9✔
116
#ifdef __APPLE__
117
  // If it exists, add the gpgtools to PATH
118
  if (QFile("/usr/local/MacGPG2/bin").exists())
119
    env.replaceInStrings("PATH=", "PATH=/usr/local/MacGPG2/bin:");
120
  // Add missing /usr/local/bin
121
  if (env.filter("/usr/local/bin").isEmpty())
122
    env.replaceInStrings("PATH=", "PATH=/usr/local/bin:");
123
#endif
124

125
  if (!QtPassSettings::getGpgHome().isEmpty()) {
18✔
126
    QDir absHome(QtPassSettings::getGpgHome());
16✔
127
    absHome.makeAbsolute();
8✔
128
    env << "GNUPGHOME=" + absHome.path();
8✔
129
  }
8✔
130
}
9✔
131

132
/**
133
 * @brief Pass::Generate use either pwgen or internal password
134
 * generator
135
 * @param length of the desired password
136
 * @param charset to use for generation
137
 * @return the password
138
 */
139
auto Pass::generatePassword(unsigned int length, const QString &charset)
1,006✔
140
    -> QString {
141
  if (length == 0) {
1,006✔
142
    emit critical(tr("Invalid password length"),
2✔
143
                  tr("Can't generate password with zero length."));
1✔
144
    return {};
145
  }
146
  QString passwd;
1,005✔
147
  if (QtPassSettings::isUsePwgen()) {
1,005✔
148
    // --secure goes first as it overrides --no-* otherwise
149
    QStringList args;
×
150
    args.append("-1");
×
151
    if (!QtPassSettings::isLessRandom()) {
×
152
      args.append("--secure");
×
153
    }
154
    args.append(QtPassSettings::isAvoidCapitals() ? "--no-capitalize"
×
155
                                                  : "--capitalize");
156
    args.append(QtPassSettings::isAvoidNumbers() ? "--no-numerals"
×
157
                                                 : "--numerals");
158
    if (QtPassSettings::isUseSymbols()) {
×
159
      args.append("--symbols");
×
160
    }
161
    args.append(QString::number(length));
×
162
    // executeBlocking returns 0 on success, non-zero on failure
163
    if (Executor::executeBlocking(QtPassSettings::getPwgenExecutable(), args,
×
164
                                  &passwd) == 0) {
165
      static const QRegularExpression literalNewLines{"[\\n\\r]"};
×
166
      passwd.remove(literalNewLines);
×
167
    } else {
168
      passwd.clear();
×
169
#ifdef QT_DEBUG
170
      qDebug() << __FILE__ << ":" << __LINE__ << "\t"
171
               << "pwgen fail";
172
#endif
173
      // Error is already handled by clearing passwd; no need for critical
174
      // signal here
175
    }
176
  } else {
177
    // Validate charset - if CUSTOM is selected but chars are empty,
178
    // fall back to ALLCHARS to prevent weak passwords (issue #780)
179
    const QString cs = fallbackCharset(
180
        charset, QtPassSettings::getPasswordConfiguration()
2,010✔
181
                     .Characters[PasswordConfiguration::ALLCHARS]);
182
    if (cs.length() > 0) {
1,005✔
183
      passwd = generateRandomPassword(cs, length);
2,010✔
184
    } else {
185
      emit critical(
×
186
          tr("No characters chosen"),
×
187
          tr("Can't generate password, there are no characters to choose from "
×
188
             "set in the configuration!"));
189
    }
190
  }
191
  return passwd;
192
}
193

194
/**
195
 * @brief Pass::gpgSupportsEd25519 check if GPG supports ed25519 (ECC)
196
 * GPG 2.1+ supports ed25519 which is much faster for key generation
197
 * @return true if ed25519 is supported
198
 */
199
bool Pass::gpgSupportsEd25519() {
2✔
200
  QString out, err;
2✔
201
  if (Executor::executeBlocking(QtPassSettings::getGpgExecutable(),
8✔
202
                                {"--version"}, &out, &err) != 0) {
203
    return false;
204
  }
205
  QRegularExpression versionRegex(R"(gpg \(GnuPG\) (\d+)\.(\d+))");
×
206
  QRegularExpressionMatch match = versionRegex.match(out);
×
207
  if (!match.hasMatch()) {
×
208
    return false;
209
  }
210
  int major = match.captured(1).toInt();
×
211
  int minor = match.captured(2).toInt();
×
212
  return major > 2 || (major == 2 && minor >= 1);
×
213
}
2✔
214

215
/**
216
 * @brief Pass::getDefaultKeyTemplate return default key generation template
217
 * Uses ed25519 if supported, otherwise falls back to RSA
218
 * @return GPG batch template string
219
 */
220
QString Pass::getDefaultKeyTemplate() {
1✔
221
  if (gpgSupportsEd25519()) {
1✔
222
    return QStringLiteral("%echo Generating a default key\n"
×
223
                          "Key-Type: EdDSA\n"
224
                          "Key-Curve: Ed25519\n"
225
                          "Subkey-Type: ECDH\n"
226
                          "Subkey-Curve: Curve25519\n"
227
                          "Name-Real: \n"
228
                          "Name-Comment: QtPass\n"
229
                          "Name-Email: \n"
230
                          "Expire-Date: 0\n"
231
                          "%no-protection\n"
232
                          "%commit\n"
233
                          "%echo done");
234
  }
235
  return QStringLiteral("%echo Generating a default key\n"
1✔
236
                        "Key-Type: RSA\n"
237
                        "Subkey-Type: RSA\n"
238
                        "Name-Real: \n"
239
                        "Name-Comment: QtPass\n"
240
                        "Name-Email: \n"
241
                        "Expire-Date: 0\n"
242
                        "%no-protection\n"
243
                        "%commit\n"
244
                        "%echo done");
245
}
246

247
namespace {
248
auto resolveWslGpgconfPath(const QString &lastPart) -> QString {
3✔
249
  int lastSep = lastPart.lastIndexOf('/');
3✔
250
  if (lastSep < 0) {
3✔
251
    lastSep = lastPart.lastIndexOf('\\');
2✔
252
  }
253
  if (lastSep >= 0) {
2✔
254
    return lastPart.left(lastSep + 1) + "gpgconf";
2✔
255
  }
256
  return QStringLiteral("gpgconf");
2✔
257
}
258

259
/**
260
 * @brief Finds the path to the gpgconf executable in the same directory as the
261
 * given GPG path.
262
 * @example
263
 * QString result = findGpgconfInGpgDir(gpgPath);
264
 * std::cout << result.toStdString() << std::endl; // Expected output: path to
265
 * gpgconf or empty string
266
 *
267
 * @param gpgPath - Absolute path to a GPG executable or related file used to
268
 * locate gpgconf.
269
 * @return QString - The full path to gpgconf if found and executable; otherwise
270
 * an empty QString.
271
 */
272
QString findGpgconfInGpgDir(const QString &gpgPath) {
1✔
273
  QFileInfo gpgInfo(gpgPath);
1✔
274
  if (!gpgInfo.isAbsolute()) {
1✔
275
    return QString();
276
  }
277

278
  QDir dir(gpgInfo.absolutePath());
1✔
279

280
#ifdef Q_OS_WIN
281
  QFileInfo candidateExe(dir.filePath("gpgconf.exe"));
282
  if (candidateExe.isExecutable()) {
283
    return candidateExe.filePath();
284
  }
285
#endif
286

287
  QFileInfo candidate(dir.filePath("gpgconf"));
1✔
288
  if (candidate.isExecutable()) {
1✔
289
    return candidate.filePath();
×
290
  }
291
  return QString();
292
}
1✔
293

294
// Compatibility shim for Qt < 5.15 where QProcess::splitCommand is not
295
// available. Keep this fallback while supporting pre-5.15 builds; remove once
296
// the project's minimum supported Qt version is raised to 5.15 or newer.
297
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
298
/**
299
 * @brief Splits a command string into arguments while respecting quotes and
300
 * escape characters.
301
 * @example
302
 * QStringList result = splitCommandCompat("cmd \"arg one\" 'arg two'
303
 * escaped\\ space");
304
 * // Expected output: ["cmd", "arg one", "arg two", "escaped space"]
305
 *
306
 * @param command - The input command string to split into individual arguments.
307
 * @return QStringList - A list of parsed command arguments.
308
 */
309
QStringList splitCommandCompat(const QString &command) {
310
  QStringList result;
311
  QString current;
312
  bool inSingleQuote = false;
313
  bool inDoubleQuote = false;
314
  bool escaping = false;
315
  for (QChar ch : command) {
316
    if (escaping) {
317
      current.append(ch);
318
      escaping = false;
319
      continue;
320
    }
321
    if (ch == '\\') {
322
      escaping = true;
323
      continue;
324
    }
325
    if (ch == '\'' && !inDoubleQuote) {
326
      inSingleQuote = !inSingleQuote;
327
      continue;
328
    }
329
    if (ch == '"' && !inSingleQuote) {
330
      inDoubleQuote = !inDoubleQuote;
331
      continue;
332
    }
333
    if (ch.isSpace() && !inSingleQuote && !inDoubleQuote) {
334
      if (!current.isEmpty()) {
335
        result.append(current);
336
        current.clear();
337
      }
338
      continue;
339
    }
340
    current.append(ch);
341
  }
342
  if (escaping) {
343
    current.append('\\');
344
  }
345
  if (!current.isEmpty()) {
346
    result.append(current);
347
  }
348
  return result;
349
}
350
#endif
351

352
} // namespace
353

354
/**
355
 * @brief Resolves the appropriate gpgconf command from a given GPG executable
356
 * path or command string.
357
 * @example
358
 * ResolvedGpgconfCommand result = Pass::resolveGpgconfCommand("wsl.exe
359
 * /usr/bin/gpg"); std::cout << result.first.toStdString() << std::endl; //
360
 * Expected output sample
361
 *
362
 * @param const QString &gpgPath - Path or command string pointing to the GPG
363
 * executable.
364
 * @return ResolvedGpgconfCommand - A pair containing the resolved gpgconf
365
 * command and its arguments.
366
 */
367
auto Pass::resolveGpgconfCommand(const QString &gpgPath)
8✔
368
    -> ResolvedGpgconfCommand {
369
  if (gpgPath.trimmed().isEmpty()) {
8✔
370
    return {"gpgconf", {}};
371
  }
372

373
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
374
  QStringList parts = QProcess::splitCommand(gpgPath);
7✔
375
#else
376
  QStringList parts = splitCommandCompat(gpgPath);
377
#endif
378

379
  if (parts.isEmpty()) {
7✔
380
    return {"gpgconf", {}};
381
  }
382

383
  const QString first = parts.first();
384
  if (first.compare("wsl", Qt::CaseInsensitive) == 0 ||
16✔
385
      first.compare("wsl.exe", Qt::CaseInsensitive) == 0) {
9✔
386
    if (parts.size() >= 2 && parts.at(1).startsWith("sh")) {
9✔
387
      return {"gpgconf", {}};
388
    }
389
    if (parts.size() >= 2 &&
4✔
390
        QFileInfo(parts.last()).fileName().startsWith("gpg")) {
10✔
391
      QString wslGpgconf = resolveWslGpgconfPath(parts.last());
3✔
392
      parts.removeLast();
3✔
393
      parts.append(wslGpgconf);
394
      return {parts.first(), parts.mid(1)};
395
    }
396
    return {"gpgconf", {}};
397
  }
398

399
  if (!first.contains('/') && !first.contains('\\')) {
2✔
400
    return {"gpgconf", {}};
401
  }
402

403
  QString gpgconfPath = findGpgconfInGpgDir(first);
1✔
404
  if (!gpgconfPath.isEmpty()) {
1✔
405
    return {gpgconfPath, {}};
×
406
  }
407

408
  return {"gpgconf", {}};
409
}
8✔
410

411
/**
412
 * @brief Pass::GenerateGPGKeys internal gpg keypair generator . .
413
 * @param batch GnuPG style configuration string
414
 */
415
void Pass::GenerateGPGKeys(QString batch) {
×
416
  // Kill any stale GPG agents that might be holding locks on the key database
417
  // This helps avoid "database locked" timeouts during key generation
418
  QString gpgPath = QtPassSettings::getGpgExecutable();
×
419
  if (!gpgPath.isEmpty()) {
×
420
    ResolvedGpgconfCommand resolvedGpgconf = resolveGpgconfCommand(gpgPath);
×
421
    QStringList killArgs = resolvedGpgconf.arguments;
422
    killArgs << "--kill";
×
423
    killArgs << "gpg-agent";
×
424
    // Use same environment as key generation to target correct gpg-agent
NEW
425
    if (Executor::executeBlocking(env, resolvedGpgconf.program, killArgs) !=
×
426
        0) {
NEW
427
      qWarning() << "Failed to kill gpg-agent";
×
428
    }
429
  }
430

431
  executeWrapper(GPG_GENKEYS, gpgPath, {"--gen-key", "--no-tty", "--batch"},
×
432
                 std::move(batch));
433
}
×
434

435
/**
436
 * @brief Pass::listKeys list users
437
 * @param keystrings
438
 * @param secret list private keys
439
 * @return QList<UserInfo> users
440
 */
441
auto Pass::listKeys(QStringList keystrings, bool secret) -> QList<UserInfo> {
×
442
  QStringList args = {"--no-tty", "--with-colons", "--with-fingerprint"};
×
443
  args.append(secret ? "--list-secret-keys" : "--list-keys");
×
444

445
  for (const QString &keystring : AS_CONST(keystrings)) {
×
446
    if (!keystring.isEmpty()) {
×
447
      args.append(keystring);
448
    }
449
  }
450
  QString p_out;
×
451
  if (Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
452
                                &p_out) != 0) {
453
    return QList<UserInfo>();
×
454
  }
455
  return parseGpgColonOutput(p_out, secret);
×
456
}
×
457

458
/**
459
 * @brief Pass::listKeys list users
460
 * @param keystring
461
 * @param secret list private keys
462
 * @return QList<UserInfo> users
463
 */
464
auto Pass::listKeys(const QString &keystring, bool secret) -> QList<UserInfo> {
×
465
  return listKeys(QStringList(keystring), secret);
×
466
}
467

468
/**
469
 * @brief Maps GPG stderr (which may include --status-fd 2 tokens) to a
470
 * user-friendly encryption error string.
471
 *
472
 * Checked in order: machine-readable [GNUPG:] status tokens first (locale-
473
 * independent), then case-insensitive substring fallbacks for GPG builds that
474
 * don't emit status tokens.
475
 *
476
 * @param err Raw stderr from GPG
477
 * @return Translated human-readable error, or empty string if not recognised
478
 */
479
namespace {
480

481
auto containsAny(const QString &str, const QStringList &patterns) -> bool {
31✔
482
  for (const QString &p : patterns) {
82✔
483
    if (str.contains(p)) {
58✔
484
      return true;
485
    }
486
  }
487
  return false;
488
}
489

490
/**
491
 * @brief Checks if str contains any of the patterns (case-insensitive).
492
 * @param str String to search in (will be lowercased once).
493
 * @param patterns List of patterns to search for (must be lowercase; caller
494
 * should convert patterns to lowercase before calling).
495
 * @return true if any pattern is found.
496
 */
497
auto containsAnyCaseInsensitive(const QString &str, const QStringList &patterns)
13✔
498
    -> bool {
499
  const QString lower = str.toLower();
500
  for (const QString &p : patterns) {
32✔
501
    if (lower.contains(p)) {
23✔
502
      return true;
503
    }
504
  }
505
  return false;
506
}
507

508
} // namespace
509

510
auto gpgErrorMessage(const QString &err) -> QString {
13✔
511
  // Machine-readable status tokens added by --status-fd 2
512
  if (containsAny(err, {QStringLiteral("[GNUPG:] KEYEXPIRED"),
65✔
513
                        QStringLiteral("[GNUPG:] INV_RECP 5 ")}))
13✔
514
    return QCoreApplication::translate(
515
        "Pass", "Encryption failed: GPG key has expired. Please renew or "
516
                "replace it.");
3✔
517
  if (containsAny(err, {QStringLiteral("[GNUPG:] KEYREVOKED"),
50✔
518
                        QStringLiteral("[GNUPG:] INV_RECP 4 ")}))
10✔
519
    return QCoreApplication::translate(
520
        "Pass", "Encryption failed: GPG key has been revoked.");
2✔
521
  if (containsAny(err, {QStringLiteral("[GNUPG:] NO_PUBKEY"),
40✔
522
                        QStringLiteral("[GNUPG:] INV_RECP")}))
8✔
523
    return QCoreApplication::translate(
524
        "Pass", "Encryption failed: recipient GPG key not found or invalid. "
525
                "Check that the key ID in .gpg-id is correct and imported.");
2✔
526
  if (err.contains(QStringLiteral("[GNUPG:] FAILURE")))
6✔
527
    return QCoreApplication::translate(
528
        "Pass", "Encryption failed. Check that your GPG key is valid.");
1✔
529

530
  // Locale-dependent fallbacks
531
  if (containsAnyCaseInsensitive(err, {QLatin1String("key has expired"),
20✔
532
                                       QLatin1String("key expired")}))
533
    return QCoreApplication::translate(
534
        "Pass", "Encryption failed: GPG key has expired. Please renew or "
535
                "replace it.");
1✔
536
  if (containsAnyCaseInsensitive(err, {QLatin1String("key has been revoked"),
16✔
537
                                       QLatin1String("revoked")}))
538
    return QCoreApplication::translate(
539
        "Pass", "Encryption failed: GPG key has been revoked.");
1✔
540
  if (containsAnyCaseInsensitive(err, {QLatin1String("no public key"),
15✔
541
                                       QLatin1String("unusable public key"),
542
                                       QLatin1String("no secret key")}))
543
    return QCoreApplication::translate(
544
        "Pass", "Encryption failed: recipient GPG key not found or invalid. "
545
                "Check that the key ID in .gpg-id is correct and imported.");
2✔
546
  if (containsAnyCaseInsensitive(err, {QLatin1String("encryption failed")}))
3✔
547
    return QCoreApplication::translate(
548
        "Pass", "Encryption failed. Check that your GPG key is valid.");
×
549

550
  return {};
551
}
×
552

553
namespace {
554
auto isGrepHeaderLine(const QString &rawLine, const QString &trimmedLine)
45✔
555
    -> bool {
556
  // ANSI-colored header starts with the blue escape; plain-text fallback:
557
  // a non-indented line ending with ':' (pass grep format without color)
558
  return rawLine.startsWith(QStringLiteral("\x1B[94m")) ||
123✔
559
         (!rawLine.startsWith(' ') && !rawLine.startsWith('\t') &&
91✔
560
          trimmedLine.endsWith(':'));
119✔
561
}
562
} // namespace
563

564
/**
565
 * @brief Parses 'pass grep' raw output into (entry, matches) pairs.
566
 *
567
 * pass grep emits ANSI blue color (\x1B[94m) at the start of each entry
568
 * header line. This is checked before stripping ANSI so headers are detected
569
 * reliably regardless of locale.
570
 */
571
auto parseGrepOutput(const QString &rawOut)
12✔
572
    -> QList<QPair<QString, QStringList>> {
573
  static const QRegularExpression ansi(
574
      QStringLiteral(R"(\x1B\[[0-9;]*[a-zA-Z])"));
13✔
575
  QList<QPair<QString, QStringList>> results;
12✔
576
  QString currentEntry;
12✔
577
  QStringList currentMatches;
12✔
578
  for (const QString &rawLine : rawOut.split('\n')) {
69✔
579
    QString line = rawLine;
580
    line.remove('\r');
45✔
581
    line.remove(ansi);
45✔
582
    line = line.trimmed();
45✔
583
    const bool isHeader = isGrepHeaderLine(rawLine, line);
45✔
584
    if (isHeader) {
45✔
585
      if (!currentEntry.isEmpty() && !currentMatches.isEmpty())
14✔
586
        results.append({currentEntry, currentMatches});
3✔
587
      currentEntry = line.endsWith(':') ? line.chopped(1) : line;
14✔
588
      currentMatches.clear();
14✔
589
    } else if (!currentEntry.isEmpty()) {
31✔
590
      if (!line.isEmpty())
29✔
591
        currentMatches << line;
592
    }
593
  }
594
  if (!currentEntry.isEmpty() && !currentMatches.isEmpty())
12✔
595
    results.append({currentEntry, currentMatches});
11✔
596
  return results;
12✔
597
}
598

599
/**
600
 * @brief Pass::processFinished reemits specific signal based on what process
601
 * has finished
602
 * @param id    id of Pass process that was scheduled and finished
603
 * @param exitCode  return code of a process
604
 * @param out   output generated by process(if capturing was requested, empty
605
 *              otherwise)
606
 * @param err   error output generated by process(if capturing was requested,
607
 *              or error occurred)
608
 */
609
void Pass::finished(int id, int exitCode, const QString &out,
18✔
610
                    const QString &err) {
611
  auto pid = static_cast<PROCESS>(id);
18✔
612

613
  if (exitCode != 0) {
18✔
614
    handleProcessError(pid, exitCode, out, err);
2✔
615
    return;
2✔
616
  }
617

618
  emitProcessFinishedSignal(pid, out, err);
16✔
619
}
620

621
void Pass::handleProcessError(PROCESS pid, int exitCode, const QString &out,
2✔
622
                              const QString &err) {
623
  if (pid == PASS_GREP) {
2✔
624
    handleGrepError(exitCode, err);
2✔
625
    return;
2✔
626
  }
627

628
  if (pid == PASS_INSERT) {
×
629
    const QString friendly = gpgErrorMessage(err);
×
630
    if (!friendly.isEmpty()) {
×
631
      emit processErrorExit(exitCode, formatInsertError(friendly, err));
×
632
      return;
633
    }
634
  }
635

636
  emit processErrorExit(exitCode, err);
×
637
}
638

639
void Pass::handleGrepError(int exitCode, const QString &err) {
2✔
640
  if (exitCode == 1) {
2✔
641
    emit finishedGrep({});
2✔
642
  } else {
643
    emit processErrorExit(exitCode, err);
1✔
644
    emit finishedGrep({});
2✔
645
  }
646
}
2✔
647

648
auto Pass::formatInsertError(const QString &friendly, const QString &err)
×
649
    -> QString {
650
  QStringList humanLines;
×
651
  for (const QString &line : err.split('\n')) {
×
652
    QString cleanedLine = line;
653
    cleanedLine.remove('\r');
×
654
    if (!cleanedLine.startsWith(QLatin1String("[GNUPG:]")))
×
655
      humanLines.append(cleanedLine);
656
  }
657
  const QString humanErr = humanLines.join('\n').trimmed();
×
658
  return humanErr.isEmpty() ? friendly : friendly + "\n\n" + humanErr;
×
659
}
660

661
/**
662
 * @brief Emit the appropriate finished signal for a completed subprocess.
663
 *
664
 * Emits a specific Qt signal corresponding to the given process identifier; for
665
 * grep results the stdout is parsed into a list of matches before emitting.
666
 *
667
 * @param pid The process identifier indicating which finished signal to emit.
668
 * @param out Standard output produced by the process.
669
 * @param err Standard error produced by the process.
670
 */
671
void Pass::emitProcessFinishedSignal(PROCESS pid, const QString &out,
16✔
672
                                     const QString &err) {
673
  switch (pid) {
16✔
674
  case GIT_INIT:
×
675
    emit finishedGitInit(out, err);
×
676
    break;
×
677
  case GIT_PULL:
×
678
    emit finishedGitPull(out, err);
×
679
    break;
×
680
  case GIT_PUSH:
×
681
    emit finishedGitPush(out, err);
×
682
    break;
×
683
  case PASS_SHOW:
5✔
684
    emit finishedShow(out);
5✔
685
    break;
5✔
686
  case PASS_OTP_GENERATE:
×
687
    emit finishedOtpGenerate(out);
×
688
    break;
×
689
  case PASS_INSERT:
10✔
690
    emit finishedInsert(out, err);
10✔
691
    break;
10✔
692
  case PASS_REMOVE:
×
693
    emit finishedRemove(out, err);
×
694
    break;
×
695
  case PASS_INIT:
×
696
    emit finishedInit(out, err);
×
697
    break;
×
698
  case PASS_MOVE:
×
699
    emit finishedMove(out, err);
×
700
    break;
×
701
  case PASS_COPY:
×
702
    emit finishedCopy(out, err);
×
703
    break;
×
704
  case GPG_GENKEYS:
×
705
    emit finishedGenerateGPGKeys(out, err);
×
706
    break;
×
707
  case PASS_GREP:
1✔
708
    emit finishedGrep(parseGrepOutput(out));
1✔
709
    break;
1✔
710
  default:
711
#ifdef QT_DEBUG
712
    dbg() << "Unhandled process type" << pid;
713
#endif
714
    break;
715
  }
716
}
16✔
717

718
/**
719
 * @brief Set or remove a single environment variable in the local env list.
720
 *
721
 * The provided key must include a trailing '=' (e.g. "FOO="). Existing entries
722
 * whose text begins with the given key are removed before the new value is
723
 * applied. If value is non-empty the pair "key+value" is appended; if value is
724
 * empty the variable is removed.
725
 *
726
 * The function asserts if key does not end with '='; if the assertion is not
727
 * active it will emit a warning and return without modifying env.
728
 *
729
 * @param key Environment variable name with trailing '=' (anchors the lookup).
730
 * @param value Value to set for the variable; an empty string unsets the
731
 * variable.
732
 */
733
void Pass::setEnvVar(const QString &key, const QString &value) {
70✔
734
  const bool hasEq = key.endsWith('=');
70✔
735
  Q_ASSERT_X(hasEq, "Pass::setEnvVar",
736
             "called with malformed key (missing '=')");
737
  if (!hasEq) {
70✔
738
    qWarning() << "Pass::setEnvVar called with malformed key (missing '='):"
×
739
               << key;
×
740
    return;
×
741
  }
742
  env.erase(std::remove_if(
140✔
743
                env.begin(), env.end(),
744
                [&key](const QString &entry) { return entry.startsWith(key); }),
9,164✔
745
            env.end());
746
  if (!value.isEmpty())
70✔
747
    env.append(key + value);
104✔
748
}
749

750
/**
751
 * @brief Update the process environment used for executing external commands.
752
 *
753
 * Updates environment entries for PASSWORD_STORE_SIGNING_KEY,
754
 * PASSWORD_STORE_DIR, PASSWORD_STORE_GENERATED_LENGTH, and
755
 * PASSWORD_STORE_CHARACTER_SET based on current settings, then applies the
756
 * environment to the internal executor.
757
 */
758
void Pass::updateEnv() {
16✔
759
  setEnvVar(QStringLiteral("PASSWORD_STORE_SIGNING_KEY="),
32✔
760
            QtPassSettings::getPassSigningKey());
32✔
761
  setEnvVar(QStringLiteral("PASSWORD_STORE_DIR="),
32✔
762
            QtPassSettings::getPassStore());
32✔
763

764
  PasswordConfiguration passConfig = QtPassSettings::getPasswordConfiguration();
16✔
765
  setEnvVar(QStringLiteral("PASSWORD_STORE_GENERATED_LENGTH="),
32✔
766
            QString::number(passConfig.length));
16✔
767

768
  setEnvVar(QStringLiteral("PASSWORD_STORE_CHARACTER_SET="),
32✔
769
            effectiveCharset(passConfig));
16✔
770

771
  exec.setEnvironment(env);
16✔
772
}
16✔
773

774
/**
775
 * @brief Pass::getGpgIdPath return gpgid file path for some file (folder).
776
 * @param for_file which file (folder) would you like the gpgid file path for.
777
 * @return path to the gpgid file.
778
 */
779
auto Pass::getGpgIdPath(const QString &for_file) -> QString {
32✔
780
  QString passStore =
781
      QDir::fromNativeSeparators(QtPassSettings::getPassStore());
64✔
782
  QString normalizedFile = QDir::fromNativeSeparators(for_file);
32✔
783
  QString fullPath = normalizedFile.startsWith(passStore)
32✔
784
                         ? normalizedFile
32✔
785
                         : passStore + "/" + normalizedFile;
27✔
786
  QDir gpgIdDir(QFileInfo(fullPath).absoluteDir());
32✔
787
  bool found = false;
788
  while (gpgIdDir.exists() && gpgIdDir.absolutePath().startsWith(passStore)) {
81✔
789
    if (QFile(gpgIdDir.absoluteFilePath(".gpg-id")).exists()) {
26✔
790
      found = true;
791
      break;
792
    }
793
    if (!gpgIdDir.cdUp()) {
12✔
794
      break;
795
    }
796
  }
797
  QString gpgIdPath(
798
      found ? gpgIdDir.absoluteFilePath(".gpg-id")
32✔
799
            : QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"));
125✔
800

801
  return gpgIdPath;
32✔
802
}
32✔
803

804
/**
805
 * @brief Pass::getRecipientList return list of gpg-id's to encrypt for
806
 * @param for_file which file (folder) would you like recipients for
807
 * @return recipients gpg-id contents
808
 */
809
auto Pass::getRecipientList(const QString &for_file) -> QStringList {
17✔
810
  QFile gpgId(getGpgIdPath(for_file));
17✔
811
  if (!gpgId.open(QIODevice::ReadOnly | QIODevice::Text)) {
17✔
812
    return {};
×
813
  }
814
  QStringList recipients;
17✔
815
  while (!gpgId.atEnd()) {
42✔
816
    QString recipient(gpgId.readLine());
50✔
817
    recipient = recipient.split("#")[0].trimmed();
50✔
818
    if (!recipient.isEmpty() && Util::isValidKeyId(recipient)) {
25✔
819
      recipients += recipient;
820
    }
821
  }
822
  return recipients;
823
}
17✔
824

825
/**
826
 * @brief Pass::getRecipientString formatted string for use with GPG
827
 * @param for_file which file (folder) would you like recipients for
828
 * @param separator formating separator eg: " -r "
829
 * @param count
830
 * @return recipient string
831
 */
832
auto Pass::getRecipientString(const QString &for_file, const QString &separator,
2✔
833
                              int *count) -> QStringList {
834
  Q_UNUSED(separator)
835
  QStringList recipients = Pass::getRecipientList(for_file);
2✔
836
  if (count) {
2✔
837
    *count = recipients.size();
1✔
838
  }
839
  return recipients;
2✔
840
}
841

842
/* Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
843
 */
844

845
/**
846
 * @brief Generates a random number bounded by the given value.
847
 * @param bound Upper bound (exclusive)
848
 * @return Random number in range [0, bound)
849
 */
850
auto Pass::boundedRandom(quint32 bound) -> quint32 {
1,224✔
851
  if (bound < 2) {
1,224✔
852
    return 0;
853
  }
854

855
  quint32 randval;
856
  // Rejection-sampling threshold to avoid modulo bias.
857
  // This follows the well-known "arc4random_uniform"-style approach:
858
  // reject values in the low range [0, min), where
859
  //   min = 2^32 % bound
860
  // so that the remaining range size is an exact multiple of `bound`.
861
  //
862
  // In quint32 arithmetic, (1 + ~bound) wraps to (2^32 - bound), therefore
863
  //   (1 + ~bound) % bound == 2^32 % bound.
864
  const quint32 rejectionThreshold = (1 + ~bound) % bound;
1,224✔
865

866
  do {
867
    randval = QRandomGenerator::system()->generate();
868
  } while (randval < rejectionThreshold);
1,224✔
869

870
  return randval % bound;
1,224✔
871
}
872

873
/**
874
 * @brief Generates a random password from the given charset.
875
 * @param charset Characters to use in the password
876
 * @param length Desired password length
877
 * @return Generated password string
878
 */
879
auto Pass::generateRandomPassword(const QString &charset, unsigned int length)
1,005✔
880
    -> QString {
881
  if (charset.isEmpty() || length == 0U) {
1,005✔
882
    return {};
883
  }
884
  QString out;
1,005✔
885
  for (unsigned int i = 0; i < length; ++i) {
2,229✔
886
    out.append(charset.at(static_cast<int>(
1,224✔
887
        boundedRandom(static_cast<quint32>(charset.length())))));
1,224✔
888
  }
889
  return out;
890
}
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