- Vue.js 2 Web Development Projects
- Guillaume Chau
- 302字
- 2021-07-02 22:34:25
Text interpolation escaping
Let's try to display our note in a new pane using a text interpolation:
- Create an <aside> element with the preview class, which displays our notePreview computed property:
<!-- Preview pane --> <aside class="preview"> {{ notePreview }} </aside>
We should now have the preview pane displaying our note on the right side of the app. If you type some text in the note editor, you should see the preview updating automatically. However, there is an issue with our app, which arises when you use markdown formatting.
- Try making your text bold by surrounding it with **, as follows:
I'm in **bold**!
Our computed property should return this in valid HTML, and we should have some bold text rendered in our preview pane. Instead, we can see the following:
I'm in <strong>bold</strong>!
We have just discovered that the text interpolation automatically escapes the HTML tags. This is to prevent injection attacks and improve the security of our app. Fortunately, there is a way to display some HTML, as we will see in a moment. However, this forces you to think about using it to include potentially harmful dynamic content.
For example, you create a comment system, where any user can write some text to comment on your app pages. What if someone writes some HTML in their comment, which is then displayed in the page as valid HTML? They could add some malicious JavaScript code, and all the visitors of your app would be vulnerable. It's called a cross-site scripting attack, or an XSS attack. That's why text interpolation always escapes HTML tags.
It is not recommended to use v-html on content created by the users of the application. They could write malicious JavaScript code inside a <script> tag that would be executed. However, with normal text interpolation, you would be safe because the HTML would not be executed.
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁設(shè)計(jì)密碼
- Progressive Web Apps with React
- SoapUI Cookbook
- Java EE 6 企業(yè)級(jí)應(yīng)用開發(fā)教程
- Python計(jì)算機(jī)視覺編程
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- WordPress Plugin Development Cookbook(Second Edition)
- Visual C++數(shù)字圖像處理技術(shù)詳解
- Swift語言實(shí)戰(zhàn)精講
- MINECRAFT編程:使用Python語言玩轉(zhuǎn)我的世界
- Orleans:構(gòu)建高性能分布式Actor服務(wù)
- OpenMP核心技術(shù)指南
- C語言程序設(shè)計(jì)
- Python程序設(shè)計(jì)現(xiàn)代方法
- PyTorch生成對(duì)抗網(wǎng)絡(luò)編程