mozilla/pdf.js · error · PasswordException

Incorrect Password

Error message

Incorrect Password

What it means

PasswordException with INCORRECT_PASSWORD code, thrown after both user-password and owner-password key-derivation paths failed to produce an encryption key. The supplied password does not match either the user or owner password.

Source

Thrown at src/core/crypto.js:1241

        passwordBytes,
        ownerPassword,
        revision,
        keyLength
      );
      encryptionKey = this.#prepareKeyData(
        fileIdBytes,
        decodedPassword,
        ownerPassword,
        userPassword,
        flags,
        revision,
        keyLength,
        encryptMetadata
      );
    }

    if (!encryptionKey) {
      throw new PasswordException(
        "Incorrect Password",
        PasswordResponses.INCORRECT_PASSWORD
      );
    }

    if (algorithm === 4 && encryptionKey.length < 16) {
      // Extend key to 16 byte minimum (undocumented),
      // fixes issue19484_1.pdf and issue19484_2.pdf.
      this.encryptionKey = new Uint8Array(16);
      this.encryptionKey.set(encryptionKey);
    } else {
      this.encryptionKey = encryptionKey;
    }
  }

  /**
   * Set password.
   *

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Re-prompt the user for the password and call loadingTask with the new value (destroy + recreate, or pdfDocument.updatePassword).
  2. Trim whitespace only if you are sure the producer did not include it (R<6 passwords are byte-exact, so do not over-normalize).
  3. Check err.code === PasswordResponses.INCORRECT_PASSWORD (value 2) and show a 'wrong password' UI.

Example fix

// before
const doc = await getDocument({ url, password: userInput }).promise;

// after
try {
  const doc = await getDocument({ url, password: userInput }).promise;
} catch (e) {
  if (e.name === 'PasswordException' && e.code === 2) {
    showWrongPasswordError();
  }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  return await getDocument({ url, password: userInput }).promise;
} catch (e) {
  if (e.name === 'PasswordException' && e.code === PasswordResponses.INCORRECT_PASSWORD) {
    showWrongPasswordError();
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a wrong password to getDocument for a Standard-encrypted PDF; a typo; a PDF whose password was changed after caching.

Common situations: User mistypes the password; stale cached password; copy/paste error with trailing whitespace (note: SASLprep only applies at R=6).

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/5e4f3ba8073aaa47. Report an issue: GitHub.