Skip to content

Commit

Permalink
[17.0][FIX] fs_attachment: Add CSP header for fs stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kafai-lam authored and lmignon committed Nov 10, 2024
1 parent 4922352 commit e71f748
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion fs_attachment/fs_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def read(self):
return f.read()
return super().read()

def get_response(self, as_attachment=None, immutable=None, **send_file_kwargs):
def get_response(
self,
as_attachment=None,
immutable=None,
content_security_policy="default-src 'none'",
**send_file_kwargs,
):
if self.type != "fs":
return super().get_response(
as_attachment=as_attachment, immutable=immutable, **send_file_kwargs
Expand Down Expand Up @@ -79,6 +85,12 @@ def get_response(self, as_attachment=None, immutable=None, **send_file_kwargs):

if immutable and res.cache_control:
res.cache_control["immutable"] = None

res.headers["X-Content-Type-Options"] = "nosniff"

if content_security_policy:
res.headers["Content-Security-Policy"] = content_security_policy

return res

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions fs_attachment/tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ def test_image_url_with_size(self):
},
)
self.assertEqual(Image.open(io.BytesIO(res.content)).size, (64, 64))

def test_response_csp_header(self):
self.authenticate("admin", "admin")
url = f"/web/content/{self.attachment_binary.id}"
self.assertDownload(
url,
headers={},
assert_status_code=200,
assert_headers={
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'none'",
},
)

0 comments on commit e71f748

Please sign in to comment.