fix(restore): accept originals paths in group photos

- validate group_photos.photo_original_path with the same tolerant helper
  as entries, so /uploads/.originals/<file> no longer fails restore with
  "Invalid upload path"
- accept legacy flat values in both photo_original_path columns
- preserve users.avatar in normalizeRestoreData and the restore INSERT
- add selftests for originals paths and avatars
This commit is contained in:
dev
2026-10-21 18:36:01 +03:00
parent 697aac9b87
commit 3439d57485
3 changed files with 26 additions and 10 deletions
+3 -8
View File
@@ -151,12 +151,6 @@ function optUploadPath(v, max) {
const ORIGINALS_PATH_RE = /^\/uploads\/\.originals\/[\w.,()-]+$/;
function optOriginalsPath(v, max) {
if (v === null || v === undefined) return null;
if (typeof v !== 'string' || v.length > max || !ORIGINALS_PATH_RE.test(v)) throw new Error('Invalid originals path');
return v;
}
function reqPhotoRefPath(v, max) {
if (typeof v !== 'string' || v.length > max) throw new Error('Invalid photo path');
if (isSafeUploadPath(v) || ORIGINALS_PATH_RE.test(v)) return v;
@@ -333,7 +327,7 @@ function normalizeRestoreData(data) {
ai_checked_at: optTs(x.ai_checked_at),
ai_error: optAiText(x.ai_error, 500),
photo_path: optUploadPath(x.photo_path, 255),
photo_original_path: optOriginalsPath(x.photo_original_path, 255),
photo_original_path: optPhotoRefPath(x.photo_original_path, 255),
deleted_at: optTs(x.deleted_at),
purge_at: optTs(x.purge_at),
created_at: optTs(x.created_at),
@@ -359,6 +353,7 @@ function normalizeRestoreData(data) {
username: reqStr(x.username, 100),
password_hash: reqStr(x.password_hash, 255),
name: optStr(x.name, 150),
avatar: optUploadPath(x.avatar, 255),
role: (x.role === 'admin' || x.role === 'tutor') ? x.role : 'tutor',
is_active: !!x.is_active,
created_at: optTs(x.created_at),
@@ -397,7 +392,7 @@ function normalizeRestoreData(data) {
id: reqInt(x.id),
group_id: reqInt(x.group_id),
photo_path: reqUploadPath(x.photo_path, 255),
photo_original_path: optUploadPath(x.photo_original_path, 255),
photo_original_path: optPhotoRefPath(x.photo_original_path, 255),
caption: optStr(x.caption, 10000),
taken_at: optDate(x.taken_at),
sort_order: optInt(x.sort_order, -2147483648, 2147483647),
+21
View File
@@ -58,6 +58,27 @@ ok('чужой префикс отклоняется', !isSafeUploadPath('/etc/p
const base = { version: BACKUP_FORMAT_VERSION, groups: [], entries: [], users: [], branches: [] };
const originals = normalizeRestoreData({
...base,
entries: [{ id: 1, student_name: 'S', group_id: 1, description: 'd', photo_path: '/uploads/e.jpg', photo_original_path: '/uploads/.originals/e.jpg' }],
group_photos: [{ id: 1, group_id: 1, photo_path: '/uploads/g.jpg', photo_original_path: '/uploads/.originals/g.jpg' }],
});
ok('entries.photo_original_path принимает .originals',
originals.entries[0].photo_original_path === '/uploads/.originals/e.jpg', originals.entries[0]);
ok('group_photos.photo_original_path принимает .originals',
originals.group_photos[0].photo_original_path === '/uploads/.originals/g.jpg', originals.group_photos[0]);
const legacyOriginals = normalizeRestoreData({
...base,
group_photos: [{ id: 1, group_id: 1, photo_path: '/uploads/g.jpg', photo_original_path: '/uploads/g-orig.jpg' }],
});
ok('group_photos.photo_original_path принимает старый плоский путь',
legacyOriginals.group_photos[0].photo_original_path === '/uploads/g-orig.jpg', legacyOriginals.group_photos[0]);
const userAvatar = normalizeRestoreData({
...base,
users: [{ id: 1, username: 'u', password_hash: 'h', avatar: '/uploads/avatar.jpg' }],
});
ok('users.avatar сохраняется', userAvatar.users[0].avatar === '/uploads/avatar.jpg', userAvatar.users[0]);
const n = normalizeRestoreData({
...base,
groups: [{ id: 1, name: 'G', deleted_at: '2026-01-02T03:04:05.000Z', purge_at: '2026-02-03T04:05:06.000Z' }],
+2 -2
View File
@@ -6246,8 +6246,8 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
}
for (const x of ndata.users) {
await client.query(
'INSERT INTO users (id, username, password_hash, name, role, is_active, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)',
[x.id, x.username, x.password_hash, x.name, x.role, x.is_active, x.created_at]
'INSERT INTO users (id, username, password_hash, name, avatar, role, is_active, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
[x.id, x.username, x.password_hash, x.name, x.avatar, x.role, x.is_active, x.created_at]
);
}
for (const x of (ndata.user_settings || [])) {