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,44 @@ 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+ # top-level title
54+ if "title" in metadata and "title" in metadata ["title" ]:
55+ title = metadata ["title" ]["title" ]
56+ title = self .html_tag_remover .unescape (title )
57+ metadata ["title" ]["title" ] = remove_html_tags (
58+ self .html_tag_remover , title
59+ )
60+
61+ # top-level description
62+ if "description" in metadata :
63+ description = metadata ["description" ]
64+ description = self .html_tag_remover .unescape (description )
65+ metadata ["description" ] = sanitize_html (description )
66+
67+ # translations
68+ if "translations" in metadata :
69+ for t in metadata ["translations" ]:
70+ if "title" in t and "title" in t ["title" ]:
71+ t_title = t ["title" ]["title" ]
72+ t_title = self .html_tag_remover .unescape (t_title )
73+ t ["title" ]["title" ] = remove_html_tags (
74+ self .html_tag_remover , t_title
75+ )
76+
77+ if "description" in t :
78+ t_desc = t ["description" ]
79+ t_desc = self .html_tag_remover .unescape (t_desc )
80+ t ["description" ] = sanitize_html (t_desc )
81+
82+ except KeyError :
83+ # ignore error if keys are missing
84+ pass
85+
86+ return metadata
87+
4988 def preprocess_record (self , pid , record , links_factory = None ):
5089 """Include ``_eos_library_path`` for single record retrievals."""
5190 result = super (CDSJSONSerializer , self ).preprocess_record (
@@ -62,16 +101,7 @@ def preprocess_record(self, pid, record, links_factory=None):
62101
63102 # sanitize title by unescaping and stripping html tags
64103 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- )
104+ metadata = self ._sanitize_metadata (metadata )
75105 if has_request_context ():
76106 metadata ["videos" ] = [
77107 video
@@ -93,19 +123,6 @@ def preprocess_search_hit(self, pid, record_hit, links_factory=None):
93123
94124 if "metadata" in result :
95125 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
126+ result ["metadata" ] = self ._sanitize_metadata (result ["metadata" ])
110127
111128 return result
0 commit comments