Opened 15 years ago
Last modified 15 years ago
#9281 new enhancement
Allow IAttachmentManipulator to easily check on the uploaded attachment's file data
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | normal | Milestone: | next-major-releases |
Component: | attachment | Version: | 0.12dev |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: | |||
Internal Changes: |
Description
Currently, both the filename and the file data is not available to the manipulator, unless it would reimplement part of the logic found in AttachmentModule#_do_save.
_do_save already does some verification on the uploaded file, which I presume is present in some temporary location on the local filesystem.
My proposal would be to also pass in the upload file object associated with that file to the manipulator so that it is able to verify its contents, for example scanning it with an antivirus scanner and so on.
See also ticket #9280.
Attachments (0)
Change History (4)
comment:1 by , 15 years ago
Milestone: | 0.12 → next-major-0.1X |
---|
comment:2 by , 15 years ago
Will do.
I was not sure whether the unbound method from IAttachmentManipulator could be changed so that it will feature all of the required parameters to the validate_attachment method, hence I simply included it with the doc string.
Here is a proposed patch OTOH and untested:
-
attachment.py
34 34 from trac.perm import PermissionError, IPermissionPolicy 35 35 from trac.resource import * 36 36 from trac.search import search_to_sql, shorten_result 37 from trac.util import get_reporter_id, create_unique_file 37 from trac.util import get_reporter_id, create_unique_file, arity 38 38 from trac.util.datefmt import format_datetime, from_utimestamp, \ 39 39 to_datetime, to_utimestamp, utc 40 40 from trac.util.text import exception_to_unicode, pretty_size, print_table, \ … … 85 85 detected. `field` can be any of `description`, `username`, `filename`, 86 86 `content`, or `None` to indicate an overall problem with the 87 87 attachment. Therefore, a return value of `[]` means everything is 88 OK.""" 88 OK. 89 90 New style manipulators can also validate the uploaded file's content. 91 In order to do this, they will have to implement a third parameter 92 to this method, e.g. 93 94 def validate_attachment(self, req, attachment, upload): 95 ... 96 """ 89 97 90 98 class ILegacyAttachmentPolicyDelegate(Interface): 91 99 """Interface that can be used by plugins to seemlessly participate to the … … 637 645 attachment.ipnr = req.remote_addr 638 646 639 647 # Validate attachment 648 def validate(manipulator): 649 650 def validate_old(req, attachment): 651 return manipulator.validate_attachment(req, attachment) 652 653 def validate_new(req, attachment, upload): 654 return manipulator.validate_attachment(req, attachment, 655 upload) 656 if arity(manipulator.validate_attachment) == 2: 657 return validate_old 658 return validate_new 659 640 660 for manipulator in self.manipulators: 641 for field, message in manipulator.validate_attachment(req, 642 attachment): 643 if field: 644 raise InvalidAttachment(_('Attachment field %(field)s is ' 645 'invalid: %(message)s', 646 field=field, message=message)) 647 else: 648 raise InvalidAttachment(_('Invalid attachment: %(message)s', 661 for field, message in validate(manipulator): 662 if field: 663 raise InvalidAttachment(_('Attachment field %(field)s ' 664 'is invalid: %(message)s', 665 field=field, message=message)) 666 else: 667 raise InvalidAttachment(_('Invalid attachment: %(message)s', 649 668 message=message)) 650 669 651 670 if req.args.get('replace'):
comment:3 by , 15 years ago
Just tested and it turned out that the function was not iterable, of course *sigh*:
-
attachment.py
34 34 from trac.perm import PermissionError, IPermissionPolicy 35 35 from trac.resource import * 36 36 from trac.search import search_to_sql, shorten_result 37 from trac.util import get_reporter_id, create_unique_file 37 from trac.util import get_reporter_id, create_unique_file, arity 38 38 from trac.util.datefmt import format_datetime, from_utimestamp, \ 39 39 to_datetime, to_utimestamp, utc 40 40 from trac.util.text import exception_to_unicode, pretty_size, print_table, \ … … 85 85 detected. `field` can be any of `description`, `username`, `filename`, 86 86 `content`, or `None` to indicate an overall problem with the 87 87 attachment. Therefore, a return value of `[]` means everything is 88 OK.""" 88 OK. 89 90 New style manipulators can also validate the uploaded file's content. 91 In order to do this, they will have to implement a third parameter 92 to this method, e.g. 93 94 def validate_attachment(self, req, attachment, upload): 95 ... 96 """ 89 97 90 98 class ILegacyAttachmentPolicyDelegate(Interface): 91 99 """Interface that can be used by plugins to seemlessly participate to the … … 637 645 attachment.ipnr = req.remote_addr 638 646 639 647 # Validate attachment 648 def validate(manipulator): 649 650 def validate_old(): 651 return manipulator.validate_attachment(req, attachment) 652 653 def validate_new(): 654 return manipulator.validate_attachment(req, attachment, 655 upload) 656 if arity(manipulator.validate_attachment) == 2: 657 return validate_old 658 return validate_new 659 640 660 for manipulator in self.manipulators: 641 for field, message in manipulator.validate_attachment(req, 642 attachment): 643 if field: 644 raise InvalidAttachment(_('Attachment field %(field)s is ' 645 'invalid: %(message)s', 646 field=field, message=message)) 647 else: 648 raise InvalidAttachment(_('Invalid attachment: %(message)s', 661 for field, message in validate(manipulator)(): 662 if field: 663 raise InvalidAttachment(_('Attachment field %(field)s ' 664 'is invalid: %(message)s', 665 field=field, message=message)) 666 else: 667 raise InvalidAttachment(_('Invalid attachment: %(message)s', 649 668 message=message)) 650 669 651 670 if req.args.get('replace'):
comment:4 by , 15 years ago
Dear me, the validate() function is suboptimal in that it should directly call the chosen manipulator and return the result from that call
Please don't set the milestone arbitrarily. Better leave it empty, we'll take care of it.