Skip to content

Commit 20de6f6

Browse files
author
Taois
committed
feat: 修复安全性问题
1 parent b6e218a commit 20de6f6

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

apps/image-manager/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,53 @@ <h2>📋 图片列表</h2>
383383
}
384384
}
385385

386+
// 获取图片信息
387+
async function getImageInfo() {
388+
const imageId = document.getElementById('getImageId').value.trim();
389+
390+
if (!imageId) {
391+
showResult('getResult', '请输入图片ID', 'error');
392+
return;
393+
}
394+
395+
try {
396+
showLoading('getResult');
397+
398+
// 通过图片列表API获取特定图片信息
399+
const response = await fetch('/image/list');
400+
const result = await response.json();
401+
402+
if (result.success) {
403+
const targetImage = result.data.images.find(img => img.imageId === imageId);
404+
405+
if (targetImage) {
406+
showResult('getResult',
407+
`图片信息:\n` +
408+
`ID: ${targetImage.imageId}\n` +
409+
`大小: ${(targetImage.size / 1024).toFixed(2)} KB\n` +
410+
`MIME类型: ${targetImage.mimeType}\n` +
411+
`上传时间: ${new Date(targetImage.timestamp).toLocaleString()}\n` +
412+
`访问URL: ${targetImage.imageUrl}`,
413+
'success'
414+
);
415+
416+
// 显示图片预览
417+
document.getElementById('getPreview').innerHTML =
418+
`<img src="${targetImage.imageUrl}" alt="图片: ${imageId}">`;
419+
} else {
420+
showResult('getResult', `未找到图片ID为 "${imageId}" 的图片`, 'error');
421+
document.getElementById('getPreview').innerHTML = '';
422+
}
423+
} else {
424+
showResult('getResult', `获取图片信息失败: ${result.message}`, 'error');
425+
}
426+
427+
} catch (error) {
428+
showResult('getResult', `获取图片信息失败: ${error.message}`, 'error');
429+
document.getElementById('getPreview').innerHTML = '';
430+
}
431+
}
432+
386433
// 获取图片列表
387434
async function getImageList() {
388435
try {

controllers/image-store.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {imageManager} from '../utils/imageManager.js'
2+
import {validateBasicAuth} from '../utils/api_validate.js'
23

34
// Fastify插件导出
45
export default (fastify, options, done) => {
56
fastify.post('/image/upload', {
7+
preHandler: validateBasicAuth,
68
schema: {
79
body: {
810
type: 'object',
@@ -119,6 +121,7 @@ export default (fastify, options, done) => {
119121

120122
// 删除图片 - DELETE /image/:imageId
121123
fastify.delete('/image/:imageId', {
124+
preHandler: validateBasicAuth,
122125
schema: {
123126
params: {
124127
type: 'object',
@@ -172,6 +175,7 @@ export default (fastify, options, done) => {
172175

173176
// 清理过期图片 - POST /image/cleanup
174177
fastify.post('/image/cleanup', {
178+
preHandler: validateBasicAuth,
175179
schema: {
176180
body: {
177181
type: 'object',
@@ -204,6 +208,7 @@ export default (fastify, options, done) => {
204208

205209
// 更新图片 - PUT /image/:imageId
206210
fastify.put('/image/:imageId', {
211+
preHandler: validateBasicAuth,
207212
schema: {
208213
params: {
209214
type: 'object',

docs/updateRecord.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
7. 修复 `番茄小说` 宝盒因为缺少返回章节标题导致的不可看正文问题
1414
8. 设置中心增加百度扫码
1515
9. 新增图片储存插件(内存版,兼容vercel)
16+
10. 紧急撤包,修复图片接口的安全性问题
1617

1718
### 20250913
1819

libs/drpyS.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ initializeGlobalDollar();
5959
const {Ali, Baidu, Baidu2, Cloud, Pan, Quark, UC, Yun} = PanS;
6060
const {
6161
sleep, sleepSync, getNowTime, computeHash, deepCopy,
62-
urljoin, urljoin2, joinUrl, keysToLowerCase, naturalSort, $js
62+
urljoin, urljoin2, joinUrl, keysToLowerCase, naturalSort, $js,
63+
createBasicAuthHeaders
6364
} = utils;
6465
// 缓存已初始化的模块和文件 hash 值
6566
const moduleCache = new Map();
@@ -156,6 +157,7 @@ export async function getSandbox(env = {}) {
156157
joinUrl,
157158
naturalSort,
158159
$js,
160+
createBasicAuthHeaders,
159161
$,
160162
pupWebview,
161163
getProxyUrl,

spider/js/设置中心.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,17 +1082,21 @@ var rule = {
10821082
log('[百度扫码] 图片上传接口 imageUploadUrl:', imageUploadUrl);
10831083
// log('[百度扫码] qrCode:', qrCode);
10841084
try {
1085+
const basicHeader = createBasicAuthHeaders();
10851086
const baiduQrcode = (await axios({
10861087
url: httpUrl,
10871088
method: "POST",
10881089
data: {
10891090
url: imageUploadUrl,
10901091
method: "POST",
1092+
headers: {
1093+
...basicHeader
1094+
},
10911095
data: {imageId: 'baiduQrcode', base64Data: qrCode}
10921096
},
10931097
})).data.data;
10941098
log('[百度扫码] baiduQrcode:', baiduQrcode);
1095-
qrcodeUrl = requestHost + baiduQrcode.data.imageUrl;
1099+
qrcodeUrl = requestHost + baiduQrcode.data.imageUrl + `?t=${requestId}`;
10961100
log('[百度扫码] ds代理 qrcodeUrl:', qrcodeUrl);
10971101
} catch (e) {
10981102
log('[百度扫码] error:', e.message);
@@ -1113,9 +1117,11 @@ var rule = {
11131117
msg: '请使用百度APP扫码登录获取',
11141118
width: 500,
11151119
button: 1,
1116-
timeout: 20,
1117-
qrcode: qrcodeUrl,
1118-
isQrcode: 1, // 告诉壳子我已经是二维码了,你不要再去生成了(图片链接或者base64文本)!!!
1120+
timeout: 30,
1121+
imageUrl: qrcodeUrl,
1122+
imageHeight: 400,
1123+
// qrcode: qrcodeUrl,
1124+
// isQrcode: 1, // 告诉壳子我已经是二维码了,你不要再去生成了(图片链接或者base64文本)!!!
11191125
qrcodeSize: '400',
11201126
initAction: 'baiduScanCheck',
11211127
initValue: requestId,

utils/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ export function naturalSortAny(arr, key, customOrder = []) {
224224
});
225225
}
226226

227+
/**
228+
* 构造Basic验证请求头
229+
* @param {string} username - 用户名,如果不提供则从环境变量API_AUTH_NAME获取
230+
* @param {string} password - 密码,如果不提供则从环境变量API_AUTH_CODE获取
231+
* @returns {Object} - 包含Authorization头的对象
232+
*/
233+
export function createBasicAuthHeaders(username, password) {
234+
const authName = username || process.env.API_AUTH_NAME;
235+
const authCode = password || process.env.API_AUTH_CODE;
236+
237+
// if (!authName || !authCode) {
238+
// throw new Error('Basic认证信息不完整,请检查用户名和密码或环境变量API_AUTH_NAME和API_AUTH_CODE');
239+
// }
240+
241+
const credentials = Buffer.from(`${authName}:${authCode}`).toString('base64');
242+
243+
return {
244+
'Authorization': `Basic ${credentials}`
245+
};
246+
}
247+
227248
export const $js = {
228249
toString(func) {
229250
let strfun = func.toString();

0 commit comments

Comments
 (0)