TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Serialize Django Data for JavaScript

6 点作者 brento超过 2 年前

5 条评论

brento超过 2 年前
I've updated the article based on some of the feedback.
Chiron1991超过 2 年前
Or simply use Django&#x27;s builtin mechanism for that instead of poorly reimplementing it: <a href="https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;4.1&#x2F;ref&#x2F;templates&#x2F;builtins&#x2F;#json-script" rel="nofollow">https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;4.1&#x2F;ref&#x2F;templates&#x2F;builtins...</a>
评论 #32750194 未加载
chrismorgan超过 2 年前
&gt; <i>get_data_from_data_attribute({{ data|to_json }});</i><p>This constitutes an injection vulnerability that can be demonstrated with string &quot;&lt;&#x2F;script&gt;&lt;script&gt;alert(1&lt;2)&lt;&#x2F;script&gt;&lt;script&gt;&quot;.<p>If you want to inject arbitrary strings into a script tag, you need to reach for some more exotic escapes, starting with things like &lt;&#x2F;script&gt; → &lt;\&#x2F;script&gt; or \u003c&#x2F;script&gt;. But I’m not going to give you a complete solution because I don’t want you to think this is a good idea. (But if you really want to know: look at the <i>script data state</i> in the HTML spec, and follow the parser. All up, you could do it two different ways, one requiring three things and the other two, though one of the two&#x2F;three is impossible after JSON encoding.)<p>People often treat auto-escaping template languages as safe to do anything in, but they’re just <i>not</i> if you use HTML syntax—there are quite a few hazards to be aware of. Here you disabled auto-escaping (mark_safe in the to_json body) because the auto-escaping broke things in one way, but it introduced another vulnerability. For sanity in this context, you need to use XML syntax (by serving with the MIME type application&#x2F;xhtml+xml), though some JavaScript libraries might misbehave due to side-effects if they make bad assumptions, and third-party templates might not be designed for XML syntax. (All up, I don’t generally recommend using XML syntax, though it’s a close thing.)<p>Fun fact: you <i>can</i> actually use entity encoding in JavaScript in the HTML syntax… by using the <i>SVG</i> script tag instead of the HTML one:<p><pre><code> &lt;svg&gt;&lt;script&gt;alert(1&amp;lt;2)&lt;&#x2F;script&gt;&lt;&#x2F;svg&gt;</code></pre>
评论 #32750266 未加载
traverseda超过 2 年前
Uhh, don&#x27;t use the &quot;safe&quot; filter on user data that you&#x27;re embedding in javascript, as that introduces arbitrary code injection attacks.<p>As for the default representation not being flat like the author needed, you can use the &quot;values_list&quot; method on your queryset.<p>I worry that articles like this lead to &quot;the blind leading the blind&quot;. The arbitrary js injection attack enabled by their first example is concerning, and really should be accompanied by a big disclaimer saying &quot;don&#x27;t mess around with this filter unless you actually know what you&#x27;re doing&quot;.
评论 #32750299 未加载
acidburnNSA超过 2 年前
Neat.<p>I guess I&#x27;m a little surprised that the Django REST Framework isn&#x27;t mentioned, since I thought that&#x27;s the go-to for pretty much everyone for this task. Certainly this post&#x27;s code is lighter weight if all you need to do is send some data out of Django.<p><a href="https:&#x2F;&#x2F;www.django-rest-framework.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.django-rest-framework.org&#x2F;</a>
评论 #32738644 未加载
评论 #32737813 未加载