News for hackers. Stuff that matters.
Re:API Shouldn't Redirect HTTP to HTTPS by marcosdumay on Tuesday May 28, 2024 @08:44PM (#40507077)
If anybody is looking to copy in a public API, please return 400 and don't misuse a standard code.
Re:API Shouldn't Redirect HTTP to HTTPS by CGamesPlay on Wednesday May 29, 2024 @12:29AM (#40508480)
Not sure how I feel about this (extremely arbitrary) distinction. 400 Bad Request maybe implies that no matter how many times you retry the request, it will never succeed. 403 Forbidden maybe implies that some external change in the authentication system could perhaps allow the same request to succeed in the future? So I guess in that lens I can see the logic, but again, seems extremely arbitrary.
Re:API Shouldn't Redirect HTTP to HTTPS by hn_throwaway_99 on Wednesday May 29, 2024 @12:20AM (#40508423)
Why do you think 403 is the wrong error code? Based on the spec it seems entirely appropriate to me:

> HTTP 403 provides a distinct error case from HTTP 401; while HTTP 401 is returned when the client has not authenticated, and implies that a successful response may be returned following valid authentication, HTTP 403 is returned when the client is not permitted access to the resource despite providing authentication such as insufficient permissions of the authenticated account.[a]

> Error 403: "The server understood the request, but is refusing to authorize it." (RFC 7231)

Re:API Shouldn't Redirect HTTP to HTTPS by samatman on Wednesday May 29, 2024 @11:50AM (#40513243)
I thought the best response from the article was the 426 Upgrade Required. That way you can throw in an Upgrade: https field in the response. It makes it immediately clear that something weird is going on. 403s are common, my first thought would be a badly-configured API key which lacks the expected permissions.
Re:API Shouldn't Redirect HTTP to HTTPS by marcosdumay on Wednesday May 29, 2024 @12:34PM (#40513856)
Because it's not an authorization error.

You do not throw 403 if the client is authorized to access whatever resource it's trying to.

The 426 on the sibling comment is great, though. But if you don't find an error code for your case, you don't go and redefined a well defined one. You use 400 (or 500).

Re:API Shouldn't Redirect HTTP to HTTPS by ants_everywhere on Tuesday May 28, 2024 @09:06PM (#40507209)
400 is usually for a malformed request. It seems like in this case the request is well formed, it's just not allowed. 403 seems reasonable if the user isn't authorized to make a request to the URL, which they aren't. Some APIs return redirects which also seems pretty reasonable.
Re:API Shouldn't Redirect HTTP to HTTPS by mbreese on Tuesday May 28, 2024 @11:08PM (#40508032)
But that also implies that some user would be authorized to make a request to the HTTP port (or that the resource does exist, which in this case it doesn’t).

IMO, 400 is more accurate, but really either could be acceptable, so long as the client is notified of the error. But, I wouldn’t automatically redirect the client. That’s what we are trying to avoid.

Re:API Shouldn't Redirect HTTP to HTTPS by ants_everywhere on Wednesday May 29, 2024 @10:43AM (#40512504)
Good point.

I guess this might depend a little on the implementation. In some cases the http endpoint may exist but may only be accessible to a sidecar container via localhost. For example, if the sidecar terminates https.

Re:API Shouldn't Redirect HTTP to HTTPS by endofreach on Tuesday May 28, 2024 @09:35PM (#40507383)
Well in that case really anything is a 403.
Re:API Shouldn't Redirect HTTP to HTTPS by kijin on Tuesday May 28, 2024 @09:42PM (#40507437)
404 would also work, since the resource does not exist at the http: address.
Re:API Shouldn't Redirect HTTP to HTTPS by bruce511 on Wednesday May 29, 2024 @12:31AM (#40508485)
True, but 404 has trained us to look hard at the URL part, not the protocol part.

Whereas 403 or 400 are less likely to have so automated built-in handling on the client side.