プログラミングガール

Developing a better me

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が返却される。
f:id:hana_ori:20170813002311p:plain
403はアクセス権限がない場合なのでなんの違和感もない。

Github

404が返却される。
f:id:hana_ori:20170813002619p:plain
なぜアクセス権限がないというエラーのはずなのに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を返してしまうとプライベートリポジトリが存在することがわかってしまう。
Githubwe don't want to provide any information about private repositorie とあるようにその情報すらも第三者に与えないように 404を返しているようだ。

所感

ちゃんと意識してエラーコードを返却しようと思った。