简体   繁体   English

在 svete 中,使用什么代替 @html 来避免 xss 攻击?

[英]in svete, what to use instead of @html, to avoid xss attacks?

i'm creating a little chat for a website, the front is in svelte.我正在为一个网站创建一个小聊天,前面很苗条。

when users enter their prompt inside the text field (in my case, it is a contenteditable div), I grab it and do two things: I send it to the backend with a websocket, to display it on other users' windows, and i print it directly on the sender chat window, without going through the server当用户在文本字段中输入他们的提示时(在我的例子中,它是一个 contenteditable div),我抓住它并做两件事:我用 websocket 将它发送到后端,以在其他用户的 windows 上显示它,然后我直接在发件人聊天 window 上打印,无需通过服务器

here my question is specifically about the security of the text that is taken from the div and printed directly in the other div (the chat display).在这里,我的问题特别是关于从 div 中获取并直接打印在另一个 div(聊天显示)中的文本的安全性。 It's not because i already figured out the best secure way of treating the text server side, but because I'm not even there;)这不是因为我已经想出了处理文本服务器端的最佳安全方法,而是因为我什至不在那里;)

I do that, inserting the user's input on the chat display, by using the {@html} svelte property, because it's the only way I found, using svelte capacities, to keep the newlines.我这样做,通过使用{@html} svelte 属性将用户的输入插入聊天显示,因为这是我发现的唯一方法,使用 svelte 容量来保留换行符。

But in the doc, it's specified to be careful about xss attacks: https://svelte.dev/tutorial/html-tags但在文档中,指定要小心 xss 攻击: https://svelte.dev/tutorial/html-tags

And on this discussion, https://github.com/sveltejs/svelte/issues/7253 , some people that seems to know a lot more than me what is good svelte and good security practice, say that maybe we shouldn't even use this feature {@html} .在这个讨论中, https://github.com/sveltejs/svelte/issues/7253 ,有些人似乎比我更了解什么是好的苗条和好的安全实践,说也许我们甚至不应该使用此功能{@html} I wonder what I can do at least to increase security, they mention using 'DOMPurify'.我想知道我至少可以做些什么来提高安全性,他们提到使用“DOMPurify”。 But, what else should I do, instead of using {@html} ?但是,我还应该做什么,而不是使用{@html} I tried to use bind:textContent to grab the text, instead of bind:innerHTML , but I loose the newlines, which is not ok我尝试使用bind:textContent来抓取文本,而不是bind:innerHTML ,但是我放掉了换行符,这是不行的

here is a simplified example of my code:这是我的代码的一个简化示例:

Item.svelte

<p style="border: 1px solid black;">
    <slot></slot>
</p>

App.svelte

<script>
  import Item from './Item.svelte';
  let items = [];
  let text = "";

  function add() {
    items = [...items, { inner: text }];
  }
</script>

<div bind:innerHTML={text} contenteditable=true></div>
<button on:click={add}>add</button>
<div>
  {#each items as item}
    <Item>{@html item.inner}</Item>
  {/each}
</div>

and here is the REPL of this simplify version, with a little more options to test innerHTML or textContent , with css white-space: pre-wrap;这是这个简化版本的 REPL,带有更多选项来测试innerHTMLtextContent ,带有 css white-space: pre-wrap; for exemple (which do nothing) https://svelte.dev/repl/f2095dd2195543eabb00068c1cb1e6e6?version=3.55.0例如(什么都不做) https://svelte.dev/repl/f2095dd2195543eabb00068c1cb1e6e6?version=3.55.0

as you can see;如你看到的; if you try to put a text with newlines, the only option that save the newlines is to grab the text with bind:innerHTML and to print it with {@html text}如果您尝试放置带有换行符的文本,保存换行符的唯一选择是使用bind:innerHTML获取文本并使用{@html text}打印它

so what should I do?所以我该怎么做? What is the alternative to not use @html , that they are talking about in this github page?他们在这个 github 页面中谈论的不使用@html的替代方法是什么? should i leave the svelte approach for this element and do a vanilla js createElement() and appendChild() and all that stuff?我应该为这个元素保留精巧的方法并做一个普通的 js createElement()appendChild()和所有这些东西吗? Is it preferable?它更可取吗? Or using a library to sanitize the text is a good option?或者使用库来清理文本是一个不错的选择?

Sveltekit becoming the defacto standard to deploy svelte apps, you may go with {@html} block directive and just carefully configure the Content Security Policies as stated in the documentation Sveltekit 成为部署 svelte 应用程序的事实标准,您可以使用{@html}块指令 go 并按照文档中的说明仔细配置内容安全策略

To paraphrase said docs, in your svelte.config.js file, you should add, at least, the following stuff:换句话说,在你的 svelte.config.js 文件中,你应该至少添加以下内容:

    const config = {
      kit: {
        csp: {
          directives: {
            'script-src': ['self']
          },
          reportOnly: {
            'script-src': ['self']
          }
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM