BlueGit

timfa filemanager

Add recursive chmod on folder list

When enabled through environment settings, opening or refreshing a folder now performs a best-effort recursive chmod before listing items. This helps folders uploaded via other systems remain accessible in the file manager without manual intervention.\n\nThe behaviour is covered with route tests that verify chmod is applied when enabled and unchanged when disabled.

timfa Tim Falken · pushed by timfa

Authored 2026-05-25 · 5 months ago

Hash
79ac5be9dbc5a0f4b6dc41ab17fb818e729cc850
Parent
99438df4
2 files changed +130 −0
server/routes/files.js +75 −0
@@ -184,6 +184,70 @@ const upload = multer({
184 184 limits: { fileSize: 10 * 1024 * 1024 * 1024 }, // 10 GB
185 185 });
186 186
187 function parseOctalMode (rawValue, fallback)
188 {
189 const trimmed = String(rawValue || '').trim();
190 if (!trimmed) return fallback;
191 if (!/^[0-7]{3,4}$/.test(trimmed)) return fallback;
192 const parsed = Number.parseInt(trimmed, 8);
193 if (!Number.isInteger(parsed) || parsed < 0 || parsed > 0o7777) return fallback;
194 return parsed;
195 }
196
197 function envFlagEnabled (name)
198 {
199 const value = String(process.env[name] || '').trim().toLowerCase();
200 return value === '1' || value === 'true' || value === 'yes' || value === 'on';
201 }
202
203 function applyRecursiveAccessChmod (startDir)
204 {
205 const dirMode = parseOctalMode(process.env.FILEMANAGER_LIST_ACCESS_CHMOD_DIR_MODE, 0o777);
206 const fileMode = parseOctalMode(process.env.FILEMANAGER_LIST_ACCESS_CHMOD_FILE_MODE, 0o666);
207 const queue = [path.resolve(startDir)];
208
209 while (queue.length)
210 {
211 const current = queue.pop();
212 if (!current) continue;
213
214 let stat;
215 try
216 {
217 stat = fs.lstatSync(current);
218 } catch
219 {
220 continue;
221 }
222
223 if (stat.isSymbolicLink()) continue;
224
225 try
226 {
227 fs.chmodSync(current, stat.isDirectory() ? dirMode : fileMode);
228 } catch
229 {
230 // Continue best-effort; listing should still work when possible.
231 }
232
233 if (!stat.isDirectory()) continue;
234
235 let entries = [];
236 try
237 {
238 entries = fs.readdirSync(current, { withFileTypes: true });
239 } catch
240 {
241 continue;
242 }
243
244 for (const entry of entries)
245 {
246 queue.push(path.join(current, entry.name));
247 }
248 }
249 }
250
187 251 // ---------------------------------------------------------------------------
188 252 // Helpers
189 253 // ---------------------------------------------------------------------------
@@ -1045,6 +1109,17 @@ router.get('/list', requireAuth, (req, res) =>
1045 1109 const info = resolveRootForUser(dir, roots, req.user, config);
1046 1110 if (!info || !canReadInfo(req.user, info)) return res.status(403).json({ error: 'Access denied.' });
1047 1111
1112 if (envFlagEnabled('FILEMANAGER_LIST_ACCESS_CHMOD'))
1113 {
1114 try
1115 {
1116 applyRecursiveAccessChmod(dir);
1117 } catch (err)
1118 {
1119 logError('Best-effort chmod pass failed before directory listing.', err, { dir: String(dir || '') });
1120 }
1121 }
1122
1048 1123 let entries;
1049 1124 try
1050 1125 {
@@ -184,6 +184,70 @@ const upload = multer({
184 limits: { fileSize: 10 * 1024 * 1024 * 1024 }, // 10 GB
184 limits: { fileSize: 10 * 1024 * 1024 * 1024 }, // 10 GB
185});
185});
186
186
187function parseOctalMode (rawValue, fallback)
188{
189 const trimmed = String(rawValue || '').trim();
190 if (!trimmed) return fallback;
191 if (!/^[0-7]{3,4}$/.test(trimmed)) return fallback;
192 const parsed = Number.parseInt(trimmed, 8);
193 if (!Number.isInteger(parsed) || parsed < 0 || parsed > 0o7777) return fallback;
194 return parsed;
195}
196
197function envFlagEnabled (name)
198{
199 const value = String(process.env[name] || '').trim().toLowerCase();
200 return value === '1' || value === 'true' || value === 'yes' || value === 'on';
201}
202
203function applyRecursiveAccessChmod (startDir)
204{
205 const dirMode = parseOctalMode(process.env.FILEMANAGER_LIST_ACCESS_CHMOD_DIR_MODE, 0o777);
206 const fileMode = parseOctalMode(process.env.FILEMANAGER_LIST_ACCESS_CHMOD_FILE_MODE, 0o666);
207 const queue = [path.resolve(startDir)];
208
209 while (queue.length)
210 {
211 const current = queue.pop();
212 if (!current) continue;
213
214 let stat;
215 try
216 {
217 stat = fs.lstatSync(current);
218 } catch
219 {
220 continue;
221 }
222
223 if (stat.isSymbolicLink()) continue;
224
225 try
226 {
227 fs.chmodSync(current, stat.isDirectory() ? dirMode : fileMode);
228 } catch
229 {
230 // Continue best-effort; listing should still work when possible.
231 }
232
233 if (!stat.isDirectory()) continue;
234
235 let entries = [];
236 try
237 {
238 entries = fs.readdirSync(current, { withFileTypes: true });
239 } catch
240 {
241 continue;
242 }
243
244 for (const entry of entries)
245 {
246 queue.push(path.join(current, entry.name));
247 }
248 }
249}
250
187// ---------------------------------------------------------------------------
251// ---------------------------------------------------------------------------
188// Helpers
252// Helpers
189// ---------------------------------------------------------------------------
253// ---------------------------------------------------------------------------
@@ -1045,6 +1109,17 @@ router.get('/list', requireAuth, (req, res) =>
1045 const info = resolveRootForUser(dir, roots, req.user, config);
1109 const info = resolveRootForUser(dir, roots, req.user, config);
1046 if (!info || !canReadInfo(req.user, info)) return res.status(403).json({ error: 'Access denied.' });
1110 if (!info || !canReadInfo(req.user, info)) return res.status(403).json({ error: 'Access denied.' });
1047
1111
1112 if (envFlagEnabled('FILEMANAGER_LIST_ACCESS_CHMOD'))
1113 {
1114 try
1115 {
1116 applyRecursiveAccessChmod(dir);
1117 } catch (err)
1118 {
1119 logError('Best-effort chmod pass failed before directory listing.', err, { dir: String(dir || '') });
1120 }
1121 }
1122
1048 let entries;
1123 let entries;
1049 try
1124 try
1050 {
1125 {
tests/files.routes.test.js +55 −0
@@ -170,6 +170,10 @@ afterAll(() =>
170 170
171 171 beforeEach(() =>
172 172 {
173 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD;
174 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD_DIR_MODE;
175 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD_FILE_MODE;
176
173 177 fs.rmSync(rootDir, { recursive: true, force: true });
174 178 fs.mkdirSync(rootDir, { recursive: true });
175 179 if (mockUserPath)
@@ -685,6 +689,57 @@ describe('orphaned user folder access', () =>
685 689 });
686 690 });
687 691
692 describe('list access recursive chmod', () =>
693 {
694 test('GET /api/files/list applies recursive chmod when enabled', async () =>
695 {
696 const nestedDir = path.join(rootDir, 'locked');
697 const nestedFile = path.join(nestedDir, 'data.txt');
698 fs.mkdirSync(nestedDir, { recursive: true });
699 fs.writeFileSync(nestedFile, 'hello');
700
701 fs.chmodSync(rootDir, 0o700);
702 fs.chmodSync(nestedDir, 0o700);
703 fs.chmodSync(nestedFile, 0o600);
704
705 process.env.FILEMANAGER_LIST_ACCESS_CHMOD = '1';
706
707 const listRes = await req('GET', `/api/files/list?dir=${encodeURIComponent(rootDir)}`);
708 expect(listRes.status).toBe(200);
709
710 const rootMode = fs.statSync(rootDir).mode & 0o777;
711 const dirMode = fs.statSync(nestedDir).mode & 0o777;
712 const fileMode = fs.statSync(nestedFile).mode & 0o777;
713
714 expect(rootMode).toBe(0o777);
715 expect(dirMode).toBe(0o777);
716 expect(fileMode).toBe(0o666);
717 });
718
719 test('GET /api/files/list leaves permissions unchanged when disabled', async () =>
720 {
721 const nestedDir = path.join(rootDir, 'locked');
722 const nestedFile = path.join(nestedDir, 'data.txt');
723 fs.mkdirSync(nestedDir, { recursive: true });
724 fs.writeFileSync(nestedFile, 'hello');
725
726 fs.chmodSync(rootDir, 0o700);
727 fs.chmodSync(nestedDir, 0o700);
728 fs.chmodSync(nestedFile, 0o600);
729
730 const listRes = await req('GET', `/api/files/list?dir=${encodeURIComponent(rootDir)}`);
731 expect(listRes.status).toBe(200);
732
733 const rootMode = fs.statSync(rootDir).mode & 0o777;
734 const dirMode = fs.statSync(nestedDir).mode & 0o777;
735 const fileMode = fs.statSync(nestedFile).mode & 0o777;
736
737 expect(rootMode).toBe(0o700);
738 expect(dirMode).toBe(0o700);
739 expect(fileMode).toBe(0o600);
740 });
741 });
742
688 743 describe('subtitle debug cache access', () =>
689 744 {
690 745 test('admins can browse the subtitle debug cache folder', async () =>
@@ -170,6 +170,10 @@ afterAll(() =>
170
170
171beforeEach(() =>
171beforeEach(() =>
172{
172{
173 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD;
174 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD_DIR_MODE;
175 delete process.env.FILEMANAGER_LIST_ACCESS_CHMOD_FILE_MODE;
176
173 fs.rmSync(rootDir, { recursive: true, force: true });
177 fs.rmSync(rootDir, { recursive: true, force: true });
174 fs.mkdirSync(rootDir, { recursive: true });
178 fs.mkdirSync(rootDir, { recursive: true });
175 if (mockUserPath)
179 if (mockUserPath)
@@ -685,6 +689,57 @@ describe('orphaned user folder access', () =>
685 });
689 });
686});
690});
687
691
692describe('list access recursive chmod', () =>
693{
694 test('GET /api/files/list applies recursive chmod when enabled', async () =>
695 {
696 const nestedDir = path.join(rootDir, 'locked');
697 const nestedFile = path.join(nestedDir, 'data.txt');
698 fs.mkdirSync(nestedDir, { recursive: true });
699 fs.writeFileSync(nestedFile, 'hello');
700
701 fs.chmodSync(rootDir, 0o700);
702 fs.chmodSync(nestedDir, 0o700);
703 fs.chmodSync(nestedFile, 0o600);
704
705 process.env.FILEMANAGER_LIST_ACCESS_CHMOD = '1';
706
707 const listRes = await req('GET', `/api/files/list?dir=${encodeURIComponent(rootDir)}`);
708 expect(listRes.status).toBe(200);
709
710 const rootMode = fs.statSync(rootDir).mode & 0o777;
711 const dirMode = fs.statSync(nestedDir).mode & 0o777;
712 const fileMode = fs.statSync(nestedFile).mode & 0o777;
713
714 expect(rootMode).toBe(0o777);
715 expect(dirMode).toBe(0o777);
716 expect(fileMode).toBe(0o666);
717 });
718
719 test('GET /api/files/list leaves permissions unchanged when disabled', async () =>
720 {
721 const nestedDir = path.join(rootDir, 'locked');
722 const nestedFile = path.join(nestedDir, 'data.txt');
723 fs.mkdirSync(nestedDir, { recursive: true });
724 fs.writeFileSync(nestedFile, 'hello');
725
726 fs.chmodSync(rootDir, 0o700);
727 fs.chmodSync(nestedDir, 0o700);
728 fs.chmodSync(nestedFile, 0o600);
729
730 const listRes = await req('GET', `/api/files/list?dir=${encodeURIComponent(rootDir)}`);
731 expect(listRes.status).toBe(200);
732
733 const rootMode = fs.statSync(rootDir).mode & 0o777;
734 const dirMode = fs.statSync(nestedDir).mode & 0o777;
735 const fileMode = fs.statSync(nestedFile).mode & 0o777;
736
737 expect(rootMode).toBe(0o700);
738 expect(dirMode).toBe(0o700);
739 expect(fileMode).toBe(0o600);
740 });
741});
742
688describe('subtitle debug cache access', () =>
743describe('subtitle debug cache access', () =>
689{
744{
690 test('admins can browse the subtitle debug cache folder', async () =>
745 test('admins can browse the subtitle debug cache folder', async () =>