T09 · Insecure Skill Coding Practices
- Location
scripts/train_query.js:20- Finding
Disabled TLS Certificate Validation and Unrestricted Redirect Handling
- Content
View full analysis
{ let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => { const stations = {}; const regex = /@[^|]+\|([^|]+)\|([^|]+)\|/g; let match; while ((match = regex.exec(data)) !== null) { stations[match[1]] = match[2]; } resolve(stations); }); }).on('error', reject); ``` ```js const options = { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36', 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Referer': 'https://www.12306.cn/mormhweb/', 'Cookie': 'JSESSIONID=test123; BIGipServerotn=123456' }, rejectUnauthorized: false }; function fetch(url, opts, callback) { const client = url.startsWith('https') ? https : http; client.get(url, opts, (res) => { if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { const newUrl = new URL(res.headers.location, url).toString(); fetch(newUrl, opts, callback); } else { let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => callback(data)); } }).on('error', e => { console.log('Fetch Error:', e.message); resolve([]); }); } ``` ### Technical Analysis The script explicitly sets `rejectUnauthorized: false` for both station-data retrieval and ticket queries. This disables standard TLS certificate-chain and hostname verification. Consequently, the script will accept expired, self-signed, incorrectly scoped, or attac ...[truncated 2928 chars]- Remediation
View remediation
{ // Process the response. }); ``` 2. Permit redirects only when the destination uses HTTPS and belongs to an explicit trusted-host allowlist, such as `kyfw.12306.cn`. 3. Reject HTTPS-to-HTTP redirects: ```js const nextUrl = new URL(res.headers.location, url); if (nextUrl.protocol !== 'https:') { return reject(new Error('Insecure redirect blocked')); } if (nextUrl.hostname !== 'kyfw.12306.cn') { return reject(new Error('Redirect to an untrusted host blocked')); } ``` 4. Do not forward cookies, authorization headers, referer values, or other origin-specific headers when the destination origin changes. 5. Remove the hard-coded `Cookie` header unless the endpoint demonstrably requires it. If authentication is later required, obtain session values securely and never embed valid credentials in source code. 6. Add a strict redirect limit, such as three redirects, to prevent redirect loops and recursion-based resource exhaustion. 7. Enforce maximum response sizes and request timeouts before buffering complete remote responses in memory. 8. Validate HTTP status codes and expected response content types before parsing data. 9. Treat remote station and ticket data as untrusted input and verify the expected schema before displaying it. ]]>
