Skip to content

Fix unable to open local pdfs#2687

Open
pechenb wants to merge 2 commits intominbrowser:masterfrom
pechenb:fix-local-pdf
Open

Fix unable to open local pdfs#2687
pechenb wants to merge 2 commits intominbrowser:masterfrom
pechenb:fix-local-pdf

Conversation

@pechenb
Copy link
Copy Markdown

@pechenb pechenb commented Mar 6, 2026

FIxes #2375.

Tested only on MacOS 15.7.3

Changes

  • main/download.js
    Added IPC handler readLocalPDF that reads local PDF bytes in the main process.

  • main/localPDFAccess.js
    Added centralized validation for local PDF access:

    • request must come from internal PDF viewer (min://app/pages/pdfViewer/index.html...)
    • requested URL must be file://
    • requested file must exactly match the PDF URL opened in that viewer
  • js/preload/default.js
    Added bridge for readLocalPDF requests:

    • receives window.postMessage from viewer
    • calls ipc.invoke('readLocalPDF')
    • returns result/error back to viewer with window.postMessage
  • pages/pdfViewer/viewer.js
    Updated loading flow:

    • file:// PDFs: load via byte data (getDocument({ data }))
    • remote PDFs: keep existing URL flow (getDocument({ url, withCredentials: true }))
  • pages/pdfViewer/documentRequest.mjs
    Added helper for selecting local-vs-remote PDF.js request mode.

Security

  • only internal PDF viewer can use local file read path
  • only for the exact local file currently opened in that viewer

@pechenb pechenb marked this pull request as draft March 6, 2026 11:19
@pechenb pechenb marked this pull request as ready for review March 7, 2026 04:20
Copy link
Copy Markdown
Collaborator

@PalmerAL PalmerAL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. This looks good to me aside from the couple of comments I've added.

Obviously the largest concern with this is security; I've read through it and I don't see any issues, but I'll review it one more time prior to merging.

Comment thread pages/pdfViewer/viewer.js
pdfjsLib.getDocument({ url: url, withCredentials: true }).promise.then(async function (_pdf) {
function requestLocalPDFData (fileURL) {
return new Promise(function (resolve, reject) {
var requestId = ++localPDFRequestId
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localPDFRequestId is unique to each viewer instance, so it will be 0 in basically all cases. To create a unique request ID, you probably need to use Math.random() or similar.

Comment thread pages/pdfViewer/viewer.js
})
}

function getPDFDocumentRequest (targetURL, requestLocalPDFData) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can just call the global requestLocalPDFData; it doesn't need to be an argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF files no longer open properly

2 participants