-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-security.js
More file actions
192 lines (172 loc) · 5.93 KB
/
Copy pathtest-security.js
File metadata and controls
192 lines (172 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* 安全测试脚本
* 用于验证 API 端点的认证和授权
*/
const API_BASE_URL = process.env.API_BASE_URL || 'https://writer-api.qwqc.cc';
// 颜色输出
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[36m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
async function testUnauthorizedUpload() {
log('\n=== 测试 1: 未认证的图片上传 (应该返回 401) ===', 'blue');
try {
const response = await fetch(`${API_BASE_URL}/api/upload`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
imageData: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
mimeType: 'image/png',
config: {
provider: 'custom',
endpoint: 'https://s3.amazonaws.com',
region: 'us-east-1',
accessKeyId: 'test',
secretAccessKey: 'test',
bucket: 'test',
publicUrl: 'https://example.com',
pathPrefix: '',
forcePathStyle: false
}
})
});
if (response.status === 401) {
log('✅ 通过: 未认证请求被正确拒绝', 'green');
return true;
} else {
log(`❌ 失败: 期望 401,收到 ${response.status}`, 'red');
return false;
}
} catch (error) {
log(`❌ 错误: ${error.message}`, 'red');
return false;
}
}
async function testUnauthorizedDelete() {
log('\n=== 测试 2: 未认证的图片删除 (应该返回 401) ===', 'blue');
try {
const response = await fetch(`${API_BASE_URL}/api/upload/delete`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
key: 'test-key',
config: {
provider: 'custom',
endpoint: 'https://s3.amazonaws.com',
region: 'us-east-1',
accessKeyId: 'test',
secretAccessKey: 'test',
bucket: 'test',
publicUrl: 'https://example.com',
pathPrefix: '',
forcePathStyle: false
}
})
});
if (response.status === 401) {
log('✅ 通过: 未认证请求被正确拒绝', 'green');
return true;
} else {
log(`❌ 失败: 期望 401,收到 ${response.status}`, 'red');
return false;
}
} catch (error) {
log(`❌ 错误: ${error.message}`, 'red');
return false;
}
}
async function testUnauthorizedPostsAccess() {
log('\n=== 测试 3: 未认证的文章访问 (应该返回 401) ===', 'blue');
try {
const response = await fetch(`${API_BASE_URL}/api/posts`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.status === 401) {
log('✅ 通过: 未认证请求被正确拒绝', 'green');
return true;
} else {
log(`❌ 失败: 期望 401,收到 ${response.status}`, 'red');
return false;
}
} catch (error) {
log(`❌ 错误: ${error.message}`, 'red');
return false;
}
}
async function testCorsOrigin() {
log('\n=== 测试 4: CORS 未允许来源 (应该返回 403 或无 CORS 头) ===', 'blue');
try {
const response = await fetch(`${API_BASE_URL}/api/posts`, {
method: 'OPTIONS',
headers: {
'Origin': 'https://malicious-site.com',
'Access-Control-Request-Method': 'POST'
}
});
const acao = response.headers.get('Access-Control-Allow-Origin');
if (!acao || acao === 'null' || response.status === 403) {
log('✅ 通过: 未允许来源被正确拒绝', 'green');
return true;
} else {
log(`❌ 失败: CORS 头泄露: ${acao}`, 'red');
return false;
}
} catch (error) {
log(`⚠️ 跳过: ${error.message}`, 'yellow');
return null;
}
}
async function testAuthEndpoint() {
log('\n=== 测试 5: OAuth 端点可用性 (应该返回 200) ===', 'blue');
try {
const response = await fetch(`${API_BASE_URL}/auth/github`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
if (data.url && data.url.includes('github.com')) {
log('✅ 通过: OAuth 端点正常工作', 'green');
return true;
}
}
log(`❌ 失败: OAuth 端点异常`, 'red');
return false;
} catch (error) {
log(`⚠️ 跳过: ${error.message}`, 'yellow');
return null;
}
}
async function main() {
log('╔══════════════════════════════════════════╗', 'blue');
log('║ Hexo Blog Manager 安全测试套件 ║', 'blue');
log('╚══════════════════════════════════════════╝', 'blue');
log(`API 地址: ${API_BASE_URL}`, 'yellow');
const results = [];
results.push(await testUnauthorizedUpload());
results.push(await testUnauthorizedDelete());
results.push(await testUnauthorizedPostsAccess());
results.push(await testCorsOrigin());
results.push(await testAuthEndpoint());
const passed = results.filter(r => r === true).length;
const failed = results.filter(r => r === false).length;
const skipped = results.filter(r => r === null).length;
log('\n═════════════════════════════════════════', 'blue');
log(`测试结果: ${passed} 通过, ${failed} 失败, ${skipped} 跳过`, 'blue');
log('═════════════════════════════════════════', 'blue');
if (failed === 0) {
log('🎉 所有安全测试通过!', 'green');
process.exit(0);
} else {
log('⚠️ 部分测试失败,请检查以上输出', 'red');
process.exit(1);
}
}
main().catch(console.error);