mozilla/pdf.js · error · Error

doc.securityHandler is read-only

Error message

doc.securityHandler is read-only

What it means

`Doc.securityHandler` returns the document's security handler object (`this._securityHandler`), populated during parsing if the document is encrypted. It is read-only per the Acrobat spec — the security handler is an intrinsic property of how the doc was authored/encrypted, not settable. PDF.js's throwing setter enforces that contract.

Source

Thrown at src/scripting_api/doc.js:623

  set producer(_) {
    throw new Error("doc.producer is read-only");
  }

  get requiresFullSave() {
    return false;
  }

  set requiresFullSave(_) {
    throw new Error("doc.requiresFullSave is read-only");
  }

  get securityHandler() {
    return this._securityHandler;
  }

  set securityHandler(_) {
    throw new Error("doc.securityHandler is read-only");
  }

  get selectedAnnots() {
    return [];
  }

  set selectedAnnots(_) {
    throw new Error("doc.selectedAnnots is read-only");
  }

  get sounds() {
    return [];
  }

  set sounds(_) {
    throw new Error("doc.sounds is read-only");
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Remove the assignment; read the value (`var h = doc.securityHandler;`).
  2. Apply encryption/security at PDF creation or via an external tool, not from inside a document script.
  3. Wrap script execution in try/catch for legacy tolerance.

Example fix

// before
doc.securityHandler = myHandler;

// after
var h = doc.securityHandler; // read-only intrinsic property
Defensive patterns

Strategy: try-catch

Validate before calling

const READ_ONLY_DOC_PROPS = new Set(['securityHandler','dataObjects' /* ... */]);
function assertsNoReadOnlyWrite(scriptSrc) {
  for (const p of READ_ONLY_DOC_PROPS) {
    if (new RegExp(`\\bdoc\\.${p}\\s*=[^=]`).test(scriptSrc)) {
      throw new Error(`Script writes read-only property doc.${p}`);
    }
  }
}
assertsNoReadOnlyWrite(myScript);

Type guard

function isReadOnlyDocProp(doc, prop) {
  const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)
    || Object.getOwnPropertyDescriptor(doc, prop);
  return !!desc && !!desc.get && !desc.set;
}
isReadOnlyDocProp(doc, 'securityHandler'); // true

Try / catch

try {
  doc.securityHandler = handler;
} catch (e) {
  if (/securityHandler is read-only/.test(e.message)) console.warn(e.message);
  else throw e;
}

Prevention

When it happens

Trigger: A script writes `doc.securityHandler = ...;`, attempting to attach or replace a security handler. The setter throws on assignment.

Common situations: DRM/encryption scripts ported from Acrobat; code that confuses the handler object with a policy flag; legacy enterprise document scripts.

Related errors


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