Cara meneruskan html sebagai string di c #

Selama beberapa hari terakhir, kami telah melihat cara kerja serangan skrip lintas situs, dan bagaimana menyuntikkan teks biasa atau string HTML yang disandikan dapat membantu Anda tetap lebih aman. Kami juga melihat beberapa kelemahan dari kedua teknik tersebut

Hari ini, kita akan melihat satu pendekatan terakhir. sanitasi. Mari kita menggali lebih dalam

Apa itu sanitasi?

Sanitasi adalah proses menghapus atribut, properti, dan nilai apa pun yang tidak termasuk dalam daftar yang diizinkan atau yang secara eksplisit dilarang dalam daftar yang tidak diizinkan

Misalnya, jika HTML yang dirender dari string HTML kita terlihat seperti ini…

<p><img src=x" onerror="alert('XSS Attack')"></p>
<p><a href="javascript:alert('Another XSS Attack')">View My Profile</a></p>

Versi yang sudah dibersihkan mungkin terlihat seperti ini

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_

Saat ditambahkan ke UI, beberapa item mungkin terlihat rusak, tetapi konten berbahaya tidak akan ditampilkan

Cara membersihkan string HTML dengan vanilla JS

Metode

<p><img src=x"></p>
<p><a>View My Profile</a></p>
5 mengonversi string HTML menjadi HTML asli tanpa merendernya di DOM sebenarnya. Akibatnya, kode jahat apa pun tidak dijalankan (dan tidak akan sampai elemen HTML tersebut disuntikkan ke UI)

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);

Pustaka Sanitizer menggunakan metode

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_5 untuk membuat elemen HTML dari string HTML Anda, lalu mengulang setiap elemen dan menghapus semua atribut, properti, dan nilai yang tidak termasuk dalam daftar yang diizinkan atau secara eksplisit dilarang pada daftar yang tidak diizinkan

Anda dapat meneruskan seluruh string HTML Anda ke pustaka pembersih, dan itu akan mengembalikan string yang telah disanitasi yang dapat Anda gunakan dengan properti string HTML, atau elemen yang telah disanitasi yang dapat Anda masukkan ke dalam DOM dengan metode seperti

<p><img src=x"></p>
<p><a>View My Profile</a></p>
7

DOMPurify adalah library terkemuka di industri yang menggunakan daftar yang diizinkan dan sangat dapat dikonfigurasi

Saya sangat merekomendasikannya. Namun hari ini, kita juga akan melihat cara membuat versi kita sendiri yang kurang dapat dikonfigurasi

Membuat perpustakaan sanitasi

Pertama, mari buat fungsi pembungkus untuk pustaka kita bernama

<p><img src=x"></p>
<p><a>View My Profile</a></p>
8. Kami akan menerima string untuk dibersihkan sebagai argumen

Kami dapat mengembalikan string yang disanitasi ATAU node yang disanitasi itu sendiri. Mari beri pengguna kemampuan untuk memutuskan mana yang mereka inginkan dengan argumen kedua,

<p><img src=x"></p>
<p><a>View My Profile</a></p>
9. Jika
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
_0, kami akan mengembalikan node, bukan string

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_

Hal pertama yang ingin kita lakukan adalah mengonversi HTML

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
1 kita menjadi node HTML yang sebenarnya

Mari buat fungsi pembantu,

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
2, untuk melakukannya untuk kita. Di dalamnya, kita akan menggunakan konstruktor
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
3 dan metode
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
4, dan mengembalikan
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
5. Jika tidak ada karena beberapa alasan, kami akan mengembalikan elemen
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
6 baru sebagai gantinya

Kami akan segera menjalankannya, dan menetapkan nilai yang dikembalikan ke variabel

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
7

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}

Sekarang, kami siap untuk membersihkannya

Menghapus let parser = new DOMParser(); let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html'); // doc.body is a real HTML element with the malicious image // No alert is thrown, though, because the elements exist outside the DOM console.log(doc.body); 8 elemen

Pertama, kami ingin menghapus semua

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
_8 elemen dari HTML kami. Mari buat fungsi
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_0 yang menerima simpul
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
7 sebagai argumen

Kami akan menggunakan metode

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_2 untuk menemukan semua elemen
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
8. Kemudian, kami akan menggunakan
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_4 loop untuk melewati masing-masing loop, dan menggunakan metode
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
5 untuk menghapusnya dari DOM

/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}

Kemudian, kami akan meneruskan

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
_7 kami yang dikonversi ke dalamnya

// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);

Menghapus atribut berbahaya

Sekarang, kami siap menghapus atribut jahat dari

let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
7 kami

Mari buat fungsi

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_8 yang menerima elemen
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
7 sebagai parameter. Di dalamnya, kita akan menggunakan properti
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
_0 untuk mendapatkan semua elemen turunan dalam sebuah elemen

Kita dapat menggunakan

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
4 loop untuk melewati masing-masing loop. Kami akan meneruskannya ke fungsi
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
_2 untuk menghapus atribut berbahaya apa pun

Jika

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
_3 memiliki elemen anak itu sendiri, kami juga ingin membersihkannya. Kami akan secara rekursif meneruskan
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
3 kembali ke fungsi
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
8

/**
 * Remove dangerous stuff from the HTML document's nodes
 * @param  {Node} html The HTML document
 */
function clean (html) {
	let nodes = html.children;
	for (let node of nodes) {
		removeAttributes(node);
		clean(node);
	}
}

Dalam fungsi

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
_2, kita akan mendapatkan semua atribut pada elemen dengan properti
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
7

Kami akan mengulang masing-masing dengan

/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {
	// Do stuff here...
}
_4 loop, menggunakan penghancuran objek untuk mendapatkan
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
9 dan
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
0 dari atribut

Dalam loop, kami akan memeriksa apakah atribut

/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
1 menggunakan fungsi pembantu. Jika tidak, kami akan menggunakan operator
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
2 untuk melompat ke item berikutnya. Jika tidak, kami akan menggunakan metode
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
3 untuk menghapusnya

/**
 * Remove potentially dangerous attributes from an element
 * @param  {Node} elem The element
 */
function removeAttributes (elem) {

	// Loop through each attribute
	// If it's dangerous, remove it
	let atts = elem.attributes;
	for (let {name, value} of atts) {
		if (!isPossiblyDangerous(name, value)) continue;
		elem.removeAttribute(name);
	}

}

Untuk memeriksa apakah atribut kita berbahaya, pertama-tama kita akan melihat apakah itu dimulai dengan

/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
4, karena
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
5 acara menjalankan skrip

Kita bisa melakukannya dengan metode

/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
6. Kami akan mengembalikan
let parser = new DOMParser();
let doc = parser.parseFromString(`<img src="x" onerror="alert('XSS attacks!')">`, 'text/html');

// doc.body is a real HTML element with the malicious image
// No alert is thrown, though, because the elements exist outside the DOM
console.log(doc.body);
_0 jika itu terjadi

/**
 * Check if the attribute is potentially dangerous
 * @param  {String}  name  The attribute name
 * @param  {String}  value The attribute value
 * @return {Boolean}       If true, the attribute is potentially dangerous
 */
function isPossiblyDangerous (name, value) {
	if (name.startsWith('on')) return true;
}

Selanjutnya, kami ingin mencari

/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
_8 dan
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
9 ketika atributnya adalah
// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
0,
// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
1, atau
// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
2 (atribut yang tidak digunakan lagi pada SVG)

Kami pertama-tama akan memasukkan nama properti itu ke dalam array, dan menggunakan metode

// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
3 untuk melihat apakah
/*!
 * Sanitize an HTML string
 * (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String}          str   The HTML string to sanitize
 * @param  {Boolean}         nodes If true, returns HTML nodes instead of a string
 * @return {String|NodeList}       The sanitized string or nodes
 */
function cleanHTML (str, nodes) {

	/**
	 * Convert the string to an HTML document
	 * @return {Node} An HTML document
	 */
	function stringToHTML () {
		let parser = new DOMParser();
		let doc = parser.parseFromString(str, 'text/html');
		return doc.body || document.createElement('body');
	}

	// Convert the string to HTML
	let html = stringToHTML();

}
9 adalah salah satunya. Jika ya, kita dapat menggunakan metode
// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
_5 untuk memeriksa
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
8 dan
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
9 di
/**
 * Remove <script> elements
 * @param  {Node} html The HTML
 */
function removeScripts (html) {
	let scripts = html.querySelectorAll('script');
	for (let script of scripts) {
		script.remove();
	}
}
0

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_0

Tapi peretas menggunakan segala macam trik kapitalisasi dan spasi untuk menghindari hal-hal seperti itu, jadi kita perlu menormalkan string kita

Kita dapat menggunakan metode

// Convert the string to HTML
let html = stringToHTML();

// Sanitize it
removeScripts(html);
_9 untuk menghapus semua spasi dari string kita, dan metode
/**
 * Remove dangerous stuff from the HTML document's nodes
 * @param  {Node} html The HTML document
 */
function clean (html) {
	let nodes = html.children;
	for (let node of nodes) {
		removeAttributes(node);
		clean(node);
	}
}
0 untuk mengubahnya menjadi huruf kecil. Kami akan menetapkan string yang dinormalisasi ke variabel
/**
 * Remove dangerous stuff from the HTML document's nodes
 * @param  {Node} html The HTML document
 */
function clean (html) {
	let nodes = html.children;
	for (let node of nodes) {
		removeAttributes(node);
		clean(node);
	}
}
1, dan menggunakannya sebagai gantinya

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_1

Sekarang, kami memiliki pembersih yang lengkap

Menggunakannya

Katakanlah kita memiliki string pihak ketiga seperti ini

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_2

Kita bisa menyuntikkannya ke DOM seperti ini

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_3

Sebagai alternatif, kita dapat menggunakan operator spread dan metode

/**
 * Remove dangerous stuff from the HTML document's nodes
 * @param  {Node} html The HTML document
 */
function clean (html) {
	let nodes = html.children;
	for (let node of nodes) {
		removeAttributes(node);
		clean(node);
	}
}
2 dengan node sebagai gantinya

<p><img src=x"></p>
<p><a>View My Profile</a></p>
_4

Ini demonya. Anda juga dapat mengunduh skrip yang sudah selesai di Vanilla JS Toolkit

Jika Anda menginginkan sesuatu yang lebih andal, DOMPurify adalah pustaka terkemuka di industri yang menggunakan daftar yang diizinkan dan sangat dapat dikonfigurasi

Bagaimana cara mengubah file HTML menjadi string di C#?

Langkah-langkah untuk Mengonversi HTML menjadi Teks di C# .
Instal Aspose. HTML untuk. NET dari manajer paket NuGet
Sertakan Aspose. namespace HTML di proyek Anda
Muat konten file HTML ke sebuah String
Buat instance kelas HTMLDocument untuk memuat String yang berisi HTML

Bagaimana cara membaca file HTML di C# dan mengganti string?

Coba ikuti. keluaran string = Regex. Replace(input, "XXX", "Kiran"); dengan input template html Anda sebagai string.

Bagaimana Anda menulis string dalam HTML?

Penggunaan. Anda dapat membuat instance String menggunakan teks biasa atau HTML, misalnya kedua pernyataan ini valid. // Dari teks biasa var helloWorld = new HTMLString. String('Halo Dunia'); // Dari HTML var helloWorldBold = HTMLString baru.

Bagaimana cara memuat halaman HTML di C#?

Cara memuat dokumen HTML di C# dan VB. Bersih .
Muat dari file. DocumentCore dc = DocumentCore. Muat(@"d. \Buku. html"); Objek dc mewakili dokumen yang dimuat ke dalam memori. .
Muat dari Arus. // Katakanlah kita sudah memiliki dokumen HTML sebagai array byte