{"record":{"id":"62130e30346de5a9","repo":"mozilla/pdf.js","slug":"1","errorCode":"1","errorMessage":"No password given","messagePattern":"No password given","errorType":"exception","errorClass":"PasswordException","httpStatus":null,"severity":"error","filePath":"src/core/file_spec.js","lineNumber":158,"sourceCode":"    }\n    return this.readStreamContent(ef);\n  }\n\n  /**\n   * Read the bytes of an embedded-file stream.\n   *\n   * @param {BaseStream} stream\n   *   Embedded-file stream.\n   * @returns {CatalogAttachmentContent}\n   *   Attachment bytes.\n   * @throws {PasswordException}\n   *   When the bytes are encrypted and no key is available.\n   */\n  static readStreamContent(stream) {\n    // Throw if we need a password but don’t have one.\n    const encrypt = stream.dict?.xref?.encrypt;\n    if (encrypt?.encryptionKey === null) {\n      throw new PasswordException(\n        \"No password given\",\n        PasswordResponses.NEED_PASSWORD\n      );\n    }\n    return stream.getBytes();\n  }\n}\n\nexport { FileSpec };\n","sourceCodeStart":140,"sourceCodeEnd":168,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/file_spec.js#L140-L168","documentation":"Thrown by FileSpec.readStreamContent() when an embedded-file stream's encryption key is null — meaning the attachment is encrypted but no password/key has been supplied to decrypt it. This is a PasswordException with code NEED_PASSWORD (1), distinct from a FormatError: it is a recoverable, user-facing authentication error, not a structural defect. It fires when reading attachment bytes from a password-protected PDF where the document-level password was not provided or did not derive a key for the embedded file.","triggerScenarios":"Calling getData() / getAttachments() / FileSpec.readContent() on a PDF that is encrypted (has /Encrypt) and where the password supplied to getDocument() did not produce a usable per-file encryption key — i.e. encrypt.encryptionKey is null. Common when the document was opened with an empty/owner password that unlocked the page content but not the attachments, or when no password was given at all.","commonSituations":"Encrypted PDFs with attached files where the user supplied the wrong password, no password, or a user-password that grants read access but not full decryption. Also occurs in workflows that programmatically iterate attachments without handling the encryption state. Newly relevant if readStreamContent was added to harden attachment decryption.","solutions":["Supply the correct password via getDocument({ password }) using the document's owner/user password.","Catch PasswordException, inspect e.code === PasswordResponses.NEED_PASSWORD, and prompt the user for a password before retrying.","If the password is correct but attachments still fail, the PDF may use a separate/corrupt encryption key — re-save the PDF in Acrobat with 'Save As' to normalize encryption.","Skip the encrypted attachment and continue processing others by wrapping each readStreamContent call individually."],"exampleFix":"// before\nconst attachments = await pdf.getAttachments(); // throws PasswordException\n\n// after\ntry {\n  const attachments = await pdf.getAttachments();\n} catch (e) {\n  if (e.name === 'PasswordException' && e.code === PasswordResponses.NEED_PASSWORD) {\n    const password = promptUserForPassword();\n    const pdf = await getDocument({ url, password }).promise;\n    const attachments = await pdf.getAttachments();\n  } else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Before reading attachments, check the document's encryption state via\n// the public API: pdf.isEncrypted indicates whether /Encrypt is present.\n// To avoid NEED_PASSWORD, supply a password at load time:\nif (pdf.isEncrypted) {\n  const password = await promptUserForPassword(); // your UX\n  const pdf2 = await getDocument({ url, password }).promise;\n  await pdf2.getAttachments();\n} else {\n  await pdf.getAttachments();\n}","typeGuard":"// Inspect a PasswordException to distinguish NEED_PASSWORD from INCORRECT_PASSWORD:\nfunction isNeedsPassword(e) {\n  return e?.name === 'PasswordException' && e.code === 1 /* NEED_PASSWORD */;\n}","tryCatchPattern":"try {\n  await pdf.getAttachments();\n} catch (e) {\n  if (e.name === 'PasswordException' && e.code === PasswordResponses.NEED_PASSWORD) {\n    // prompt for password, reload with getDocument({ url, password }), retry\n  } else throw e;\n}","preventionTips":["Always pass the known password to getDocument({ password }) for encrypted PDFs.","Handle PasswordException separately from FormatError — it is a recoverable auth error, not corruption.","When iterating multiple attachments, wrap each readStreamContent call so one encrypted file does not block the rest.","Test with both user and owner passwords; some encryption configurations only derive the attachment key from the owner password."],"tags":["pdf","encryption","password","attachments","password-exception"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}