子域上的第三方代码

Third party code on subdomain

作为域名example.com的所有者,内容很多,将子域名提供给第三方公司会带来哪些安全风险。我们不想共享任何内容,第三方公司将完全控制托管子域站点的应用程序和机器。

我主要关心的是:

  1. 共享 cookies

我们有 cookie .example.com,因此也会在对子域的请求中发送。我们是否可以将 A 记录指向反向代理,我们在其中剥离 cookie 并在没有它们的情况下将请求发送给第三方提供商?

  1. 从主域加载内容

是否可以将 document.domain 设置为 example.com 并对 example.com 执行 XMLHttpRequest?

  1. 跨站脚本

同源政策我猜应该没问题。子域被视为单独的域?

还有其他安全问题吗?

We have cookies .example.com, so there will be sent also in the requests to subdomain. Is it possible for us to point A record to reverse proxy where we strip the cookies and send the request to third party provider without them?

好主意,你可以这样做是的,但是你还需要设置 HttpOnly 标志,否则他们将能够使用 JavaScript.

检索它们

Is it possible to set document.domain to example.com and do XMLHttpRequest to the example.com?

不,Ajax 的子域被视为不同的来源。参见 this answer

I guess that it would be no problem because of the same origin policy. Subdomain is treated as separate domain?

JavaScript 代码可以与其他子域交互 - 但只有与您的站点合作。您还需要设置 document.domain = 'example.com'; 如果您不这样做,您就可以免受此威胁。

参见here

When using document.domain to allow a subdomain to access its parent securely, you need to set document.domain to the same value in both the parent domain and the subdomain. This is necessary even if doing so is simply setting the parent domain back to its original value. Failure to do this may result in permission errors.


Any other security issues?

您需要注意 cookie 中毒。如果 evil.example.com.example.com 设置了一个非 host-only 的 cookie,而您的域认为它已自行设置,那么恶意 cookie 可能会用于您的站点。

比如你把cookie的内容显示为HTML,那么这可能会引入XSS. Also, if you're using the double submit cookies CSRF prevention method an evil domain may be able to set their own cookie value to achieve CSRF. See this answer.