Skip to content

Commit 9b9e020

Browse files
committed
fix(schema): take the list of allowed tags and attrs from the app config
* previously, the list of allowed HTML tags and attributes was taken from a hard-coded definition
1 parent 5e2bdec commit 9b9e020

File tree

8 files changed

+67
-8
lines changed

8 files changed

+67
-8
lines changed

invenio_rdm_records/resources/deserializers/rocrate/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
from invenio_i18n import lazy_gettext as _
1212
from marshmallow import EXCLUDE, Schema, ValidationError, fields, pre_load, validate
13-
from marshmallow_utils.fields import SanitizedHTML, SanitizedUnicode
13+
from marshmallow_utils.fields import SanitizedUnicode
14+
15+
from ....services.schemas.fields import SanitizedHTML
1416

1517

1618
def _list_value(lst):

invenio_rdm_records/resources/serializers/cff/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from flask_resources.serializers import BaseSerializerSchema
1111
from invenio_i18n import lazy_gettext as _
1212
from marshmallow import ValidationError, fields, missing
13-
from marshmallow_utils.fields import SanitizedHTML, SanitizedUnicode
13+
from marshmallow_utils.fields import SanitizedUnicode
14+
15+
from ....services.schemas.fields import SanitizedHTML
1416

1517

1618
def _serialize_person(person):

invenio_rdm_records/resources/serializers/schemaorg/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
from idutils import to_url
2121
from invenio_i18n import lazy_gettext as _
2222
from marshmallow import Schema, ValidationError, fields, missing
23-
from marshmallow_utils.fields import SanitizedHTML, SanitizedUnicode
23+
from marshmallow_utils.fields import SanitizedUnicode
2424
from pydash import py_
2525

26+
from ....services.schemas.fields import SanitizedHTML
2627
from ..schemas import CommonFieldsMixin
2728
from ..utils import convert_size, get_vocabulary_props
2829

invenio_rdm_records/resources/serializers/ui/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
from marshmallow_utils.fields.babel import gettext_from_dict
3434
from pyparsing import ParseException
3535

36-
from invenio_rdm_records.services.request_policies import RDMRecordDeletionPolicy
37-
36+
from ....services.request_policies import RDMRecordDeletionPolicy
37+
from ....services.schemas.fields import SanitizedHTML
3838
from .fields import AccessStatusField
3939

4040

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2025 TU Wien.
4+
#
5+
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
6+
# it under the terms of the MIT License; see LICENSE file for more details.
7+
8+
"""Marshmallow field definitions."""
9+
10+
11+
from flask import current_app
12+
from marshmallow_utils.fields import SanitizedHTML as SanitizedHTMLBase
13+
14+
15+
class SanitizedHTML(SanitizedHTMLBase):
16+
"""String field that sanitizes HTML tags with the ``bleach`` library.
17+
18+
In contrast to the base class, this field takes the Flask application's
19+
configuration into account.
20+
"""
21+
22+
@property
23+
def tags(self):
24+
"""Get the list of allowed HTML tags.
25+
26+
If no application context is available, use the field's set value as fallback.
27+
"""
28+
try:
29+
return current_app.config["ALLOWED_HTML_TAGS"]
30+
except RuntimeError:
31+
return self._tags
32+
33+
@tags.setter
34+
def tags(self, value):
35+
"""Set the field's fallback value for allowed HTML tags."""
36+
self._tags = value
37+
38+
@property
39+
def attrs(self):
40+
"""Get the dictionary for allowed attributes per HTML tag.
41+
42+
If no application context is available, use the field's set value as fallback.
43+
"""
44+
try:
45+
return current_app.config["ALLOWED_HTML_ATTRS"]
46+
except RuntimeError:
47+
return self._attrs
48+
49+
@attrs.setter
50+
def attrs(self, value):
51+
"""Set the field's fallback value for allowed HTML attributes."""
52+
self._attrs = value

invenio_rdm_records/services/schemas/metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
EDTFDateTimeString,
3636
IdentifierSet,
3737
IdentifierValueSet,
38-
SanitizedHTML,
3938
SanitizedUnicode,
4039
)
4140
from marshmallow_utils.schemas import GeometryObjectSchema, IdentifierSchema
4241
from werkzeug.local import LocalProxy
4342

43+
from .fields import SanitizedHTML
44+
4445
record_personorg_schemes = LocalProxy(
4546
lambda: current_app.config["RDM_RECORDS_PERSONORG_SCHEMES"]
4647
)

invenio_rdm_records/services/schemas/parent/access.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
from marshmallow.validate import OneOf
1818
from marshmallow_utils.fields import (
1919
ISODateString,
20-
SanitizedHTML,
2120
SanitizedUnicode,
2221
TZDateTime,
2322
)
2423
from marshmallow_utils.permissions import FieldPermissionsMixin
2524

25+
from ..fields import SanitizedHTML
26+
2627

2728
class GrantSubject(Schema):
2829
"""Schema for a grant subject."""

invenio_rdm_records/services/schemas/record.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
from marshmallow_utils.fields import (
2222
EDTFDateTimeString,
2323
NestedAttribute,
24-
SanitizedHTML,
2524
SanitizedUnicode,
2625
TZDateTime,
2726
)
2827
from marshmallow_utils.permissions import FieldPermissionsMixin
2928

3029
from .access import AccessSchema
30+
from .fields import SanitizedHTML
3131
from .files import FilesSchema
3232
from .metadata import MetadataSchema
3333
from .parent import RDMParentSchema

0 commit comments

Comments
 (0)