Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Security: open Paperless documents via header auth and blob URLs, not query tokens (AUTH-02)

Omega (QA)481c517dbe98
diff --git a/frontend/js/components/paperless.js b/frontend/js/components/paperless.js
index 5c87814..b80ad61 100644
--- a/frontend/js/components/paperless.js
+++ b/frontend/js/components/paperless.js
@@ -341,8 +341,17 @@ export async function openPaperlessDocument(paperlessId, warrantyContext = null)
}
const viewInApp = await getUserPaperlessViewPreference(warrantyContext);
if (viewInApp) {
- const url = `/api/paperless-file/${paperlessId}?token=${encodeURIComponent(token)}`;
- const win = window.open(url, '_blank');
+ // Fetch with the token in the Authorization header and open
+ // a blob URL: a token in the query string would persist in
+ // browser history and in the service worker cache (AUTH-02).
+ const fileResponse = await fetch(`/api/paperless-file/${paperlessId}`, {
+ headers: { Authorization: `Bearer ${token}` },
+ });
+ if (!fileResponse.ok) throw new Error('Failed to open document');
+ const blob = await fileResponse.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const win = window.open(blobUrl, '_blank');
+ setTimeout(() => window.URL.revokeObjectURL(blobUrl), 10000);
if (!win) showToast(window.t ? window.t('paperless.popup_blocked') : 'Please allow popups to view documents', 'warning');
return;
}
diff --git a/frontend/script.js b/frontend/script.js
index 30e164e..a5b04ad 100644
--- a/frontend/script.js
+++ b/frontend/script.js
@@ -7639,15 +7639,30 @@ async function openPaperlessDocument(paperlessId, warrantyContext = null) {
console.log(`[openPaperlessDocument] User preference view in app: ${viewInApp}`);
if (viewInApp) {
- // Open document in Warracker interface
+ // Open document in Warracker interface. Fetch with the token
+ // in the Authorization header and open a blob URL: a token in
+ // the query string would persist in browser history and in
+ // the service worker cache (AUTH-02).
console.log(`[openPaperlessDocument] Opening document ${paperlessId} in Warracker interface`);
- const documentUrl = `/api/paperless-file/${paperlessId}?token=${encodeURIComponent(token)}`;
-
- const newTab = window.open(documentUrl, '_blank');
- if (!newTab) {
- showToast('Please allow popups to view documents', 'warning');
- } else {
- showToast('Opening document in Warracker...', 'info');
+ try {
+ const fileResponse = await fetch(`/api/paperless-file/${paperlessId}`, {
+ headers: { 'Authorization': `Bearer ${token}` }
+ });
+ if (!fileResponse.ok) {
+ throw new Error(`Failed to open document: ${fileResponse.status}`);
+ }
+ const blob = await fileResponse.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const newTab = window.open(blobUrl, '_blank');
+ setTimeout(() => window.URL.revokeObjectURL(blobUrl), 10000);
+ if (!newTab) {
+ showToast('Please allow popups to view documents', 'warning');
+ } else {
+ showToast('Opening document in Warracker...', 'info');
+ }
+ } catch (fileError) {
+ console.error('[openPaperlessDocument] Failed to open document:', fileError);
+ showToast(`Error opening document: ${fileError.message}`, 'error');
}
return;
}
diff --git a/tests/test_auth02_no_token_urls.py b/tests/test_auth02_no_token_urls.py
new file mode 100644
index 0000000..36cea89
--- /dev/null
+++ b/tests/test_auth02_no_token_urls.py
@@ -0,0 +1,32 @@
+"""AUTH-02: the JWT must never travel in a document URL.
+
+Both Paperless document open flows used to call
+`/api/paperless-file/<id>?token=<jwt>`; the URL then persisted in
+browser history and in the service worker Cache Storage. Both flows
+now fetch with the token in the Authorization header and open a blob
+URL. These pins fail if a query-string token ever comes back.
+"""
+from pathlib import Path
+
+REPO = Path(__file__).resolve().parent.parent
+
+FILES = [
+ REPO / "frontend" / "script.js",
+ REPO / "frontend" / "js" / "components" / "paperless.js",
+]
+
+
+def test_no_query_token_in_paperless_open_flows():
+ for path in FILES:
+ text = path.read_text()
+ assert "?token=" not in text, path
+ assert "paperless-file/${" in text, path
+ assert "Authorization" in text, path
+ assert "createObjectURL" in text, path
+
+
+def test_no_token_query_anywhere_in_frontend():
+ for path in REPO.rglob("frontend/**/*.js"):
+ if "node_modules" in path.parts:
+ continue
+ assert "?token=" not in path.read_text(), path