Webアプリケーションにおけるエラーコード:403と404 について
最近なるほどなあと思ったので書き留めておく。
そもそも403と404とは
403 Forbidden:
The request was valid, but the server is refusing action.
The user might not have the necessary permissions for a resource, or may need an account of some sort.
404 Not Found:
The requested resource could not be found but may be available in the future.
つまるところ403はそのリソースへのアクセス権限がなく、404はリソースが見つからないというエラー。
BitbucketとGithubのそれぞれのエラーコードの扱い
GithubとBitbacketはそれぞれプライベートリポジトリが作成可能だが、他人がそのプライベートリポジトリへアクセスしたときに返却されるエラーコードが異なっていた。
Bitbucket
403
が返却される。
403
はアクセス権限がない場合なのでなんの違和感もない。
Github
404
が返却される。
なぜアクセス権限がないというエラーのはずなのに404
なのだろう?
Githubが404を返す理由
Troubleshooting | GitHub Developer Guide
理由なんとなく予想してたけど、なんとちゃんと明記してあった。
Typically, we send a 404 error when your client isn’t properly authenticated. You might expect to see a 403 Forbidden in these cases. However, since we don’t want to provide any information about private repositories, the API returns a 404 error instead.
403
を返してしまうとプライベートリポジトリが存在することがわかってしまう。
Githubは we don't want to provide any information about private repositorie
とあるようにその情報すらも第三者に与えないように 404
を返しているようだ。
所感
ちゃんと意識してエラーコードを返却しようと思った。