T09 · Insecure Skill Coding Practices
- Location
scripts/fetch-news.js:21- Finding
Unrestricted Redirect Following Enables SSRF and Resource Exhaustion
- Content
View full analysis
{ const mod = url.startsWith('https') ? https : http; mod.get(url, { headers: { 'User-Agent': 'Mozilla/5.0' }, timeout: 10000 }, (res) => { if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { return fetch(res.headers.location).then(resolve).catch(reject); } let data = ''; res.on('data', c => data += c); res.on('end', () => resolve(data)); res.on('error', reject); }).on('error', reject); }); } ``` ### Technical Analysis The `fetch` function recursively follows every HTTP redirect without validating the destination protocol, hostname, resolved IP address, or redirect count. Although the initial feed URLs are hardcoded, any configured feed server can return an attacker-controlled `Location` header. Consequently, a compromised or malicious feed server can redirect the process to loopback, link-local, or private-network services. It can also redirect an HTTPS request to plaintext HTTP. Recursive redirects have no maximum depth, allowing redirect loops to consume resources. The response is accumulated into the `data` string without a maximum body size. A server can therefore return a very large or indefinitely streamed body and cause excessive memory consumption. The request timeout does not replace explicit body-size, redirect-count, and total-operation limits. ### Attack Path 1. An attacker compromises one of the configured RSS servers or otherwise gains control over its HTTP response. 2. The server returns a `3xx` response with a `Location` header pointing to an internal service, such as a loopback or private-network HTTP endpoint. 3. The script recursively calls `fet ...[truncated 1237 chars]- Remediation
View remediation
MAX_REDIRECTS) { throw new Error('Redirect limit exceeded'); } const parsed = new URL(url); if (parsed.protocol !== 'https:') { throw new Error('Only HTTPS feed URLs are allowed'); } if (!ALLOWED_FEED_HOSTS.has(parsed.hostname)) { throw new Error('Feed hostname is not allowed'); } // Resolve and reject private, loopback, and link-local addresses here. // Abort if the body exceeds MAX_RESPONSE_BYTES. // Revalidate every redirect before following it. } ``` ]]>
