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

IJHack / QtPass / 23900400535

02 Apr 2026 12:29PM UTC coverage: 19.969% (-0.1%) from 20.086%
23900400535

Pull #891

github

web-flow
Merge c76638c3a into ccb7812af
Pull Request #891: fix: improve re-encryption security

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

1 existing line in 1 file now uncovered.

1030 of 5158 relevant lines covered (19.97%)

7.84 hits per line

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

6.5
/src/imitatepass.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "imitatepass.h"
4
#include "qtpasssettings.h"
5
#include "util.h"
6
#include <QDirIterator>
7
#include <QRegularExpression>
8
#include <utility>
9

10
#ifdef QT_DEBUG
11
#include "debughelper.h"
12
#endif
13

14
using Enums::CLIPBOARD_ALWAYS;
15
using Enums::CLIPBOARD_NEVER;
16
using Enums::CLIPBOARD_ON_DEMAND;
17
using Enums::GIT_ADD;
18
using Enums::GIT_COMMIT;
19
using Enums::GIT_COPY;
20
using Enums::GIT_INIT;
21
using Enums::GIT_MOVE;
22
using Enums::GIT_PULL;
23
using Enums::GIT_PUSH;
24
using Enums::GIT_RM;
25
using Enums::GPG_GENKEYS;
26
using Enums::INVALID;
27
using Enums::PASS_COPY;
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
using Enums::PROCESS_COUNT;
35

36
/**
37
 * @brief ImitatePass::ImitatePass for situaions when pass is not available
38
 * we imitate the behavior of pass https://www.passwordstore.org/
39
 */
40
ImitatePass::ImitatePass() = default;
20✔
41

42
static auto pgit(const QString &path) -> QString {
×
43
  if (!QtPassSettings::getGitExecutable().startsWith("wsl ")) {
×
44
    return path;
45
  }
46
  QString res = "$(wslpath " + path + ")";
×
47
  return res.replace('\\', '/');
×
48
}
49

50
static auto pgpg(const QString &path) -> QString {
×
51
  if (!QtPassSettings::getGpgExecutable().startsWith("wsl ")) {
×
52
    return path;
53
  }
54
  QString res = "$(wslpath " + path + ")";
×
55
  return res.replace('\\', '/');
×
56
}
57

58
/**
59
 * @brief ImitatePass::GitInit git init wrapper
60
 */
61
void ImitatePass::GitInit() {
×
62
  executeGit(GIT_INIT, {"init", pgit(QtPassSettings::getPassStore())});
×
63
}
×
64

65
/**
66
 * @brief ImitatePass::GitPull git init wrapper
67
 */
68
void ImitatePass::GitPull() { executeGit(GIT_PULL, {"pull"}); }
×
69

70
/**
71
 * @brief ImitatePass::GitPull_b git pull wrapper
72
 */
73
void ImitatePass::GitPull_b() {
×
74
  Executor::executeBlocking(QtPassSettings::getGitExecutable(), {"pull"});
×
75
}
×
76

77
/**
78
 * @brief ImitatePass::GitPush git init wrapper
79
 */
80
void ImitatePass::GitPush() {
×
81
  if (QtPassSettings::isUseGit()) {
×
82
    executeGit(GIT_PUSH, {"push"});
×
83
  }
84
}
×
85

86
/**
87
 * @brief ImitatePass::Show shows content of file
88
 */
89
void ImitatePass::Show(QString file) {
×
90
  file = QtPassSettings::getPassStore() + file + ".gpg";
×
91
  QStringList args = {"-d",      "--quiet",     "--yes",   "--no-encrypt-to",
92
                      "--batch", "--use-agent", pgpg(file)};
×
93
  executeGpg(PASS_SHOW, args);
×
94
}
×
95

96
/**
97
 * @brief ImitatePass::OtpGenerate generates an otp code
98
 */
99
void ImitatePass::OtpGenerate(QString file) {
×
100
#ifdef QT_DEBUG
101
  dbg() << "No OTP generation code for fake pass yet, attempting for file: " +
102
               file;
103
#else
104
  Q_UNUSED(file)
105
#endif
106
}
×
107

108
/**
109
 * @brief ImitatePass::Insert create new file with encrypted content
110
 *
111
 * @param file      file to be created
112
 * @param newValue  value to be stored in file
113
 * @param overwrite whether to overwrite existing file
114
 */
115
void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
×
116
  file = file + ".gpg";
×
117
  QString gpgIdPath = Pass::getGpgIdPath(file);
×
118
  if (!verifyGpgIdFile(gpgIdPath)) {
×
119
    emit critical(tr("Check .gpgid file signature!"),
×
120
                  tr("Signature for %1 is invalid.").arg(gpgIdPath));
×
121
    return;
×
122
  }
123
  transactionHelper trans(this, PASS_INSERT);
×
124
  QStringList recipients = Pass::getRecipientList(file);
×
125
  if (recipients.isEmpty()) {
×
126
    // Already emit critical signal to notify user of error - no need to throw
127
    emit critical(tr("Can not edit"),
×
128
                  tr("Could not read encryption key to use, .gpg-id "
×
129
                     "file missing or invalid."));
130
    return;
131
  }
132
  QStringList args = {"--batch", "-eq", "--output", pgpg(file)};
×
133
  for (auto &r : recipients) {
×
134
    args.append("-r");
×
135
    args.append(r);
136
  }
137
  if (overwrite) {
×
138
    args.append("--yes");
×
139
  }
140
  args.append("-");
×
141
  executeGpg(PASS_INSERT, args, newValue);
×
142
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
×
143
    // Git is used when enabled - this is the standard pass workflow
144
    if (!overwrite) {
×
145
      executeGit(GIT_ADD, {"add", pgit(file)});
×
146
    }
147
    QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
×
148
    path.replace(Util::endsWithGpg(), "");
×
149
    QString msg =
150
        QString(overwrite ? "Edit" : "Add") + " for " + path + " using QtPass.";
×
151
    GitCommit(file, msg);
×
152
  }
153
}
×
154

155
/**
156
 * @brief ImitatePass::GitCommit commit a file to git with an appropriate commit
157
 * message
158
 * @param file
159
 * @param msg
160
 */
161
void ImitatePass::GitCommit(const QString &file, const QString &msg) {
×
162
  if (file.isEmpty()) {
×
163
    executeGit(GIT_COMMIT, {"commit", "-m", msg});
×
164
  } else {
165
    executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", pgit(file)});
×
166
  }
167
}
×
168

169
/**
170
 * @brief ImitatePass::Remove custom implementation of "pass remove"
171
 */
172
void ImitatePass::Remove(QString file, bool isDir) {
×
173
  file = QtPassSettings::getPassStore() + file;
×
174
  transactionHelper trans(this, PASS_REMOVE);
×
175
  if (!isDir) {
×
176
    file += ".gpg";
×
177
  }
178
  if (QtPassSettings::isUseGit()) {
×
179
    executeGit(GIT_RM, {"rm", (isDir ? "-rf" : "-f"), pgit(file)});
×
180
    // Normalize path the same way as add/edit operations
181
    QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
×
182
    path.replace(Util::endsWithGpg(), "");
×
183
    GitCommit(file, "Remove for " + path + " using QtPass.");
×
184
  } else {
185
    if (isDir) {
×
186
      QDir dir(file);
×
187
      dir.removeRecursively();
×
188
    } else {
×
189
      QFile(file).remove();
×
190
    }
191
  }
192
}
×
193

194
/**
195
 * @brief ImitatePass::Init initialize pass repository
196
 *
197
 * @param path      path in which new password-store will be created
198
 * @param users     list of users who shall be able to decrypt passwords in
199
 * path
200
 */
201
auto ImitatePass::checkSigningKeys(const QStringList &signingKeys) -> bool {
×
202
  QString out;
×
203
  QStringList args =
204
      QStringList{"--status-fd=1", "--list-secret-keys"} + signingKeys;
×
205
  int result =
206
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &out);
×
207
  if (result != 0) {
×
208
#ifdef QT_DEBUG
209
    dbg() << "GPG list-secret-keys failed with code:" << result;
210
#endif
211
    return false;
212
  }
213
  for (auto &key : signingKeys) {
×
214
    if (out.contains("[GNUPG:] KEY_CONSIDERED " + key)) {
×
215
      return true;
216
    }
217
  }
218
  return false;
219
}
×
220

221
void ImitatePass::writeGpgIdFile(const QString &gpgIdFile,
×
222
                                 const QList<UserInfo> &users) {
223
  QFile gpgId(gpgIdFile);
×
224
  if (!gpgId.open(QIODevice::WriteOnly | QIODevice::Text)) {
×
225
    emit critical(tr("Cannot update"),
×
226
                  tr("Failed to open .gpg-id for writing."));
×
227
    return;
228
  }
229
  bool secret_selected = false;
230
  for (const UserInfo &user : users) {
×
231
    if (user.enabled) {
×
232
      gpgId.write((user.key_id + "\n").toUtf8());
×
233
      secret_selected |= user.have_secret;
×
234
    }
235
  }
236
  gpgId.close();
×
237
  if (!secret_selected) {
×
238
    emit critical(
×
239
        tr("Check selected users!"),
×
240
        tr("None of the selected keys have a secret key available.\n"
×
241
           "You will not be able to decrypt any newly added passwords!"));
242
  }
243
}
×
244

245
auto ImitatePass::signGpgIdFile(const QString &gpgIdFile,
×
246
                                const QStringList &signingKeys) -> bool {
247
  QStringList args;
×
248
  // Use only the first signing key; multiple --default-key options would
249
  // override each other and only the last one would take effect.
250
  if (!signingKeys.isEmpty()) {
×
251
#ifdef QT_DEBUG
252
    if (signingKeys.size() > 1) {
253
      dbg() << "Multiple signing keys configured; using only the first key:"
254
            << signingKeys.first();
255
    }
256
#endif
257
    args.append(QStringList{"--default-key", signingKeys.first()});
×
258
  }
259
  args.append(QStringList{"--yes", "--detach-sign", gpgIdFile});
×
260
  int result =
261
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args);
×
262
  if (result != 0) {
×
263
#ifdef QT_DEBUG
264
    dbg() << "GPG signing failed with code:" << result;
265
#endif
266
    emit critical(tr("GPG signing failed!"),
×
267
                  tr("Failed to sign %1.").arg(gpgIdFile));
×
268
    return false;
×
269
  }
270
  if (!verifyGpgIdFile(gpgIdFile)) {
×
271
    emit critical(tr("Check .gpgid file signature!"),
×
272
                  tr("Signature for %1 is invalid.").arg(gpgIdFile));
×
273
    return false;
×
274
  }
275
  return true;
276
}
×
277

278
void ImitatePass::gitAddGpgId(const QString &gpgIdFile,
×
279
                              const QString &gpgIdSigFile, bool addFile,
280
                              bool addSigFile) {
281
  if (addFile) {
×
282
    executeGit(GIT_ADD, {"add", pgit(gpgIdFile)});
×
283
  }
284
  QString commitPath = gpgIdFile;
285
  commitPath.replace(Util::endsWithGpg(), "");
×
286
  GitCommit(gpgIdFile, "Added " + commitPath + " using QtPass.");
×
287
  if (!addSigFile) {
×
288
    return;
289
  }
290
  executeGit(GIT_ADD, {"add", pgit(gpgIdSigFile)});
×
291
  commitPath = gpgIdSigFile;
×
292
  commitPath.replace(QRegularExpression("\\.gpg$"), "");
×
293
  GitCommit(gpgIdSigFile, "Added " + commitPath + " using QtPass.");
×
294
}
×
295

296
void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
×
297
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
298
  QStringList signingKeys =
299
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
300
#else
301
  QStringList signingKeys =
302
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
303
#endif
304
  QString gpgIdSigFile = path + ".gpg-id.sig";
×
305
  bool addSigFile = false;
306
  if (!signingKeys.isEmpty()) {
×
307
    if (!checkSigningKeys(signingKeys)) {
×
308
      emit critical(tr("No signing key!"),
×
309
                    tr("None of the secret signing keys is available.\n"
×
310
                       "You will not be able to change the user list!"));
311
      return;
×
312
    }
313
    QFileInfo checkFile(gpgIdSigFile);
×
314
    if (!checkFile.exists() || !checkFile.isFile()) {
×
315
      addSigFile = true;
316
    }
317
  }
×
318

319
  QString gpgIdFile = path + ".gpg-id";
×
320
  bool addFile = false;
321
  transactionHelper trans(this, PASS_INIT);
×
322
  if (QtPassSettings::isAddGPGId(true)) {
×
323
    QFileInfo checkFile(gpgIdFile);
×
324
    if (!checkFile.exists() || !checkFile.isFile()) {
×
325
      addFile = true;
326
    }
327
  }
×
328
  writeGpgIdFile(gpgIdFile, users);
×
329

330
  if (!signingKeys.isEmpty()) {
×
331
    if (!signGpgIdFile(gpgIdFile, signingKeys)) {
×
332
      return;
333
    }
334
  }
335

336
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() &&
×
337
      !QtPassSettings::getGitExecutable().isEmpty()) {
×
338
    gitAddGpgId(gpgIdFile, gpgIdSigFile, addFile, addSigFile);
×
339
  }
340
  reencryptPath(path);
×
341
}
342

343
/**
344
 * @brief ImitatePass::verifyGpgIdFile verify detached gpgid file signature.
345
 * @param file which gpgid file.
346
 * @return was verification succesful?
347
 */
348
auto ImitatePass::verifyGpgIdFile(const QString &file) -> bool {
×
349
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
350
  QStringList signingKeys =
351
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
352
#else
353
  QStringList signingKeys =
354
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
355
#endif
356
  if (signingKeys.isEmpty()) {
×
357
    return true;
358
  }
359
  QString out;
×
360
  QStringList args =
361
      QStringList{"--verify", "--status-fd=1", pgpg(file) + ".sig", pgpg(file)};
×
362
  int result =
363
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &out);
×
364
  if (result != 0) {
×
365
#ifdef QT_DEBUG
366
    dbg() << "GPG verify failed with code:" << result;
367
#endif
368
    return false;
369
  }
370
  QRegularExpression re(
371
      R"(^\[GNUPG:\] VALIDSIG ([A-F0-9]{40}) .* ([A-F0-9]{40})\r?$)",
372
      QRegularExpression::MultilineOption);
×
373
  QRegularExpressionMatch m = re.match(out);
×
374
  if (!m.hasMatch()) {
×
375
    return false;
376
  }
377
  QStringList fingerprints = m.capturedTexts();
×
378
  fingerprints.removeFirst();
×
379
  for (auto &key : signingKeys) {
×
380
    if (fingerprints.contains(key)) {
×
381
      return true;
×
382
    }
383
  }
384
  return false;
×
385
}
×
386

387
/**
388
 * @brief ImitatePass::removeDir delete folder recursive.
389
 * @param dirName which folder.
390
 * @return was removal succesful?
391
 */
392
auto ImitatePass::removeDir(const QString &dirName) -> bool {
1✔
393
  bool result = true;
394
  QDir dir(dirName);
1✔
395

396
  if (dir.exists(dirName)) {
1✔
397
    for (const QFileInfo &info :
398
         dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
399
                               QDir::AllDirs | QDir::Files,
400
                           QDir::DirsFirst)) {
1✔
401
      if (info.isDir()) {
×
402
        result = removeDir(info.absoluteFilePath());
×
403
      } else {
404
        result = QFile::remove(info.absoluteFilePath());
×
405
      }
406

407
      if (!result) {
×
408
        return result;
409
      }
410
    }
411
    result = dir.rmdir(dirName);
1✔
412
  }
413
  return result;
414
}
1✔
415

416
/**
417
 * @brief ImitatePass::reencryptPath reencrypt all files under the chosen
418
 * directory
419
 *
420
 * This is stil quite experimental..
421
 * @param dir
422
 */
423
void ImitatePass::verifyGpgIdForDir(const QString &file,
×
424
                                    QStringList &gpgIdFilesVerified,
425
                                    QStringList &gpgId) {
426
  QString gpgIdPath = Pass::getGpgIdPath(file);
×
427
  if (gpgIdFilesVerified.contains(gpgIdPath)) {
×
428
    return;
429
  }
430
  if (!verifyGpgIdFile(gpgIdPath)) {
×
431
    emit critical(tr("Check .gpgid file signature!"),
×
432
                  tr("Signature for %1 is invalid.").arg(gpgIdPath));
×
433
    emit endReencryptPath();
×
434
    return;
435
  }
436
  gpgIdFilesVerified.append(gpgIdPath);
437
  gpgId = getRecipientList(file);
×
438
  gpgId.sort();
439
}
440

441
auto ImitatePass::getKeysFromFile(const QString &fileName) -> QStringList {
×
442
  QStringList args = {
443
      "-v",          "--no-secmem-warning", "--no-permission-warning",
444
      "--list-only", "--keyid-format=long", pgpg(fileName)};
×
445
  QString keys;
×
446
  QString err;
×
447
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &keys,
×
448
                            &err);
449
  QStringList actualKeys;
×
450
  keys += err;
451
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
452
  QStringList key = keys.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
×
453
#else
454
  QStringList key = keys.split(Util::newLinesRegex(), QString::SkipEmptyParts);
455
#endif
456
  QListIterator<QString> itr(key);
457
  while (itr.hasNext()) {
×
458
    QString current = itr.next();
459
    QStringList cur = current.split(" ");
×
460
    if (cur.length() > 4) {
×
461
      QString actualKey = cur.takeAt(4);
×
462
      if (actualKey.length() == 16) {
×
463
        actualKeys << actualKey;
464
      }
465
    }
466
  }
467
  actualKeys.sort();
468
  return actualKeys;
×
469
}
×
470

NEW
471
auto ImitatePass::reencryptSingleFile(const QString &fileName,
×
472
                                      const QStringList &recipients) -> bool {
473
#ifdef QT_DEBUG
474
  dbg() << "reencrypt " << fileName << " for " << recipients;
475
#endif
NEW
476
  QString local_lastDecrypt;
×
477
  QStringList args = {
478
      "-d",      "--quiet",     "--yes",       "--no-encrypt-to",
479
      "--batch", "--use-agent", pgpg(fileName)};
×
NEW
480
  int result = Executor::executeBlocking(QtPassSettings::getGpgExecutable(),
×
481
                                         args, &local_lastDecrypt);
482

NEW
483
  if (result != 0 || local_lastDecrypt.isEmpty() ||
×
484
      local_lastDecrypt == "Could not decrypt") {
485
#ifdef QT_DEBUG
486
    dbg() << "Decrypt error on re-encrypt for:" << fileName;
487
#endif
NEW
488
    return false;
×
489
  }
490

491
  if (local_lastDecrypt.right(1) != "\n") {
×
492
    local_lastDecrypt += "\n";
×
493
  }
494

495
  QStringList recipientsList = Pass::getRecipientList(fileName);
×
496
  if (recipientsList.isEmpty()) {
×
497
    emit critical(tr("Can not edit"),
×
498
                  tr("Could not read encryption key to use, .gpg-id "
×
499
                     "file missing or invalid."));
NEW
500
    return false;
×
501
  }
502

503
  // Encrypt to temporary file for atomic replacement
NEW
504
  QString tempPath = fileName + ".reencrypt.tmp";
×
NEW
505
  args = QStringList{"--yes", "--batch", "-eq", "--output", pgpg(tempPath)};
×
506
  for (auto &i : recipientsList) {
×
507
    args.append("-r");
×
508
    args.append(i);
509
  }
510
  args.append("-");
×
NEW
511
  result = Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
512
                                     local_lastDecrypt);
513

NEW
514
  if (result != 0) {
×
515
#ifdef QT_DEBUG
516
    dbg() << "Encrypt error on re-encrypt for:" << fileName;
517
#endif
NEW
518
    QFile::remove(tempPath);
×
519
    return false;
520
  }
521

522
  // Verify encryption worked by attempting to decrypt the temp file
NEW
523
  QString verifyOutput;
×
NEW
524
  args = {"-d", "--quiet", "--batch", "--use-agent", pgpg(tempPath)};
×
NEW
525
  result = Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
526
                                     &verifyOutput);
NEW
527
  if (result != 0 || verifyOutput.isEmpty()) {
×
528
#ifdef QT_DEBUG
529
    dbg() << "Verification failed for:" << tempPath;
530
#endif
NEW
531
    QFile::remove(tempPath);
×
532
    return false;
533
  }
534

535
  // Atomic replace: remove original, rename temp to original
NEW
536
  if (!QFile::remove(fileName)) {
×
537
#ifdef QT_DEBUG
538
    dbg() << "Failed to remove original file:" << fileName;
539
#endif
NEW
540
    QFile::remove(tempPath);
×
541
    return false;
542
  }
NEW
543
  if (!QFile::rename(tempPath, fileName)) {
×
544
#ifdef QT_DEBUG
545
    dbg() << "Failed to rename temp file to:" << fileName;
546
#endif
547
    // Critical: original removed but rename failed - data loss risk!
NEW
548
    emit critical(tr("Re-encryption failed"),
×
NEW
549
                  tr("Failed to replace %1. Data may be lost!").arg(fileName));
×
NEW
550
    return false;
×
551
  }
552

553
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
×
554
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
555
                              {"add", pgit(fileName)});
556
    QString path =
557
        QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
×
558
    path.replace(Util::endsWithGpg(), "");
×
559
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
560
                              {"commit", pgit(fileName), "-m",
NEW
561
                               "Re-encrypt for " + path + " using QtPass."});
×
562
  }
563

564
  return true;
UNCOV
565
}
×
566

567
void ImitatePass::reencryptPath(const QString &dir) {
×
568
  emit statusMsg(tr("Re-encrypting from folder %1").arg(dir), 3000);
×
569
  emit startReencryptPath();
×
570
  if (QtPassSettings::isAutoPull()) {
×
571
    emit statusMsg(tr("Updating password-store"), 2000);
×
572
    GitPull_b();
×
573
  }
574

575
  // Create backup before re-encryption
NEW
576
  if (QtPassSettings::isUseGit() &&
×
NEW
577
      !QtPassSettings::getGitExecutable().isEmpty()) {
×
NEW
578
    emit statusMsg(tr("Creating backup commit"), 2000);
×
NEW
579
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
580
                              {"add", "-A"});
NEW
581
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
582
                              {"commit", "-m", "Backup before re-encryption"});
583
  }
584

585
  QDir currentDir;
×
586
  QDirIterator gpgFiles(dir, QStringList() << "*.gpg", QDir::Files,
×
587
                        QDirIterator::Subdirectories);
×
588
  QStringList gpgIdFilesVerified;
×
589
  QStringList gpgId;
×
590
  int successCount = 0;
591
  int failCount = 0;
592
  while (gpgFiles.hasNext()) {
×
593
    QString fileName = gpgFiles.next();
×
594
    if (gpgFiles.fileInfo().path() != currentDir.path()) {
×
595
      verifyGpgIdForDir(fileName, gpgIdFilesVerified, gpgId);
×
596
      if (gpgId.isEmpty() && !gpgIdFilesVerified.isEmpty()) {
×
NEW
597
        emit critical(tr("GPG ID verification failed"),
×
NEW
598
                      tr("Could not verify .gpg-id for directory."));
×
NEW
599
        emit endReencryptPath();
×
600
        return;
601
      }
602
    }
603
    QStringList actualKeys = getKeysFromFile(fileName);
×
604
    if (actualKeys != gpgId) {
×
NEW
605
      if (reencryptSingleFile(fileName, gpgId)) {
×
606
        successCount++;
607
      } else {
NEW
608
        failCount++;
×
NEW
609
        emit critical(tr("Re-encryption failed"),
×
NEW
610
                      tr("Failed to re-encrypt %1").arg(fileName));
×
611
      }
612
    }
613
  }
614

NEW
615
  if (failCount > 0) {
×
NEW
616
    emit statusMsg(
×
NEW
617
        tr("Re-encryption completed with %1 failures").arg(failCount), 5000);
×
618
  } else {
NEW
619
    emit statusMsg(tr("Re-encryption completed successfully"), 3000);
×
620
  }
621

622
  if (QtPassSettings::isAutoPush()) {
×
623
    emit statusMsg(tr("Updating password-store"), 2000);
×
624
    GitPush();
×
625
  }
626
  emit endReencryptPath();
×
627
}
×
628

629
auto ImitatePass::resolveMoveDestination(const QString &src,
5✔
630
                                         const QString &dest, bool force)
631
    -> QString {
632
  QFileInfo srcFileInfo(src);
5✔
633
  QFileInfo destFileInfo(dest);
5✔
634
  QString destFile;
5✔
635
  QString srcFileBaseName = srcFileInfo.fileName();
5✔
636

637
  if (srcFileInfo.isFile()) {
5✔
638
    if (destFileInfo.isFile()) {
4✔
639
      if (!force) {
2✔
640
#ifdef QT_DEBUG
641
        dbg() << "Destination file already exists";
642
#endif
643
        return QString();
644
      }
645
      destFile = dest;
1✔
646
    } else if (destFileInfo.isDir()) {
2✔
647
      destFile = QDir(dest).filePath(srcFileBaseName);
2✔
648
    } else {
649
      destFile = dest;
1✔
650
    }
651

652
    if (destFile.endsWith(".gpg", Qt::CaseInsensitive)) {
6✔
653
      destFile.chop(4);
3✔
654
    }
655
    destFile.append(".gpg");
3✔
656
  } else if (srcFileInfo.isDir()) {
1✔
657
    if (destFileInfo.isDir()) {
×
658
      destFile = QDir(dest).filePath(srcFileBaseName);
×
659
    } else if (destFileInfo.isFile()) {
×
660
#ifdef QT_DEBUG
661
      dbg() << "Destination is a file";
662
#endif
663
      return QString();
664
    } else {
665
      destFile = dest;
×
666
    }
667
  } else {
668
#ifdef QT_DEBUG
669
    dbg() << "Source file does not exist";
670
#endif
671
    return QString();
672
  }
673
  return destFile;
674
}
5✔
675

676
void ImitatePass::executeMoveGit(const QString &src, const QString &destFile,
×
677
                                 bool force) {
678
  QStringList args;
×
679
  args << "mv";
×
680
  if (force) {
×
681
    args << "-f";
×
682
  }
683
  args << pgit(src);
×
684
  args << pgit(destFile);
×
685
  executeGit(GIT_MOVE, args);
×
686

687
  QString relSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(src);
×
688
  relSrc.replace(Util::endsWithGpg(), "");
×
689
  QString relDest =
690
      QDir(QtPassSettings::getPassStore()).relativeFilePath(destFile);
×
691
  relDest.replace(Util::endsWithGpg(), "");
×
692
  QString message = QString("Moved for %1 to %2 using QtPass.");
×
693
  message = message.arg(relSrc, relDest);
×
694
  GitCommit("", message);
×
695
}
×
696

697
void ImitatePass::Move(const QString src, const QString dest,
×
698
                       const bool force) {
699
  transactionHelper trans(this, PASS_MOVE);
×
700
  QString destFile = resolveMoveDestination(src, dest, force);
×
701
  if (destFile.isEmpty()) {
×
702
    return;
703
  }
704

705
#ifdef QT_DEBUG
706
  dbg() << "Move Source: " << src;
707
  dbg() << "Move Destination: " << destFile;
708
#endif
709

710
  if (QtPassSettings::isUseGit()) {
×
711
    executeMoveGit(src, destFile, force);
×
712
  } else {
713
    QDir qDir;
×
714
    if (force) {
×
715
      qDir.remove(destFile);
×
716
    }
717
    qDir.rename(src, destFile);
×
718
  }
×
719
}
720

721
void ImitatePass::Copy(const QString src, const QString dest,
×
722
                       const bool force) {
723
  QFileInfo destFileInfo(dest);
×
724
  transactionHelper trans(this, PASS_COPY);
×
725
  if (QtPassSettings::isUseGit()) {
×
726
    QStringList args;
×
727
    args << "cp";
×
728
    if (force) {
×
729
      args << "-f";
×
730
    }
731
    args << pgit(src);
×
732
    args << pgit(dest);
×
733
    executeGit(GIT_COPY, args);
×
734

735
    QString message = QString("Copied from %1 to %2 using QtPass.");
×
736
    message = message.arg(src, dest);
×
737
    GitCommit("", message);
×
738
  } else {
739
    QDir qDir;
×
740
    if (force) {
×
741
      qDir.remove(dest);
×
742
    }
743
    QFile::copy(src, dest);
×
744
  }
×
745
  // reecrypt all files under the new folder
746
  if (destFileInfo.isDir()) {
×
747
    reencryptPath(destFileInfo.absoluteFilePath());
×
748
  } else if (destFileInfo.isFile()) {
×
749
    reencryptPath(destFileInfo.dir().path());
×
750
  }
751
}
×
752

753
/**
754
 * @brief ImitatePass::executeGpg easy wrapper for running gpg commands
755
 * @param args
756
 */
757
void ImitatePass::executeGpg(PROCESS id, const QStringList &args, QString input,
×
758
                             bool readStdout, bool readStderr) {
759
  executeWrapper(id, QtPassSettings::getGpgExecutable(), args, std::move(input),
×
760
                 readStdout, readStderr);
761
}
×
762
/**
763
 * @brief ImitatePass::executeGit easy wrapper for running git commands
764
 * @param args
765
 */
766
void ImitatePass::executeGit(PROCESS id, const QStringList &args, QString input,
×
767
                             bool readStdout, bool readStderr) {
768
  executeWrapper(id, QtPassSettings::getGitExecutable(), args, std::move(input),
×
769
                 readStdout, readStderr);
770
}
×
771

772
/**
773
 * @brief ImitatePass::finished this function is overloaded to ensure
774
 *                              identical behaviour to RealPass ie. only PASS_*
775
 *                              processes are visible inside Pass::finish, so
776
 *                              that interface-wise it all looks the same
777
 * @param id
778
 * @param exitCode
779
 * @param out
780
 * @param err
781
 */
782
void ImitatePass::finished(int id, int exitCode, const QString &out,
×
783
                           const QString &err) {
784
#ifdef QT_DEBUG
785
  dbg() << "Imitate Pass";
786
#endif
787
  static QString transactionOutput;
×
788
  PROCESS pid = transactionIsOver(static_cast<PROCESS>(id));
×
789
  transactionOutput.append(out);
×
790

791
  if (exitCode == 0) {
×
792
    if (pid == INVALID) {
×
793
      return;
794
    }
795
  } else {
796
    while (pid == INVALID) {
×
797
      id = exec.cancelNext();
×
798
      if (id == -1) {
×
799
        //  this is probably irrecoverable and shall not happen
800
#ifdef QT_DEBUG
801
        dbg() << "No such transaction!";
802
#endif
803
        return;
804
      }
805
      pid = transactionIsOver(static_cast<PROCESS>(id));
×
806
    }
807
  }
808
  Pass::finished(pid, exitCode, transactionOutput, err);
×
809
  transactionOutput.clear();
×
810
}
811

812
/**
813
 * @brief executeWrapper    overrided so that every execution is a transaction
814
 * @param id
815
 * @param app
816
 * @param args
817
 * @param input
818
 * @param readStdout
819
 * @param readStderr
820
 */
821
void ImitatePass::executeWrapper(PROCESS id, const QString &app,
×
822
                                 const QStringList &args, QString input,
823
                                 bool readStdout, bool readStderr) {
824
  transactionAdd(id);
×
825
  Pass::executeWrapper(id, app, args, input, readStdout, readStderr);
×
826
}
×
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