Skip to content
This repository was archived by the owner on Nov 18, 2026. It is now read-only.

Load media with the access token - #130

Merged
axelstudios merged 1 commit into
mainfrom
bugfix/authenticated-media
Nov 17, 2026
Merged

axelstudios merged 1 commit into
mainfrom
bugfix/authenticated-media

Conversation

@axelstudios

Copy link
Copy Markdown
Member

Any background context you want to provide?

The backend serves media at /api/v3/media/<path> only to authenticated users who can access a record referencing the file. This app signs in with a JWT only, with no Django session. Media was loaded by <iframe>, <img>, and anchor navigations, which don't go through HttpClient, so the auth interceptor never added the token.

As a result, users who signed in only through this app got 401s for analysis reports, result images, and file downloads. That was already the case in production. It now also happens in development, because the backend's dev /media/ route no longer serves files without authentication.

Companion to the SEED PR "Fix remaining access-level and data exposure gaps".

What's this PR do?

  • New MediaService (@seed/api/media):
    • Maps media references to /api/v3/media/... URLs. It accepts API URLs, dev /media/ URLs, same-origin URLs, and server filesystem paths, and rejects anything that resolves outside /api/v3/media/.
    • Saves downloads as application/octet-stream blobs, so an uploaded HTML or SVG file never renders from the app's origin.
  • Analysis view:
    • HTML reports are fetched as text and shown in a srcdoc iframe with sandbox="allow-scripts allow-popups". Without allow-same-origin, report scripts can't read the app's storage, including the token, and pages opened from the report keep the sandbox.
    • A <base href="about:srcdoc"> is inserted so links inside the report resolve within the report.
    • The frame has a fixed height and scrolls, since a sandboxed frame can't be measured.
    • Images are shown as data: URLs, which the CSP allows.
  • Downloads in other pages: the analysis runs grid, inventory documents grid, building files grid, and dataset page now use MediaService.download.
    • The building files grid never copied file into its rows.
    • The dataset page linked to the server filesystem path.
  • Analyses page:
    • getAnalyses requests include_views=false and no longer overwrites the page's views with the list endpoint's views, whose files are server paths.
    • Analyses with no views (failed or still being created) now render their status and messages.
  • Analysis view lifecycle: the view ignores stale analyses and views left in the shared streams, and stops fetching after it is destroyed. Before, it could show one report but download another property's.
  • Cleanup:
    • New downloadBlob and blobToDataUrl utils; csvDownload now uses downloadBlob.
    • Removed the unused SafeUrlPipe.
    • History.file is now string | null, since the backend returns null for files the user can't download.
    • ProgressResponse.unique_id is now number | string, since exports, mapping results, and other tasks now return string IDs. CacheService.getCacheEntry accepts either, and the data quality callers convert their run ID, which is still an integer, with Number.

How should this be manually tested?

Sign in through this app only, with no legacy session in the same browser, then check each of these:

  1. BETTER analysis: open an analysis property view. The report renders, links inside it work, and Download saves the .html file.
  2. BSyncr analysis: the result image shows.
  3. Analysis runs grid: the download icon saves the report.
  4. Inventory detail: document and building file downloads work.
  5. Dataset page: import file downloads work.
  6. Failed analysis: an analysis whose creation failed shows its status and latest message.
  7. CSP: behind the production nginx config, confirm the srcdoc report and its inline scripts aren't blocked.

@axelstudios
axelstudios requested a balanced review from Copilot November 17, 2026 20:24
@axelstudios axelstudios added the bug Something isn't working label Nov 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The widened ID type leaves a data-quality caller passing unique_id directly to a number-only parameter, causing a type error that needs the same numeric conversion used by the other DQC callers.

1 open issue
What changed in this PR

This PR routes media through authenticated HTTP requests so JWT-only users can view analysis results and download files across the SEED frontend.

Changes:

  • Adds a shared media service for URL normalization, authenticated fetching, and download-only blobs.
  • Displays reports in sandboxed frames and images as data URLs, while improving analysis lifecycle handling.
  • Updates download callers, task ID types, and the Audit Template request method, with regression tests.

Validation was limited to source inspection; builds, tests, and live-backend checks were not run.

File Description
src/​app/​modules/​inventory/​inventory.types.ts Allows unavailable history files to be null.
src/​app/​modules/​inventory-detail/​detail/​grid/​documents-grid.component.ts Uses authenticated document downloads.
src/​app/​modules/​inventory-detail/​detail/​grid/​documents-grid.component.spec.ts Tests document download delegation.
src/​app/​modules/​inventory-detail/​detail/​grid/​building-files-grid.component.ts Preserves file paths and authenticates downloads.
src/​app/​modules/​inventory-detail/​detail/​grid/​building-files-grid.component.spec.ts Tests building-file downloads.
src/​app/​modules/​datasets/​dataset/​dataset.component.ts Downloads imports through the media service.
src/​app/​modules/​datasets/​dataset/​dataset.component.spec.ts Tests import-file downloads.
src/​app/​modules/​datasets/​data-mappings/​step3/​save-mappings.component.ts Converts data-quality IDs to numbers.
src/​app/​modules/​data-quality/​start-modal.component.ts Converts data-quality result IDs to numbers.
src/​app/​modules/​analyses/​analysis/​analysis.component.ts Handles empty views and authenticated downloads.
src/​app/​modules/​analyses/​analysis/​analysis.component.spec.ts Tests failed analyses and result downloads.
src/​app/​modules/​analyses/​analysis_view/​analysis-view.component.ts Fetches report content and guards shared-stream updates.
src/​app/​modules/​analyses/​analysis_view/​analysis-view.component.spec.ts Tests rendering, downloads, and lifecycle guards.
src/​app/​modules/​analyses/​analysis_view/​analysis-view.component.html Renders sandboxed reports and fetched images.
src/​@seed/​utils/​index.ts Exports the new blob utilities.
src/​@seed/​utils/​download-blob.ts Adds shared blob downloading and URL cleanup.
src/​@seed/​utils/​download-blob.spec.ts Tests download naming and URL cleanup.
src/​@seed/​utils/​csv-download.ts Reuses the blob download utility.
src/​@seed/​utils/​blob-to-data-url.ts Adds observable blob-to-data-URL conversion.
src/​@seed/​utils/​blob-to-data-url.spec.ts Tests data-URL conversion.
src/​@seed/​pipes/​safe-url/​safe-url.pipe.ts Removes the unused URL trust pipe.
src/​@seed/​api/​progress/​progress.types.ts Supports numeric and string task IDs.
src/​@seed/​api/​media/​media.service.ts Centralizes authenticated media access.
src/​@seed/​api/​media/​media.service.spec.ts Tests URL validation, authentication, and downloads.
src/​@seed/​api/​media/​index.ts Exports the media service.
src/​@seed/​api/​index.ts Exposes media through the API barrel.
src/​@seed/​api/​cache/​cache.service.ts Accepts numeric or string cache IDs.
src/​@seed/​api/​audit-template/​audit-template.service.ts Changes submission imports to PUT.
src/​@seed/​api/​audit-template/​audit-template.service.spec.ts Tests import and export request methods.
src/​@seed/​api/​analysis/​analysis.types.ts Makes list-response views optional.
src/​@seed/​api/​analysis/​analysis.service.ts Lists analyses without overwriting detail views.
src/​@seed/​api/​analysis/​analysis.service.spec.ts Tests list requests and view-state preservation.

Review effort: Balanced

@axelstudios
axelstudios force-pushed the bugfix/authenticated-media branch from 417e3c6 to a339819 Compare November 17, 2026 21:09
@axelstudios
axelstudios merged commit 0b69c1f into main Nov 17, 2026
2 checks passed
@axelstudios
axelstudios deleted the bugfix/authenticated-media branch November 17, 2026 21:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants