Skip to content

Commit

Permalink
chore: sanitize label
Browse files Browse the repository at this point in the history
  • Loading branch information
acnormun committed Jan 31, 2025
1 parent 93e1cdb commit b07e39a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
8 changes: 7 additions & 1 deletion src/components/Label/Label.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<template>
<p class="unnnic-label__label">{{ label }}</p>
<p class="unnnic-label__label">{{ sanitizedValue(label) }}</p>
</template>

<script>
import { escapeHtml } from '../../utils/sanitize';
export default {
name: 'UnnnicLabel',
props: {
Expand All @@ -11,6 +12,11 @@ export default {
default: null,
},
},
methods: {
sanitizedValue(value){
return escapeHtml(value)
}
}
};
</script>

Expand Down
28 changes: 7 additions & 21 deletions src/utils/sanitize.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
// src/utils/sanitize.js

/**
* Escapa caracteres perigosos no HTML para evitar XSS.
* Removes all HTML tags while preserving plain text content.
* @param {string} input
* @returns {string}
*/
export function escapeHtml(input) {
if (typeof input !== 'string') return '';

// Remove todas as tags HTML, preservando o conteúdo de texto puro
// Remove all HTML tags while keeping the text content
return input.replace(/<[^>]*>/g, '');
}

/**
* Remove todas as tags HTML para retornar texto puro.
* @param {string} input
* @returns {string}
*/
export function stripHtml(input) {
if (typeof input !== 'string') return '';
const tempDiv = document.createElement('div');
tempDiv.innerHTML = input;
return tempDiv.textContent || '';
}

/**
* Verifica se uma URL é segura (http, https, mailto).
* Checks if a URL is safe (http, https, mailto).
* @param {string} url
* @returns {boolean}
*/
Expand All @@ -39,7 +25,7 @@ function isSafeUrl(url) {
}

/**
* Sanitiza o HTML permitindo apenas certas tags e removendo atributos perigosos.
* Sanitizes HTML by allowing only certain tags and removing dangerous attributes.
* @param {string} input
* @param {Array<string>} allowedTags
* @param {number} maxLength
Expand All @@ -48,7 +34,7 @@ function isSafeUrl(url) {
export function sanitizeHtml(input, allowedTags = [], maxLength = 1000) {
if (typeof input !== 'string') return '';

// Limitar o tamanho do texto
// Limit text length
if (input.length > maxLength) {
input = input.substring(0, maxLength);
}
Expand All @@ -60,13 +46,13 @@ export function sanitizeHtml(input, allowedTags = [], maxLength = 1000) {
for (let i = elements.length - 1; i >= 0; i--) {
const el = elements[i];

// Remover tags não permitidas
// Remove disallowed tags
if (!allowedTags.includes(el.nodeName.toLowerCase())) {
el.parentNode.removeChild(el);
continue;
}

// Remover atributos perigosos
// Remove dangerous attributes
const attributes = el.attributes;
for (let j = attributes.length - 1; j >= 0; j--) {
const attrName = attributes[j].name.toLowerCase();
Expand Down

0 comments on commit b07e39a

Please sign in to comment.