8

RFC 2616, the document that defines HTTP/1.1, lays out the definitions of all the HTTP status codes, including 403 Forbidden.

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

That's not how Drupal uses it: Depending on the case, it either uses it for the default condition in callbacks (e.g. shortcut_link_add_inline()), essentially making it equivalent to Drupal doesn't understand the request, or it is used to inform the client it doesn't have access to the page.

In the case of the former (the request isn't understood), the more appropriate status code should be 400 Bad Request.

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

In the latter case (the client does not have access to the page due to permissions), it should either be 401 Unauthorized, if Drupal wants to give away it's just a permission problem.

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.

Drupal could also use 404 File Not Found, if it doesn't want to make public it's a permission problem.

The server has not found anything matching the 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.

I realize it's been like this for many years, but what was the original rationale for making 403 Forbidden the only response Drupal sends when it gets a request it doesn't like?

1 Answer 1

6

As stated in your quote, 401 Unauthorized must be combined with HTTP authentication, which is not what Drupal is using, so this is not an option.

IMO, 403 is a perfectly valid response because clients must not repeat their request, but eventually fill out a login form which will result in a different request.

Special cases like the short link function you posted aren't so clear. I guess the reason for using access denied there is that it's checking the token which is a kind of authentication. It could just as well use 400 or 404. I guess the reason for using 403 instead of 400 is simply because there is no helper function for returning a 400.

There also has been a discussion regarding the Privatemsg module (that I'm maintaing) whether trying a private message as anonymous user should result in a 403 or a 404 (for privacy reasons). It used to be 404 but has been changed to 403 because users often click on direct links in mail notifications and a 403 page can easily be turned into a login form (as there is a module that does this out of the box). So this is related to the last two sentences of the 403 description.

1
  • The main concern remains: When receiving a 403 for e.g. an unpublished draft /node/123 or /the-truth-about-my-lower-body-parts (url alias), we know now that this url does exist. If we want to keep this fact a secret, we need to send a 404 (or something else?) instead.
    – donquixote
    Jan 14, 2019 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.