T09 · Insecure Skill Coding Practices
- Location
scripts/server.js:11- Finding
Unauthenticated Network-Wide Access to Memo Data and Administrative Operations
- Content
View full analysis
{ res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type'); if (req.method === 'OPTIONS') return res.sendStatus(200); next(); }); ``` All memo operations are exposed without an authentication or authorization middleware: ```js app.get('/api/memos', (req, res) => { const { page, pageSize, search, tag } = req.query; const result = listMemos({ page: parseInt(page) || 1, pageSize: Math.min(parseInt(pageSize) || 20, 100), search, tag }); res.json({ ok: true, ...result }); }); app.get('/api/memos/:id', (req, res) => { const memo = getMemo(parseInt(req.params.id)); if (!memo) return res.status(404).json({ ok: false, error: '没找到这条备忘录' }); res.json({ ok: true, memo }); }); app.post('/api/memos', (req, res) => { const { title, content, tags } = req.body; if (!content && !title) { return res.status(400).json({ ok: false, error: '标题或内容至少要有一个' }); } const memo = createMemo({ title, content, tags }); res.status(201).json({ ok: true, memo }); }); app.put('/api/memos/:id', (req, res) => { const { title, content, tags, pinned } = req.body; const memo = updateMemo(parseInt(req.params.id), { title, content, tags, pinned }); if (!memo) return res.status(404).json({ ok: false, error: '没找到这条备忘录' }); res.json({ ok: true, memo }); }); app.delete('/api/memos/:id', (req, res) => { const ok = deleteMemo(parseInt(req.params.id)); if (!ok) return res.status(404).json({ ok: false, error: '没找到这条备忘录' }); res.json({ ok: true }); }); ``` The service is bound to all availa ...[truncated 2317 chars]- Remediation
View remediation
{ console.log(`Memo service started at http://127.0.0.1:${PORT}`); }); ``` 2. Require authentication before every memo API endpoint. For deployments involving multiple users, enforce authorization at the record level rather than merely checking whether a user is logged in. 3. Replace wildcard CORS with an explicit allowlist. If the Web UI is served from the same origin, disable CORS entirely unless there is a demonstrated cross-origin requirement. ```js const allowedOrigins = new Set(['http://127.0.0.1:3377']); app.use((req, res, next) => { const origin = req.get('Origin'); if (origin && allowedOrigins.has(origin)) { res.setHeader('Access-Control-Allow-Origin', origin); res.setHeader('Vary', 'Origin'); } next(); }); ``` 4. Reject disallowed preflight origins instead of returning success unconditionally. 5. If cookie-based authentication is introduced, use `HttpOnly`, `Secure`, and appropriate `SameSite` cookie attributes and add CSRF protection for state-changing requests. 6. Place the service behind a firewall or authenticated reverse proxy as defense in depth. Do not rely on a reverse proxy alone while leaving port 3377 publicly reachable. 7. Document clearly whether the application is local-only or network-accessible and provide secure deployment defaults matching that model. ]]>
