mozilla/pdf.js · error · PasswordException
No password given
Error message
No password given
What it means
PasswordException with NEED_PASSWORD code, thrown when no encryption key could be derived and no password was supplied—and the deferred-password EFOpen/Identity case does not apply. PDF.js needs a password to derive the encryption key and none was passed to getDocument({password}).
Source
Thrown at src/core/crypto.js:1216
}
if (!encryptionKey) {
if (!password) {
if (
this.algorithm >= 4 &&
isName(this.stmf, "Identity") &&
isName(this.strf, "Identity")
) {
const effCF = this.cf?.get(this.eff.name);
const authEvent = effCF?.get("AuthEvent");
if (isName(authEvent, "EFOpen")) {
// For EFOpen with Identity as default stream/string filters, defer
// password prompting until an EmbeddedFile stream is actually read.
this.encryptionKey = null;
return;
}
}
throw new PasswordException(
"No password given",
PasswordResponses.NEED_PASSWORD
);
}
// Attempting use the password as an owner password
const decodedPassword = this.#decodeUserPassword(
passwordBytes,
ownerPassword,
revision,
keyLength
);
encryptionKey = this.#prepareKeyData(
fileIdBytes,
decodedPassword,
ownerPassword,
userPassword,
flags,
revision,View on GitHub (pinned to 5903d58d58)
Solutions
- Pass the password: getDocument({ url, password }).
- Inspect the loadingTask promise rejection, check err.code === PasswordResponses.NEED_PASSWORD (value 1), then prompt the user and call pdfDocument.updatePassword(pwd).
- For UIs, listen to the 'passwordrequest' event and surface a dialog.
Example fix
// before
const doc = await getDocument({ url }).promise;
// after — prompt on NEED_PASSWORD and retry
let task = getDocument({ url });
try {
const doc = await task.promise;
} catch (e) {
if (e.name === 'PasswordException' && e.code === 1) {
const pwd = await promptUserForPassword();
doc = await task.promise.catch(async () => {
await task.destroy();
return (task = getDocument({ url, password: pwd })).promise;
});
}
} Defensive patterns
Strategy: try-catch
Try / catch
let task = getDocument({ url });
try {
return await task.promise;
} catch (e) {
if (e.name === 'PasswordException' && e.code === PasswordResponses.NEED_PASSWORD) {
const pwd = await promptUserForPassword();
await task.destroy();
return await getDocument({ url, password: pwd }).promise;
}
throw e;
} Prevention
- Always handle the NEED_PASSWORD code in your getDocument rejection handler.
- Use loadingTask.onPassword callback for interactive viewers rather than throwing.
- Avoid hard-coded empty passwords; treat undefined password as 'prompt'.
When it happens
Trigger: Calling getDocument on a user/owner-password-protected PDF without passing the password option; the document requires a password but the UI has not yet prompted the user.
Common situations: Forgetting to pass the password; a user-facing viewer that has not yet shown the password dialog; chained loads where the password lives elsewhere.
Related errors
- Incorrect Password
- unknown encryption method
- unsupported encryption algorithm
- invalid key length
- Invalid crypt filter name.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/25c63a4ce89c08b5.
Report an issue: GitHub.