{"record":{"id":"7a6fe4bde7790d18","repo":"mozilla/pdf.js","slug":"doc-securityhandler-is-read-only","errorCode":null,"errorMessage":"doc.securityHandler is read-only","messagePattern":"doc\\.securityHandler is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":623,"sourceCode":"\n  set producer(_) {\n    throw new Error(\"doc.producer is read-only\");\n  }\n\n  get requiresFullSave() {\n    return false;\n  }\n\n  set requiresFullSave(_) {\n    throw new Error(\"doc.requiresFullSave is read-only\");\n  }\n\n  get securityHandler() {\n    return this._securityHandler;\n  }\n\n  set securityHandler(_) {\n    throw new Error(\"doc.securityHandler is read-only\");\n  }\n\n  get selectedAnnots() {\n    return [];\n  }\n\n  set selectedAnnots(_) {\n    throw new Error(\"doc.selectedAnnots is read-only\");\n  }\n\n  get sounds() {\n    return [];\n  }\n\n  set sounds(_) {\n    throw new Error(\"doc.sounds is read-only\");\n  }\n","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L605-L641","documentation":"`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.","triggerScenarios":"A script writes `doc.securityHandler = ...;`, attempting to attach or replace a security handler. The setter throws on assignment.","commonSituations":"DRM/encryption scripts ported from Acrobat; code that confuses the handler object with a policy flag; legacy enterprise document scripts.","solutions":["Remove the assignment; read the value (`var h = doc.securityHandler;`).","Apply encryption/security at PDF creation or via an external tool, not from inside a document script.","Wrap script execution in try/catch for legacy tolerance."],"exampleFix":"// before\ndoc.securityHandler = myHandler;\n\n// after\nvar h = doc.securityHandler; // read-only intrinsic property","handlingStrategy":"try-catch","validationCode":"const READ_ONLY_DOC_PROPS = new Set(['securityHandler','dataObjects' /* ... */]);\nfunction assertsNoReadOnlyWrite(scriptSrc) {\n  for (const p of READ_ONLY_DOC_PROPS) {\n    if (new RegExp(`\\\\bdoc\\\\.${p}\\\\s*=[^=]`).test(scriptSrc)) {\n      throw new Error(`Script writes read-only property doc.${p}`);\n    }\n  }\n}\nassertsNoReadOnlyWrite(myScript);","typeGuard":"function isReadOnlyDocProp(doc, prop) {\n  const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)\n    || Object.getOwnPropertyDescriptor(doc, prop);\n  return !!desc && !!desc.get && !desc.set;\n}\nisReadOnlyDocProp(doc, 'securityHandler'); // true","tryCatchPattern":"try {\n  doc.securityHandler = handler;\n} catch (e) {\n  if (/securityHandler is read-only/.test(e.message)) console.warn(e.message);\n  else throw e;\n}","preventionTips":["Security/encryption is applied at authoring time; never assign the security handler from a script.","Lint scripts for assignments to read-only security properties.","Wrap DRM/encryption legacy scripts in try/catch."],"tags":["scripting","acrobat-js-api","doc-object","read-only","security","encryption"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}