Secure image uploads and post publishing
This commit is contained in:
1 parent
145f13fee5
commit
5605ded575
3 files changed
+114
-32
No files matched your search
@@ -13,6 +13,7 @@ const { Pool } = require('pg');
|
||||
const multer = require('multer');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -564,12 +565,18 @@ app.post('/api/posts', auth, async (req, res) => {
|
||||
return res.status(400).json({ error: '请填写内容' });
|
||||
}
|
||||
|
||||
const imagePaths = images || [];
|
||||
const uploadPathPattern = /^\/uploads\/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.(?:jpg|png|gif|webp)$/i;
|
||||
if (!Array.isArray(imagePaths) || imagePaths.length > 6 || imagePaths.some(image => !uploadPathPattern.test(image))) {
|
||||
return res.status(400).json({ error: '图片地址无效或数量超过限制' });
|
||||
}
|
||||
|
||||
try {
|
||||
const r = await pool.query(
|
||||
`INSERT INTO posts (user_id, title, content, images)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *`,
|
||||
[req.user.id, title || '无标题', content, images || []]
|
||||
[req.user.id, title || '无标题', content, imagePaths]
|
||||
);
|
||||
res.json(r.rows[0]);
|
||||
} catch (err) {
|
||||
@@ -985,29 +992,51 @@ app.post('/api/event-logs', auth, async (req, res) => {
|
||||
// 图片上传
|
||||
// ============================================================
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => cb(null, 'uploads/'),
|
||||
filename: (req, file, cb) => {
|
||||
const ext = path.extname(file.originalname);
|
||||
const name = Date.now() + '-' + Math.round(Math.random() * 10000) + ext;
|
||||
cb(null, name);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
storage: multer.memoryStorage(),
|
||||
limits: { fileSize: 5 * 1024 * 1024 },
|
||||
fileFilter: (req, file, cb) => {
|
||||
const allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
cb(null, allowed.includes(file.mimetype));
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/upload', auth, upload.single('file'), (req, res) => {
|
||||
function detectImageType(buffer) {
|
||||
if (!buffer || buffer.length < 12) return null;
|
||||
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
||||
return { extension: '.jpg', mime: 'image/jpeg' };
|
||||
}
|
||||
if (buffer.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
|
||||
return { extension: '.png', mime: 'image/png' };
|
||||
}
|
||||
const header = buffer.subarray(0, 6).toString('ascii');
|
||||
if (header === 'GIF87a' || header === 'GIF89a') {
|
||||
return { extension: '.gif', mime: 'image/gif' };
|
||||
}
|
||||
if (buffer.subarray(0, 4).toString('ascii') === 'RIFF' && buffer.subarray(8, 12).toString('ascii') === 'WEBP') {
|
||||
return { extension: '.webp', mime: 'image/webp' };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
app.post('/api/upload', auth, upload.single('file'), async (req, res, next) => {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: '请选择图片' });
|
||||
}
|
||||
res.json({ url: '/uploads/' + req.file.filename });
|
||||
|
||||
const imageType = detectImageType(req.file.buffer);
|
||||
if (!imageType) {
|
||||
return res.status(400).json({ error: '仅支持有效的 JPG、PNG、GIF 或 WebP 图片' });
|
||||
}
|
||||
|
||||
const filename = crypto.randomUUID() + imageType.extension;
|
||||
try {
|
||||
await fs.promises.writeFile(path.join(__dirname, 'uploads', filename), req.file.buffer, { flag: 'wx' });
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
return res.status(201).json({
|
||||
url: '/uploads/' + filename,
|
||||
mime: imageType.mime,
|
||||
size: req.file.size,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
|
||||
Reference in new issue
Block a user