Skip to content

Commit 4955ed1

Browse files
committed
v2.6.1:增加文件夹候选项;部分安全和体验优化
1 parent 50ff6d9 commit 4955ed1

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

functions/api/directoryTree.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getDirectoryTree } from '../utils/indexManager';
22
import { dualAuthCheck } from '../utils/dualAuth';
3+
import { fetchSecurityConfig } from '../utils/sysConfig';
34

45
/**
56
* 目录树 API 端点
@@ -11,6 +12,10 @@ import { dualAuthCheck } from '../utils/dualAuth';
1112
* 响应:
1213
* - 成功:{ tree: DirectoryTreeNode }
1314
* - 失败:{ error: string }
15+
*
16+
* 权限说明:
17+
* - 管理端鉴权成功:始终允许访问
18+
* - 用户端鉴权成功:仅当 showDirectorySuggestions 开启时允许访问
1419
*/
1520
export async function onRequestGet(context) {
1621
const { env, request } = context;
@@ -22,6 +27,19 @@ export async function onRequestGet(context) {
2227
return new Response('Unauthorized', { status: 401 });
2328
}
2429

30+
// 如果是用户端鉴权,检查 showDirectorySuggestions 设置
31+
if (authResult.authType === 'user') {
32+
const securityConfig = await fetchSecurityConfig(env);
33+
const showDirectorySuggestions = securityConfig?.upload?.showDirectorySuggestions ?? true;
34+
35+
if (!showDirectorySuggestions) {
36+
return new Response(JSON.stringify({ error: 'Directory suggestions disabled' }), {
37+
status: 403,
38+
headers: { 'Content-Type': 'application/json' }
39+
});
40+
}
41+
}
42+
2543
try {
2644
const tree = await getDirectoryTree(context);
2745
const cacheTime = url.searchParams.get('cacheTime') || 60;

functions/utils/dualAuth.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ import { validateApiToken } from './tokenValidator';
44
import { getDatabase } from './databaseAdapter.js';
55

66
/**
7-
* 双重鉴权检查:用户端或管理端任意一个通过即可
7+
* 双重鉴权检查:管理端或用户端任意一个通过即可
8+
* 注意:管理端鉴权优先检查,确保管理员权限优先级更高
89
* @param {Object} env - 环境变量
910
* @param {URL} url - 请求的URL
1011
* @param {Request} request - 请求对象
1112
* @returns {Promise<{authorized: boolean, authType: string|null}>}
1213
*/
1314
export async function dualAuthCheck(env, url, request) {
14-
// 1. 尝试用户端鉴权 (authCode / API Token)
15-
const userAuthPassed = await userAuthCheck(env, url, request, null);
16-
if (userAuthPassed) {
17-
return { authorized: true, authType: 'user' };
18-
}
19-
20-
// 2. 尝试管理端鉴权 (Basic Auth / API Token)
15+
// 1. 优先尝试管理端鉴权 (Basic Auth / API Token)
2116
const adminAuthPassed = await adminAuthCheck(env, request);
2217
if (adminAuthPassed) {
2318
return { authorized: true, authType: 'admin' };
2419
}
2520

21+
// 2. 尝试用户端鉴权 (authCode / API Token)
22+
const userAuthPassed = await userAuthCheck(env, url, request, null);
23+
if (userAuthPassed) {
24+
return { authorized: true, authType: 'user' };
25+
}
26+
2627
return { authorized: false, authType: null };
2728
}
2829

0 commit comments

Comments
 (0)