[FIX] attachment_zipped_download: zip allowed document only

The previous code allowed any authenticated to retreive any attachment present
on odoo filesystem. So a WMS user could technically spoken download a zip with
accounting documents.

This implementation is calling attachemnt.check("read") to ensure access
right and use attachemnt.raw attribute to retreive file to archive which
(not test) should works with attachment saved somewhere else than the
local filesystem (s3 storage, pgsql large object storage...).

attachment_zipped_download 16.0.1.0.2
This commit is contained in:
Pierre Verkest
2023-04-28 13:51:35 +02:00
committed by Víctor Martínez
parent a99bc0dde1
commit 25ceed4377
3 changed files with 77 additions and 20 deletions

View File

@@ -28,9 +28,16 @@ class IrAttachment(models.Model):
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for attachment in self:
zip_file.write(
attachment._full_path(attachment.store_fname), attachment.name
attachment.check("read")
zip_file.writestr(
attachment._compute_zip_file_name(),
attachment.raw,
)
zip_buffer.seek(0)
zip_file.close()
return zip_buffer
def _compute_zip_file_name(self):
"""Give a chance of easily changing the name of the file inside the ZIP."""
self.ensure_one()
return self.name