3232 has_read_record_permission ,
3333)
3434from ..utils import HTMLTagRemover , remove_html_tags
35+ from marshmallow_utils .html import sanitize_html
3536
3637
3738class CDSJSONSerializer (JSONSerializer ):
@@ -46,6 +47,41 @@ def dump(self, obj, context=None):
4647 """Serialize object with schema."""
4748 return self .schema_class (context = context ).dump (obj )
4849
50+ def _sanitize_metadata (self , metadata ):
51+ """Sanitize title, description and translations in metadata."""
52+ try :
53+ if "title" in metadata and "title" in metadata ["title" ]:
54+ title = metadata ["title" ]["title" ]
55+ title = self .html_tag_remover .unescape (title )
56+ metadata ["title" ]["title" ] = remove_html_tags (
57+ self .html_tag_remover , title
58+ )
59+
60+ if "description" in metadata :
61+ description = metadata ["description" ]
62+ description = self .html_tag_remover .unescape (description )
63+ metadata ["description" ] = sanitize_html (description )
64+
65+ if "translations" in metadata :
66+ for t in metadata ["translations" ]:
67+ if "title" in t and "title" in t ["title" ]:
68+ t_title = t ["title" ]["title" ]
69+ t_title = self .html_tag_remover .unescape (t_title )
70+ t ["title" ]["title" ] = remove_html_tags (
71+ self .html_tag_remover , t_title
72+ )
73+
74+ if "description" in t :
75+ t_desc = t ["description" ]
76+ t_desc = self .html_tag_remover .unescape (t_desc )
77+ t ["description" ] = sanitize_html (t_desc )
78+
79+ except KeyError :
80+ # ignore error if keys are missing
81+ pass
82+
83+ return metadata
84+
4985 def preprocess_record (self , pid , record , links_factory = None ):
5086 """Include ``_eos_library_path`` for single record retrievals."""
5187 result = super (CDSJSONSerializer , self ).preprocess_record (
@@ -62,16 +98,7 @@ def preprocess_record(self, pid, record, links_factory=None):
6298
6399 # sanitize title by unescaping and stripping html tags
64100 try :
65- title = metadata ["title" ]["title" ]
66- title = self .html_tag_remover .unescape (title )
67- metadata ["title" ]["title" ] = remove_html_tags (
68- self .html_tag_remover , title
69- )
70-
71- # decode html entities
72- metadata ["description" ] = self .html_tag_remover .unescape (
73- metadata ["description" ]
74- )
101+ metadata = self ._sanitize_metadata (metadata )
75102 if has_request_context ():
76103 metadata ["videos" ] = [
77104 video
@@ -93,19 +120,6 @@ def preprocess_search_hit(self, pid, record_hit, links_factory=None):
93120
94121 if "metadata" in result :
95122 metadata = result ["metadata" ]
96-
97- try :
98- title = metadata ["title" ]["title" ]
99- title = self .html_tag_remover .unescape (title )
100- metadata ["title" ]["title" ] = remove_html_tags (
101- self .html_tag_remover , title
102- )
103-
104- metadata ["description" ] = self .html_tag_remover .unescape (
105- metadata ["description" ]
106- )
107- except KeyError :
108- # ignore error if keys are missing in the metadata
109- pass
123+ result ["metadata" ] = self ._sanitize_metadata (result ["metadata" ])
110124
111125 return result
0 commit comments