mozilla/pdf.js · error · PasswordException
1
1
Error message
No password given
What it means
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.
Source
Thrown at src/core/file_spec.js:158
}
return this.readStreamContent(ef);
}
/**
* Read the bytes of an embedded-file stream.
*
* @param {BaseStream} stream
* Embedded-file stream.
* @returns {CatalogAttachmentContent}
* Attachment bytes.
* @throws {PasswordException}
* When the bytes are encrypted and no key is available.
*/
static readStreamContent(stream) {
// Throw if we need a password but don’t have one.
const encrypt = stream.dict?.xref?.encrypt;
if (encrypt?.encryptionKey === null) {
throw new PasswordException(
"No password given",
PasswordResponses.NEED_PASSWORD
);
}
return stream.getBytes();
}
}
export { FileSpec };
View on GitHub (pinned to 5903d58d58)
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.
Example fix
// before
const attachments = await pdf.getAttachments(); // throws PasswordException
// after
try {
const attachments = await pdf.getAttachments();
} catch (e) {
if (e.name === 'PasswordException' && e.code === PasswordResponses.NEED_PASSWORD) {
const password = promptUserForPassword();
const pdf = await getDocument({ url, password }).promise;
const attachments = await pdf.getAttachments();
} else throw e;
} Defensive patterns
Strategy: try-catch
Validate before calling
// Before reading attachments, check the document's encryption state via
// the public API: pdf.isEncrypted indicates whether /Encrypt is present.
// To avoid NEED_PASSWORD, supply a password at load time:
if (pdf.isEncrypted) {
const password = await promptUserForPassword(); // your UX
const pdf2 = await getDocument({ url, password }).promise;
await pdf2.getAttachments();
} else {
await pdf.getAttachments();
} Type guard
// Inspect a PasswordException to distinguish NEED_PASSWORD from INCORRECT_PASSWORD:
function isNeedsPassword(e) {
return e?.name === 'PasswordException' && e.code === 1 /* NEED_PASSWORD */;
} Try / catch
try {
await pdf.getAttachments();
} catch (e) {
if (e.name === 'PasswordException' && e.code === PasswordResponses.NEED_PASSWORD) {
// prompt for password, reload with getDocument({ url, password }), retry
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- No password given
- Incorrect Password
- Parent must be a dictionary.
- Kids must be an array.
- Kid must be a reference.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/62130e30346de5a9.
Report an issue: GitHub.