使用SetIsOriginAllowed时应该多小心?

问题描述 投票:0回答:1

我在Startup.cs中找到了以下代码:

app.UseCors(options => options
          .SetIsOriginAllowed(origin => origin.EndsWith("SomeWebsite.com"))
          .AllowAnyMethod()
          .AllowAnyHeader()
          .AllowCredentials()
          .SetPreflightMaxAge(TimeSpan.FromSeconds(2520)
    )

我担心

EndsWith()
方法。

根据此代码的当前状态,这是否意味着像

maliciousSomeWebsite.com
这样的网站可以执行 CSRF 攻击?

代码应该改为

origin.EndsWith(".SomeWebsite.com")
吗?

c# cors .net-6.0 same-origin-policy
1个回答
3
投票

我认为显式检查主域并为子域添加通配符检查更安全,这应该可行:

services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder => builder
                    .WithOrigins
                    (
                        "https://*.SomeWebsite.com"
                    )
                    .SetIsOriginAllowedToAllowWildcardSubdomains()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });
© www.soinside.com 2019 - 2024. All rights reserved.