Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔒 Limited permissions for uploaded files to 0644 #21841

Merged
merged 7 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ghost/core/core/server/data/importer/import-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ class ImportManager {

try {
await extract(filePath, tmpDir);

// Set permissions for all extracted files
const files = glob.sync('**/*', {cwd: tmpDir, nodir: true});
await Promise.all(files.map(file => fs.chmod(path.join(tmpDir, file), 0o644)));
} catch (err) {
if (err.message.startsWith('ENAMETOOLONG:')) {
// The file was probably zipped with MacOS zip utility. Which doesn't correctly set UTF-8 encoding flag.
Expand Down
25 changes: 25 additions & 0 deletions ghost/core/test/unit/server/data/importer/import-manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const should = require('should');
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const importManager = require('../../../../../core/server/data/importer/import-manager');

describe('Import Manager', function () {
describe('extractZip', function () {
it('extracts zip file and sets correct file permissions', async function () {
const zipPath = path.join(__dirname, '/test.zip');
const extractedPath = await importManager.extractZip(zipPath);
try {
const files = glob.sync('**/*', {cwd: extractedPath, nodir: true});
files.forEach((file) => {
const filePath = path.join(extractedPath, file);
const stats = fs.statSync(filePath);
const fileMode = stats.mode & 0o777;
should.equal(fileMode, 0o644, `File ${file} should have 0644 permissions`);
});
} finally {
await fs.remove(extractedPath);
}
});
});
});
Binary file not shown.
Loading