Insecure Cross-Origin Resource Sharing (CORS) policy
安全でないクロスオリジンリソース共有(CORS)ポリシー
説明
クロスオリジンリソース共有(CORS)は、HTTPヘッダーを使用して、Webクライアントが異なるドメイン上のサーバーのリソースにアクセスできるようにします。ブラウザーは、セキュリティ上の目的でスクリプト内から開始されるクロスオリジンHTTPリクエストを制限します。
ポリシーで別のドメインが許可されている場合、そのドメインはアプリケーションのユーザーを攻撃する可能性があります。ユーザーがアプリケーションにログインしており、ポリシーで許可されているドメインにアクセスした場合、そのドメインで実行されている悪意のあるコンテンツは、アプリケーションからコンテンツを取得し、ログインしているユーザーのセキュリティコンテキスト内でアクションを実行する可能性があります。
推奨事項
クロスオリジンリソース共有(CORS)に関連するセキュリティリスクを軽減するために、サーバー側の制御を実装して、リクエスト元クライアントのオリジンに基づいてリソースへのアクセスを制限することができます。
const express = require('express');
const cors = require('cors');
const app = express();
// Define a whitelist of allowed origins
const whitelist = ['http://example1.com', 'http://example2.com'];
// Configure CORS options
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true); // Allow request
} else {
callback(new Error('Not allowed by CORS')); // Deny request
}
}
};
// Apply CORS middleware with configured options
app.use(cors(corsOptions));
// Define routes
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
# 許可されるオリジンのリストを定義する
allowed_origins = [
'http://example.com',
'https://example.com',
# 必要に応じてオリジンを追加する
]
# ホワイトリストを使用してCORSを初期化する
CORS(app, origins=allowed_origins)
# ルートを定義する
@app.route('/')
def hello_world():
return jsonify(message='Hello World!')
if __name__ == '__main__':
app.run(debug=True)
<?php
// Enable CORS
header('Access-Control-Allow-Origin: http://example.com');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');
// Handle CORS pre-flight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');
exit;
}
// Define your API logic
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo json_encode(array('message' => 'Hello World!'));
} else {
http_response_code(405); // Method Not Allowed
}
?>
リンク
基準
- PCI_STANDARDS:
- REQ_2_2
- REQ_6_2
- REQ_6_3
- REQ_7_3
- REQ_8_3
- REQ_11_3
- HIPAA_CONTROLS:
- SECURITY221
- SECURITY212
- SECURITY213