Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/components/OpenwbPageMessages.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@dismiss="dismissMessage"
@hide="hideMessage"
>
<span v-html="message.message" />
<span v-html="sanitizeMessage(message.message)" />

Check warning on line 55 in src/components/OpenwbPageMessages.vue

View workflow job for this annotation

GitHub Actions / build (24)

'v-html' directive can lead to XSS attack

Check warning on line 55 in src/components/OpenwbPageMessages.vue

View workflow job for this annotation

GitHub Actions / build (22)

'v-html' directive can lead to XSS attack
</openwb-base-toast>
</div>
</template>
Expand Down Expand Up @@ -179,6 +179,32 @@
this.hiddenMessages.push(event.topic);
}
},
/**
* Sanitize HTML message to allow only safe tags like links and line breaks
*/
sanitizeMessage(message) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hier könnte eine Liste mit gültigen Tags und Attributen definiert werden.

const validTags = [
  { name: "br", validAttributes: [] },
  { name: "a", validAttributes: ["href", "target"] },
  ...
];

if (!message) return '';

// Escape all HTML first
const escaped = message
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#x27;');

// Then allow specific safe tags back
return escaped
// Allow <br> tags
.replace(/&lt;br\s*\/?&gt;/gi, '<br>')
// Allow <a> tags with href and target attributes
.replace(/&lt;a\s+href=&quot;([^&"]+)&quot;(?:\s+target=&quot;([^&"]+)&quot;)?&gt;([^&]+)&lt;\/a&gt;/gi,
'<a href="$1" target="$2" rel="noopener noreferrer">$3</a>')
// Allow <strong> and <b> tags
.replace(/&lt;(strong|b)&gt;([^&]+)&lt;\/(strong|b)&gt;/gi, '<$1>$2</$3>')
// Allow <em> and <i> tags
.replace(/&lt;(em|i)&gt;([^&]+)&lt;\/(em|i)&gt;/gi, '<$1>$2</$3>');
},
},
};
</script>
Expand Down