Postman Agent Mode入門シリーズ
- Agent Modeを使いはじめる
- OWASP APIセキュリティ準拠テストの追加 (本記事)
前回の記事に続き、Postman Agent Mode入門の第2弾です。これ、シリーズ化していこうと思います。
前回、Postman Agent Mode (以下、Agent Mode)でざっくりとレスポンスを検証するテストを追加してみましたが、こんどはAPIセキュリティ対応のためのテストを追加してみます。
APIセキュリティといえばOWASPが公開するAPIセキュリティTop10 (リンクは2023年度版)が有名かとおもいますが、今回はAgent Modeを活用して、このOWASP APIセキュリティのベストプラクティスに準拠した検証を行うテストを追加します。
OWASP APIセキュリティ準拠テストの追加
まずは、Agent Modeアイコンをクリックしてチャット窓をオープンしてから、こちらで作成したポケモンAPIコレクションをコンテキストとして選択します。
- Agent Modeアイコンをクリック
- ポケモンAPIコレクションをコンテキストとして選択
- 方法1: ポケモンAPIコレクションを選択
- 方法2: ポケモンAPIコレクションをチャット窓にドラッグ&ドロップ
下図のようにチャット窓のところにポケモンAPIコレクション名が追加されていればコンテキストとして選択されていることを意味します。
次に、下記のプロンプトを入力します。
このコレクションの各APIリクエストに対して、セキュリティの網羅性を高める目的で、
OWASP APIセキュリティのベストプラクティスに準拠した検証を行うテストを追加してください
上記プロンプト入力後、下図のように追加していくセキュリティ検証の実装一覧が表示され、検証用のコードが作成されていきます。
なお、Debug Logをみると内部的に次のような処理が実行されていることがわかります。
- テストスクリプト作成用のサブエージェントの選択
- サブエージェントにOWASP APIセキュリティベストプラクティス準拠のテストを追加する指示
- サブエージェントに追加対象のAPIリクエスト一覧を共有
- サブエージェントがテストスクリプト追加のための実行プラン立案&実行
興味がある次の一行をクリックしてDebug Logを覗いてみてください:
Postman AIエージェントDebug Log (クリックで展開)
Selecting sub-agent
Script-and-Test-Generator
Instructions to sub-agent
Add comprehensive OWASP API Security best practices tests to all requests in the PokéAPI Collection (Postman UID: 27917608-1e8940ce-08d4-4bdd-9caf-a75ed0cbef6b).
For each request in the collection, add post-response test scripts that verify:
-
Secure Communication (OWASP API1:2023 - Broken Object Level Authorization)
- Verify HTTPS is used (check URL starts with https://)
- Check for secure headers if applicable
-
Authentication & Authorization (OWASP API2:2023 - Broken Authentication)
- Test that unauthorized requests are handled properly
- Verify proper status codes for auth failures
-
Rate Limiting (OWASP API4:2023 - Unrestricted Resource Consumption)
- Check for rate limit headers (X-RateLimit-Limit, X-RateLimit-Remaining, Retry-After)
- Verify response time is reasonable (< 2000ms for public APIs)
-
Input Validation (OWASP API3:2023 - Broken Object Property Level Authorization)
- Verify proper error handling for invalid inputs
- Check that error messages don't expose sensitive information
-
Data Exposure (OWASP API3:2023 - Excessive Data Exposure)
- Verify response doesn't contain excessive or sensitive data
- Check response structure is as expected
-
Error Handling (OWASP API7:2023 - Server Side Request Forgery)
- Verify error responses don't leak stack traces or internal details
- Check for proper error status codes
-
Response Validation
- Verify Content-Type header is present and correct
- Check response status is in acceptable range (200-299 for success)
- Validate response schema structure
Add these tests to all 7 requests in the collection. Make the tests comprehensive but appropriate for a public API like PokéAPI. Include clear test descriptions in Japanese for better understanding.
The task is complete when all requests in the collection have comprehensive OWASP-compliant security tests added to their post-response scripts.. I know the summary of what you're going to do. Just call a tool or ask a clarifying question.
Data for sub-agent
Collection to update: PokéAPI Collection (Postman UID: 27917608-1e8940ce-08d4-4bdd-9caf-a75ed0cbef6b)
Requests in the collection:
- Get Pokemon by Name (27917608-8038f34d-fbbe-4eab-a144-c6f3522ffbd6) - GET https://pokeapi.co/api/v2/pokemon/pikachu
- Get Pokemon by ID (27917608-1dedd7cf-fa4d-4abb-8edc-be57273f17bd) - GET https://pokeapi.co/api/v2/pokemon/25
- List Pokemon (27917608-8d35b23d-8318-4f67-97eb-dc7c575fe7bc) - GET https://pokeapi.co/api/v2/pokemon?limit=20&offset=0
- Get Pokemon Species (27917608-02032580-85f4-4e59-b9f6-440ac1a6e84b) - GET https://pokeapi.co/api/v2/pokemon-species/pikachu
- Get Type (27917608-8bc7d1f6-6d68-41d7-9a66-4e9e2aa0b2b3) - GET https://pokeapi.co/api/v2/type/electric
- Get Ability (27917608-3101e82b-d1d0-421e-b080-74fa3af86662) - GET https://pokeapi.co/api/v2/ability/static
- Get Move (27917608-35ea5021-8057-4257-8461-99fae9a1dff9) - GET https://pokeapi.co/api/v2/move/thunderbolt
Plan
- Analyze each request in the PokéAPI Collection
- Create comprehensive OWASP API Security test scripts for each request covering:
- Secure communication (HTTPS verification)
- Rate limiting checks
- Input validation
- Data exposure prevention
- Error handling validation
- Response validation
- Add the security test scripts to all 7 requests in the collection
- Verify all tests are properly added with clear Japanese descriptions
Relevant terms
COLLECTION_ADVANCED, SPECIFICATION, SPECIFICATION_VALIDATION, API_GOVERNANCE
Tool search queries
add test scripts to HTTP requests, modify post-response scripts for security testing, update request test scripts
Matched tools
updateTests, saveOpenRequest, sendRequest, updateScripts, addNewRequestToCollection, updateExampleOfRequest, createNewRequest, addNewExampleToRequest, editHTTPRequestTab, updateVariables, updateSpecificationContent, executeShellCommand, createMonitor, listCollections, listEnvironments, getListOfRunners, addVisualization, modifyActiveMCPRequest
テストスクリプトが作成されたら、スクリプトを追加するAPIリクエストごとに次のようにApprovalを確認します。
なお、ここではAgent ModeのAuto-run設定をオフにしているので、このようにスクリプト追加前(データ変更操作の前)にApprovalを確認されますが、Approvalをスキップして自動で追加したい場合はAuto-run設定をオンにしてください。
全ての追加をApproveすると、実際に全APIリクエストにテストスクリプトが追加され、次のような追加されたセキュリティテストのサマリーと次の推奨ステップが出力されます。
実際に追加されたスクリプトについて例を上げると、Get Pokemon by Name リクエストには次のスクリプトが追加されました。
// OWASP API Security Best Practices Tests
// 1. セキュアな通信 (OWASP API1:2023 - Broken Object Level Authorization)
pm.test("HTTPS通信が使用されている", function () {
pm.expect(pm.request.url.protocol).to.equal("https");
});
pm.test("セキュアなプロトコルでリクエストが送信されている", function () {
const url = pm.request.url.toString();
pm.expect(url).to.match(/^https:\/\//);
});
// 2. 認証と認可 (OWASP API2:2023 - Broken Authentication)
pm.test("適切なHTTPステータスコードが返される", function () {
pm.expect(pm.response.code).to.be.oneOf([200, 201, 204, 400, 401, 403, 404, 429, 500, 503]);
});
pm.test("認証エラー時は適切なステータスコードを返す", function () {
if (pm.response.code === 401 || pm.response.code === 403) {
pm.expect(pm.response.code).to.be.oneOf([401, 403]);
}
});
// 3. レート制限 (OWASP API4:2023 - Unrestricted Resource Consumption)
pm.test("レスポンス時間が妥当な範囲内(2秒以内)", function () {
pm.expect(pm.response.responseTime).to.be.below(2000);
});
pm.test("レート制限ヘッダーの確認(存在する場合)", function () {
const rateLimitHeaders = ['X-RateLimit-Limit', 'X-RateLimit-Remaining', 'Retry-After', 'RateLimit-Limit', 'RateLimit-Remaining'];
const hasRateLimitHeader = rateLimitHeaders.some(header => pm.response.headers.has(header));
if (hasRateLimitHeader) {
console.log("レート制限ヘッダーが検出されました");
}
});
// 4. 入力検証 (OWASP API3:2023 - Broken Object Property Level Authorization)
pm.test("エラーレスポンスが機密情報を漏洩していない", function () {
if (pm.response.code >= 400) {
const responseBody = pm.response.text().toLowerCase();
pm.expect(responseBody).to.not.include("stack trace");
pm.expect(responseBody).to.not.include("exception");
pm.expect(responseBody).to.not.include("sql");
pm.expect(responseBody).to.not.include("database");
pm.expect(responseBody).to.not.include("password");
pm.expect(responseBody).to.not.include("secret");
}
});
// 5. データ露出 (OWASP API3:2023 - Excessive Data Exposure)
pm.test("レスポンスが期待される構造を持っている", function () {
if (pm.response.code === 200) {
const jsonData = pm.response.json();
pm.expect(jsonData).to.be.an('object');
pm.expect(jsonData).to.have.property('name');
pm.expect(jsonData).to.have.property('id');
}
});
pm.test("レスポンスに機密データが含まれていない", function () {
const responseBody = pm.response.text().toLowerCase();
pm.expect(responseBody).to.not.include("password");
pm.expect(responseBody).to.not.include("secret");
pm.expect(responseBody).to.not.include("api_key");
pm.expect(responseBody).to.not.include("private_key");
pm.expect(responseBody).to.not.include("token");
});
// 6. エラーハンドリング (OWASP API7:2023 - Server Side Request Forgery)
pm.test("エラーレスポンスが内部詳細を漏洩していない", function () {
if (pm.response.code >= 500) {
const responseBody = pm.response.text().toLowerCase();
pm.expect(responseBody).to.not.include("internal server");
pm.expect(responseBody).to.not.include("debug");
pm.expect(responseBody).to.not.include("trace");
}
});
pm.test("適切なエラーステータスコードが使用されている", function () {
if (pm.response.code >= 400) {
pm.expect(pm.response.code).to.be.oneOf([400, 401, 403, 404, 405, 429, 500, 502, 503, 504]);
}
});
// 7. レスポンス検証
pm.test("Content-Typeヘッダーが存在し正しい", function () {
pm.response.to.have.header("Content-Type");
if (pm.response.code === 200) {
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
}
});
なお、このAPIには肝心の認証・認可が無いため、肝心のその辺りのテストは含まれていません。
おわりに
Agent Modeを活用することで、APIセキュリティに関するテストを効率的に生成できることを確認できたかと思います。もちろん、テストの精度・品質・網羅性については人によるレビューが不可欠ですが、Agent Modeが初期セットを一気に作ってくれることで、その後の追加・修正作業が大幅に楽になります。
今回は「OWASP APIセキュリティに準拠したテストを追加してほしい」という大まかな指示だけでしたが、指示内容をより詳細にすることで、さらに網羅的で高品質なテストを作成することも可能です。Agent Modeをうまく活用しながら、テストプロセスの生産性と品質向上を両立させていけるはずです。


