对于不安全的 POST 不可接受返回什么响应代码?

发布于 2024-12-26 01:34:11 字数 182 浏览 0 评论 0原文

如果有人通过 http(即不是 https)访问我的服务器,那么我将 GET 请求重定向到 https 版本。

但我不知道如何处理 POST 和 PUT,因为我无法重定向它们(我相信浏览器在重定向时执行 GET 操作)。

我应该返回一个错误代码。 我应该返回什么 HTTP 错误代码?

If somebody accesses my server via http (i. e. not https) then I redirect GET requests to the https version.

But I don't know what to do with POST and PUT because I cannot redirect them (the browser does a GET on redirect I believe).

I should return an error code. What HTTP error code should I return?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2025-01-02 01:34:11

通常,您会返回 403 - Forbidden。对此的一般描述是“服务器理解该请求,但拒绝授权”。这适合你的情况。

Typically, you would return a 403 - Forbidden. The general description of this is "The server understood the request, but refuses to authorize it." which would fit your situation.

暮凉 2025-01-02 01:34:11

从技术上讲,正确的答案是使用 403.4,但子状态是它只是 IIS 的功能,而 nginx 不支持它。

所以我更愿意返回 418“我是一个茶壶”或 451“出于法律原因不可用”(因为不再使用纯文本 http 是不合法的,哈哈,只是开玩笑:)),这种状态非常奇特,应该触发客户弄清楚发生了什么。

nginx 可以选择返回类似文本的状态,

# cat /etc/nginx/conf.d/default.conf
server {
    listen       80 default_server;


    location / {
        add_header Content-Type text/plain;
        return 418 "TLS REQUIRED";
    }
}

# curl -v  http://localhost/
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 418
< Server: nginx/1.16.1
< Date: Sat, 15 Feb 2020 07:13:20 GMT
< Content-Type: application/octet-stream
< Content-Length: 12
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
TLS REQUIRED

但是像 Chrome 或 Firefox 这样的浏览器无法正确处理这个问题,Chrome 会说 ERR_INVALID_RESPONSE,而 Firefox 会说“找不到文件”,这种行为并不能解决我们的目标。

所以我只返回状态 418。

Technically right answer is to use 403.4 but substatuses it's a IIS only feature and nginx doesn't support it.

So I've prefer to return 418 "I'm a teapot" or 451 "Unavailable For Legal Reasons" (because it's not legal to use plain-text http anymore, haha, just joking :)), this statuses quite exotic and should trigger client to figure out what going on.

nginx has an option to return status with text like

# cat /etc/nginx/conf.d/default.conf
server {
    listen       80 default_server;


    location / {
        add_header Content-Type text/plain;
        return 418 "TLS REQUIRED";
    }
}

# curl -v  http://localhost/
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 418
< Server: nginx/1.16.1
< Date: Sat, 15 Feb 2020 07:13:20 GMT
< Content-Type: application/octet-stream
< Content-Length: 12
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
TLS REQUIRED

But browsers like Chrome or Firefox can't handle this right, Chrome says ERR_INVALID_RESPONSE and Firefox says "File not found" and this behavior doesn't solve our goal.

So I've stay with just returning status 418.

花伊自在美 2025-01-02 01:34:11

http://www.ietf.org/id/draft -ietf-httpbis-p2-semantics-18.txt 解释 HTTP 响应代码。如果您想要错误代码,如果无法在该 URL 上处理请求,只需返回 404 即可。

7.4.5。 404 未找到

服务器未找到任何与有效请求 URI 匹配的内容。
没有说明这种情况是暂时的还是
永恒的。如果服务器
通过某种内部可配置机制知道,旧的
资源永久不可用并且没有转发地址。
当服务器不希望时,通常使用此状态代码
准确揭示请求被拒绝的原因,或者没有其他原因
响应适用。

http://www.ietf.org/id/draft-ietf-httpbis-p2-semantics-18.txt explains HTTP response codes. If you want an error code, just return a 404 if requests cannot be serviced at that URL.

7.4.5. 404 Not Found

The server has not found anything matching the effective request URI.
No indication is given of whether the condition is temporary or
permanent. The 410 (Gone) status code SHOULD be used if the server
knows, through some internally configurable mechanism, that an old
resource is permanently unavailable and has no forwarding address.
This status code is commonly used when the server does not wish to
reveal exactly why the request has been refused, or when no other
response is applicable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文