MeWrite Docs

HTTP 403 Forbidden

認証済みだがリソースへのアクセス権限がない場合に返されるエラー

概要

403 Forbidden は、サーバーがリクエストを理解したが、認可(権限)の問題でアクセスを拒否したことを示すHTTPステータスコードです。401とは異なり、認証は成功しているが権限が不足している場合に返されます。

エラーメッセージ

HTTP/1.1 403 Forbidden
1
2
3
4
5
{
  "error": "Forbidden",
  "message": "You don't have permission to access this resource",
  "statusCode": 403
}
Access Denied - You don't have permission to access "/" on this server.

原因

  1. ロールや権限の不足: ユーザーに必要な権限が付与されていない
  2. IPアドレス制限: 許可されていないIPからのアクセス
  3. WAF/ファイアウォールによるブロック: Web Application Firewallがリクエストをブロック
  4. ファイルパーミッション: サーバー上のファイル/ディレクトリの権限不足
  5. ディレクトリリスティング無効: index ファイルがなくディレクトリ一覧が無効
  6. CSRFトークンの不一致: フォーム送信時のトークン検証失敗

解決策

1. RBAC(ロールベースアクセス制御)の確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Express.js ミドルウェアの例
function checkPermission(requiredRole) {
  return (req, res, next) => {
    const userRoles = req.user?.roles || [];
    if (!userRoles.includes(requiredRole)) {
      return res.status(403).json({
        error: 'Forbidden',
        message: `Required role: ${requiredRole}`
      });
    }
    next();
  };
}

// 使用例
app.get('/admin/users', checkPermission('admin'), (req, res) => {
  // admin ロールを持つユーザーのみアクセス可能
});

2. AWS IAM ポリシーの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}
1
2
3
4
5
6
# IAMポリシーのシミュレーション
aws iam simulate-principal-policy \
  --policy-source-arn arn:aws:iam::123456789012:user/myuser \
  --action-names s3:GetObject \
  --resource-arns arn:aws:s3:::my-bucket/file.txt \
  --profile myprofile

3. Nginx でのアクセス制御

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# IP制限の確認と修正
location /admin {
    # 許可するIPを追加
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    deny all;
}

# ファイルパーミッションの確認
# Nginx ワーカープロセスのユーザーがファイルを読めるか
1
2
3
4
# ファイルパーミッションの修正
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
sudo chmod -R 644 /var/www/html/*.html

4. Apache でのアクセス制御

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# .htaccess の確認
<Directory /var/www/html>
    Options -Indexes
    AllowOverride All
    Require all granted
</Directory>

# 特定ディレクトリへのアクセス制限
<Directory /var/www/html/admin>
    Require ip 192.168.1.0/24
</Directory>

5. S3 バケットポリシーの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-bucket/*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": "203.0.113.0/24"
        }
      }
    }
  ]
}

6. デバッグ手順

1
2
3
4
5
6
7
8
9
# レスポンスヘッダーの確認
curl -v https://example.com/protected-resource 2>&1

# 自分のIPアドレスの確認
curl -s https://httpbin.org/ip

# サーバーログの確認
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/apache2/error.log

関連エラー

HTTP の他のエラー

最終更新: 2026-02-03