-
Notifications
You must be signed in to change notification settings - Fork 2
Rework mime type white list #2198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8699bc1
66ac404
5ebe0fb
9416abf
7eed62a
4ab42df
cf252e2
6e9afd6
3f718d2
5572020
a6bd385
1293c8e
af685f5
a50f6f0
458a82d
8d99429
c31d0e3
eee980c
248819a
5621994
21274d1
c4a1eef
e77bc49
9a6a187
d122951
1e7b28b
427563e
fa438e0
0f7b210
e5552d6
a545477
5f7bcb2
5c54c06
f839024
ef4b1fe
789e503
f6f9d7c
c170834
00b1614
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| from onegov.file.utils import IMAGE_MIME_TYPES_AND_SVG | ||
| from onegov.form import log, _ | ||
| from onegov.form.utils import path_to_filename | ||
| from onegov.form.validators import ValidPhoneNumber | ||
| from onegov.form.validators import ValidPhoneNumber, WhitelistedMimeType | ||
| from onegov.form.widgets import ChosenSelectWidget | ||
| from onegov.form.widgets import LinkPanelWidget | ||
| from onegov.form.widgets import DurationInput | ||
|
|
@@ -59,7 +59,7 @@ | |
| if TYPE_CHECKING: | ||
| from collections.abc import Callable, Iterable, Iterator, Sequence | ||
| from datetime import datetime | ||
| from onegov.core.types import FileDict as StrictFileDict | ||
| from onegov.core.types import FileDict as StrictFileDict, LaxFileDict | ||
| from onegov.file import File | ||
| from onegov.form import Form | ||
| from onegov.form.types import ( | ||
|
|
@@ -274,28 +274,58 @@ class UploadField(FileField): | |
| file: IO[bytes] | None | ||
| filename: str | None | ||
|
|
||
| if TYPE_CHECKING: | ||
| def __init__( | ||
| self, | ||
| label: str | None = None, | ||
| validators: Validators[FormT, Self] | None = None, | ||
| filters: Sequence[Filter] = (), | ||
| description: str = '', | ||
| id: str | None = None, | ||
| default: Sequence[StrictFileDict] = (), | ||
| widget: Widget[Self] | None = None, | ||
| render_kw: dict[str, Any] | None = None, | ||
| name: str | None = None, | ||
| _form: BaseForm | None = None, | ||
| _prefix: str = '', | ||
| _translations: _SupportsGettextAndNgettext | None = None, | ||
| _meta: DefaultMeta | None = None, | ||
| # onegov specific kwargs that get popped off | ||
| *, | ||
| fieldset: str | None = None, | ||
| depends_on: Sequence[Any] | None = None, | ||
| pricing: PricingRules | None = None, | ||
| ): ... | ||
| def __init__( | ||
Tschuppi81 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self, | ||
| label: str | None = None, | ||
| validators: Validators[FormT, Self] | None = None, | ||
| filters: Sequence[Filter] = (), | ||
| description: str = '', | ||
| id: str | None = None, | ||
| default: LaxFileDict | None = None, | ||
Tschuppi81 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| widget: Widget[Self] | None = None, | ||
| render_kw: dict[str, Any] | None = None, | ||
| name: str | None = None, | ||
| allowed_mimetypes: Sequence[str] | None = None, | ||
| _form: BaseForm | None = None, | ||
| _prefix: str = '', | ||
| _translations: _SupportsGettextAndNgettext | None = None, | ||
| _meta: DefaultMeta | None = None, | ||
| # onegov specific kwargs that get popped off | ||
| *, | ||
| fieldset: str | None = None, | ||
| depends_on: Sequence[Any] | None = None, | ||
| pricing: PricingRules | None = None, | ||
| ): | ||
| validator = ( | ||
| WhitelistedMimeType(allowed_mimetypes) | ||
| if allowed_mimetypes | ||
| else WhitelistedMimeType() | ||
| ) | ||
|
|
||
| if validators: | ||
| validators = list(validators) | ||
| if not any(isinstance(validator, WhitelistedMimeType) | ||
| for validator in validators | ||
| ): | ||
| validators.append(validator) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure this logic still holds up when field dependencies are in play, I have a feeling it does not, since it will wrap everything in a conditional validator, so you probably need to detect that case, so you don't add an unconditional and potentially redundant mimetype validator.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be easier and more robust to store the mimetypes in an extra attribute and to override the So you can do something like the following in if not validation_stopped:
WhitelistedMimeType(self.mimetypes)(form, self)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually nevermind, this has the same problem, |
||
| else: | ||
| validators = [validator] | ||
|
|
||
| super().__init__( | ||
| label=label, | ||
| validators=validators, | ||
| filters=filters, | ||
| description=description, | ||
| id=id, | ||
| default=default, | ||
| widget=widget, | ||
| render_kw=render_kw, | ||
| name=name, | ||
| _form=_form, | ||
| _prefix=_prefix, | ||
| _translations=_translations, | ||
| _meta=_meta, | ||
| ) | ||
|
|
||
| # this is not quite accurate, since it is either a dictionary with all | ||
| # the keys or none of the keys, which would make type narrowing easier | ||
|
|
@@ -474,6 +504,7 @@ def __init__( | |
| render_kw: dict[str, Any] | None = None, | ||
| name: str | None = None, | ||
| upload_widget: Widget[UploadField] | None = None, | ||
| allowed_mimetypes: Sequence[str] | None = None, | ||
| _form: BaseForm | None = None, | ||
| _prefix: str = '', | ||
| _translations: _SupportsGettextAndNgettext | None = None, | ||
|
|
@@ -492,11 +523,11 @@ def __init__( | |
|
|
||
| # a lot of the arguments we just pass through to the subfield | ||
| unbound_field = self.upload_field_class( | ||
| validators=validators, # type:ignore[arg-type] | ||
| filters=filters, | ||
| description=description, | ||
| widget=upload_widget, | ||
| render_kw=render_kw, | ||
| allowed_mimetypes=allowed_mimetypes, | ||
| **extra_arguments | ||
| ) | ||
| super().__init__( | ||
|
|
@@ -507,6 +538,7 @@ def __init__( | |
| id=id, | ||
| default=default, | ||
| widget=widget, # type:ignore[arg-type] | ||
| validators=[*(validators or ())], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still pass these to the unbound field, not the field list. There are still things like the file size validator that makes more sense on the individual field. It allows you to re-simplify the validators back to what they were before.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without having it on the FieldList it does not work. On the unbound field the validators will be overwritten from the FieldList defaulted validators which results in the wrong mime type(s)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this still happens? Before it made sense because you set a default validator on the class, now this should no longer happen, as long as you only pass the validators to the unbound field. It's possible however that this also interacts badly with field dependencies, since there's no special casing for field lists. It might make sense to change how we implement field dependencies, since right now it is pretty fragile.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It fails when parsing a form from formcode, see My guess is that |
||
| render_kw=render_kw, | ||
| name=name, | ||
| _form=_form, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.