import http from 'k6/http'; import { check } from 'k6'; export const options = { thresholds: { checks: ['rate==1.0'], // 所有安全检查必须通过 }, }; const BASE_URL = __ENV.BASE_URL || 'http://localhost:3000'; const xssPayloads = [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'X', 'javascript:alert("XSS")', '', '', '', '', '', ]; export default function () { let allPassed = true; xssPayloads.forEach((payload) => { const testCases = [ { name: 'Contact Form - XSS', url: `${BASE_URL}/api/contact`, method: 'POST', body: JSON.stringify({ name: payload, email: 'test@example.com', phone: '13800138000', message: payload, }), }, { name: 'Search - XSS', url: `${BASE_URL}/api/search?q=${encodeURIComponent(payload)}`, method: 'GET', }, ]; testCases.forEach((testCase) => { let res; if (testCase.method === 'POST') { res = http.post(testCase.url, testCase.body, { headers: { 'Content-Type': 'application/json' }, tags: { name: testCase.name }, }); } else { res = http.get(testCase.url, { tags: { name: testCase.name }, }); } const passed = check(res, { 'status is 200 or 400 or 422': (r) => [200, 400, 422].includes(r.status), 'XSS payload not reflected in response': (r) => { const lowerBody = r.body.toLowerCase(); return !lowerBody.includes('