@@ -51,23 +51,57 @@ function registerLinkSafetyHook() {
5151}
5252
5353/**
54- * Copies a relationship with label markup limited to source links and markers.
54+ * Sanitizes a relationship label down to links and removal markers.
5555 * The report-wide sanitizer permits template styles, so labels need their own
5656 * allow-list before interpolation. Never trust a precomputed value from JSON.
5757 *
58+ * @param {string } label - Raw label markup from the report JSON.
59+ * @returns {string } Label restricted to anchor and marker markup.
60+ */
61+ function sanitizeRelationshipLabel ( label ) {
62+ return DOMPurify . sanitize ( label ?? '' , {
63+ ALLOWED_TAGS : [ 'a' , 'strong' ] ,
64+ ALLOWED_ATTR : [ 'href' , 'target' , 'rel' ] ,
65+ ALLOW_DATA_ATTR : false ,
66+ ALLOW_ARIA_ATTR : false
67+ } ) ;
68+ }
69+
70+ /**
71+ * Normalizes one "class relationship to break a package cycle" entry.
72+ * Reports serialize ClassRelationshipDTO objects; entries of any other
73+ * shape (including pre-DTO rendered-label strings) are dropped.
74+ *
75+ * @param {* } entry - Raw class-break entry.
76+ * @returns {object|null } Entry extended with a sanitized label, or null when
77+ * the entry is not a valid ClassRelationshipDTO object.
78+ */
79+ function prepareClassBreakEntry ( entry ) {
80+ if ( ! entry || typeof entry !== 'object' ) return null ;
81+ return {
82+ ...entry ,
83+ safeRenderedLabel : sanitizeRelationshipLabel ( entry . renderedLabel )
84+ } ;
85+ }
86+
87+ /**
88+ * Copies a relationship with label markup limited to source links and markers,
89+ * and normalizes its class-break entries the same way. Never trust a
90+ * precomputed value from JSON.
91+ *
5892 * @param {object } relationship - Raw relationship data.
59- * @returns {object } Relationship with a restricted HTML label .
93+ * @returns {object } Relationship with restricted HTML labels .
6094 */
6195function prepareRelationship ( relationship ) {
6296 registerLinkSafetyHook ( ) ;
6397 return {
6498 ...relationship ,
65- safeRenderedLabel : DOMPurify . sanitize ( relationship . renderedLabel ?? '' , {
66- ALLOWED_TAGS : [ 'a' , 'strong' ] ,
67- ALLOWED_ATTR : [ 'href' , 'target' , 'rel' ] ,
68- ALLOW_DATA_ATTR : false ,
69- ALLOW_ARIA_ATTR : false
70- } )
99+ safeRenderedLabel : sanitizeRelationshipLabel ( relationship . renderedLabel ) ,
100+ classRelationshipsToBreakPackage : Array . isArray ( relationship . classRelationshipsToBreakPackage )
101+ ? relationship . classRelationshipsToBreakPackage
102+ . map ( prepareClassBreakEntry )
103+ . filter ( entry => entry !== null )
104+ : [ ]
71105 } ;
72106}
73107
0 commit comments