fix: viewer-server 路径遍历防护 + localhost 认证#4
Open
alloevil wants to merge 2 commits into
Open
Conversation
added 2 commits
July 8, 2026 11:16
Security fixes: - Path traversal: getGroupDir() now validates resolved path stays within OUTPUT_DIR, throws on directory escape attempts - Auth token: auto-generated on first start, stored in .viewer-token file - Required for: /api/sync (POST), /api/browser-login (POST), /api/ai-config (POST) - Not required for: GET endpoints (messages, dates, groups, summary) - Token injected into HTML as window.__AUTH_TOKEN for frontend use - All write/sensitive operations now require Authorization: Bearer <token>
Extra after respondUnauthorized function caused node --check to fail. Caught by CI syntax-check step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
1. 路径遍历
/api/messages?group=xxx和/api/dates?group=xxx的 group 参数直接拼接到文件路径,虽然过滤了部分字符,但未校验最终路径是否在 OUTPUT_DIR 内。2. 无认证
/api/sync(触发归档)、/api/browser-login(打开浏览器扫码)、/api/ai-config(写入 AI 配置)这三个敏感端点无任何认证。任何访问 localhost:3456 的人都能触发 Cookie 暴露。修复
路径校验
getGroupDir()现在用path.resolve()校验最终路径是否在OUTPUT_DIR内localhost 认证
.viewer-token文件Authorization: Bearer <token>window.__AUTH_TOKEN,前端可用前端适配
前端 fetch 请求需在敏感操作时携带 token: