passwork-me / js-connector

JS Connector for a self-hosted edition
https://passwork.pro
MIT License
2 stars 4 forks source link

occasional error decrypting attachment #25

Open sdahlbac opened 2 years ago

sdahlbac commented 2 years ago

Error: Error: Malformed UTF-8 data at Object.stringify (/Users/sdahlbacka/Work/passwork-cli-helper/node_modules/crypto-js/core.js:523:24) at WordArray.init.toString (/Users/sdahlbacka/Work/passwork-cli-helper/node_modules/crypto-js/core.js:278:38) at Object.decode (/Users/sdahlbacka/Work/passwork-cli-helper/node_modules/passwork-js/libs/crypt.js:28:85) at Object.decryptPasswordAttachment (/Users/sdahlbacka/Work/passwork-cli-helper/node_modules/passwork-js/libs/passwork.js:80:39) at Object.attachment.getData (/Users/sdahlbacka/Work/passwork-cli-helper/node_modules/passwork-js/src/rest-modules/passwords.js:10:48) at file:///Users/sdahlbacka/Work/passwork-cli-helper/index.js:32:81 at processTicksAndRejections (internal/process/task_queues.js:95:5)

but the next second it might work successfully.

Is there something that needs to be awaited that is not?


import chalk from 'chalk'
import { PASSWORK_URL } from './config.js';
import  Passwork from 'passwork-js/src/passwork-api.js';
import fs from 'fs';
import { exit } from 'process';

function fetchpassword (name, env, fetchAttachments=false) {
  /** @type PassworkAPI */
  const passwork = new Passwork(PASSWORK_URL);
  return (async () => {
    const result = {};
    try {
      await passwork.login(process.env.PASSWORK_API_KEY);

      let foldersSearchResult = await passwork.searchFolders(env);
      let searchResult = await passwork.searchPasswords(name);

      if (searchResult.length === 1) {
        let password = await passwork.getPassword(searchResult[0].id);

        if (foldersSearchResult.length !== 1 || (password.folderId !== undefined && foldersSearchResult[0].id !== password.folderId)) {
          process.stderr.write(`Password not found in the correct folder. Found ${password.folderId}, wanted ${foldersSearchResult[0].id} (name=${env}).\n`);
          exit(1);
        }

        result.vaultId = password.vaultId;
        result.folderId = password.folderId;
        result.password = password.getPassword();
        if (fetchAttachments) {
          for (const att of password.attachments) {
            const attachment = await passwork.getAttachment(password.id, att.id);
            result[attachment.name] = new TextDecoder().decode(await attachment.getData());
          }
        }
      } else {
        console.error(chalk.red.bold("Password not found. (or more than one)"));
      }
      process.stdout.write(JSON.stringify(result));
    } catch (error) {
      fs.writeFileSync('foo.txt', error.stack + "\n" + JSON.stringify(error));
      process.stderr.write("Error: " + error.stack + "\n" + error);
      exit(1);
    }
  })();
}

const stdinBuffer = fs.readFileSync(process.stdin.fd);

const config = JSON.parse(stdinBuffer.toString());

fetchpassword(config.name, config.workspace, config.includeAttachments);
sdahlbac commented 2 years ago

Possibly this might have something to do with it

{"httpRequest":"POST: https://.../api/v4/auth/login/...","httpStatus":400,"httpMessage":"Bad Request","status":"error","code":"banned","data":{"errorMessage":"You're been banned. Please wait a few minutes"}}

If so, the error handling is severely lacking