Coverage for app / util / untrusted_image.py: 100%

5 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1"""Serving image bytes that came from a third party, from this origin. 

2 

3Site icons (``/favicons``) and the images documents show (``documents``) are 

4both fetched by svc-ai from somewhere on the web and served to the browser 

5from here, so the user's browser never asks the site itself. Each is checked 

6to be a raster image before it is served, but it is still treated like any 

7untrusted file: never sniffed into something else, and inert if opened 

8directly. 

9""" 

10 

11from __future__ import annotations 

12 

13from fastapi import Response 

14 

15UNTRUSTED_IMAGE_HEADERS = { 

16 "Content-Security-Policy": "default-src 'none'; sandbox", 

17 "X-Content-Type-Options": "nosniff", 

18 # Per user, because every such request is authenticated; a day, because 

19 # what is served under one URL does not change. 

20 "Cache-Control": "private, max-age=86400", 

21} 

22 

23 

24def untrusted_image_response(data: bytes, media_type: str) -> Response: 

25 """``data`` served as ``media_type`` with :data:`UNTRUSTED_IMAGE_HEADERS`.""" 

26 return Response( 

27 content=data, media_type=media_type, headers=UNTRUSTED_IMAGE_HEADERS 

28 )