mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Cleanup
This commit is contained in:
1 parent
9507177aaa
commit
2ea05addcc
4 files changed
+67
-61
No files matched your search
@@ -3,6 +3,7 @@ import { ProjectsManager } from './ProjectsManager';
|
||||
import { JSRuntimeFS } from './JSRuntime';
|
||||
import { Git } from './git';
|
||||
import type { NPool } from '@nostrify/nostrify';
|
||||
import type JSZip from 'jszip';
|
||||
|
||||
const createMockNostr = (): NPool => ({
|
||||
req: vi.fn(),
|
||||
@@ -438,6 +439,50 @@ describe('ProjectsManager', () => {
|
||||
vi.restoreAllMocks();
|
||||
}
|
||||
});
|
||||
|
||||
it('should prevent zip slip attacks during bulk file extraction', async () => {
|
||||
await projectsManager.init();
|
||||
|
||||
// Create a mock ZIP with malicious paths for bulk import
|
||||
const mockZip = {
|
||||
files: {
|
||||
'projects/project1/../../../etc/passwd': {
|
||||
dir: false,
|
||||
async: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3]))
|
||||
},
|
||||
'projects/project1/../../outside.txt': {
|
||||
dir: false,
|
||||
async: vi.fn().mockResolvedValue(new Uint8Array([4, 5, 6]))
|
||||
},
|
||||
'projects/project1/safe-file.txt': {
|
||||
dir: false,
|
||||
async: vi.fn().mockResolvedValue(new Uint8Array([7, 8, 9]))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Spy on console.warn to check for security warnings
|
||||
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
|
||||
try {
|
||||
// Extract files from the malicious ZIP for bulk import
|
||||
const files = await projectsManager.extractFilesFromZip(mockZip as unknown as JSZip, 'projects/project1');
|
||||
|
||||
// Should only contain safe files
|
||||
expect(Object.keys(files)).toEqual(['safe-file.txt']);
|
||||
expect(files['safe-file.txt']).toEqual(new Uint8Array([7, 8, 9]));
|
||||
|
||||
// Should have logged a security warning about skipped files
|
||||
expect(consoleSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Skipped 2 files due to resolving to paths outside of project directory:'),
|
||||
['projects/project1/../../../etc/passwd', 'projects/project1/../../outside.txt']
|
||||
);
|
||||
|
||||
} finally {
|
||||
// Restore mocks
|
||||
consoleSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('renameProject', () => {
|
||||
|
||||
@@ -900,6 +900,7 @@ export class ProjectsManager {
|
||||
async extractFilesFromZip(zip: JSZip, projectPath: string): Promise<{ [path: string]: Uint8Array }> {
|
||||
const files: { [path: string]: Uint8Array } = {};
|
||||
const skippedFiles: string[] = [];
|
||||
const securitySkippedFiles: string[] = [];
|
||||
|
||||
for (const [relativePath, zipEntry] of Object.entries(zip.files)) {
|
||||
if (zipEntry.dir) continue;
|
||||
@@ -915,6 +916,13 @@ export class ProjectsManager {
|
||||
// Skip empty paths (shouldn't happen due to the dir check above, but just in case)
|
||||
if (!fileRelativePath) continue;
|
||||
|
||||
// Validate extraction path is safe (zip slip protection)
|
||||
// Check for directory traversal attempts in the original path
|
||||
if (fileRelativePath.includes('../') || fileRelativePath.includes('..\\') || fileRelativePath.startsWith('/') || fileRelativePath.startsWith('\\')) {
|
||||
securitySkippedFiles.push(relativePath);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = await zipEntry.async('uint8array');
|
||||
files[fileRelativePath] = content;
|
||||
@@ -924,6 +932,10 @@ export class ProjectsManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (securitySkippedFiles.length > 0) {
|
||||
console.warn(`Skipped ${securitySkippedFiles.length} files due to resolving to paths outside of project directory:`, securitySkippedFiles);
|
||||
}
|
||||
|
||||
if (skippedFiles.length > 0) {
|
||||
console.warn(`Skipped ${skippedFiles.length} files due to extraction errors:`, skippedFiles);
|
||||
}
|
||||
|
||||
@@ -250,8 +250,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: 'Your browser does not support persistent storage requests.',
|
||||
failedToRequestPersistentStorage: 'Failed to request persistent storage',
|
||||
|
||||
|
||||
|
||||
// 404 Page
|
||||
pageNotFound: 'Oops! Page not found',
|
||||
returnToHome: 'Return to Home',
|
||||
@@ -552,8 +550,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: 'Seu navegador não suporta solicitações de armazenamento persistente.',
|
||||
failedToRequestPersistentStorage: 'Falha ao solicitar armazenamento persistente',
|
||||
|
||||
|
||||
|
||||
// About Settings
|
||||
aboutShakespeare: 'Sobre o Shakespeare',
|
||||
aboutShakespeareDescription: 'Informações sobre o Shakespeare.',
|
||||
@@ -863,8 +859,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: '您的浏览器不支持持久存储请求。',
|
||||
failedToRequestPersistentStorage: '请求持久存储失败',
|
||||
|
||||
|
||||
|
||||
// About Settings
|
||||
aboutShakespeare: '关于Shakespeare',
|
||||
aboutShakespeareDescription: '关于Shakespeare的信息。',
|
||||
@@ -1174,7 +1168,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: 'Burauzar ku ba ta goyan bayan bukatun ajiyar ta dindindin ba.',
|
||||
failedToRequestPersistentStorage: 'Ya gaza neman ajiyar ta dindindin',
|
||||
|
||||
|
||||
// About Settings
|
||||
aboutShakespeare: 'Game da Shakespeare',
|
||||
aboutShakespeareDescription: 'Bayani game da Shakespeare.',
|
||||
@@ -1484,7 +1477,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: 'Aṣawakiri rẹ ko ni atilẹyin awọn ibeere ibi ipamọ ti o wa nigbagbogbo.',
|
||||
failedToRequestPersistentStorage: 'O kuna lati beere ibi ipamọ ti o wa nigbagbogbo',
|
||||
|
||||
|
||||
// About Settings
|
||||
aboutShakespeare: 'Nipa Shakespeare',
|
||||
aboutShakespeareDescription: 'Alaye nipa Shakespeare.',
|
||||
@@ -1789,7 +1781,6 @@ const resources = {
|
||||
persistentStorageNotSupportedDescription: 'Browser gị akwadoghị arịrịọ nchekwa na-adịgide adịgide.',
|
||||
failedToRequestPersistentStorage: 'O dara ịrịọ nchekwa na-adịgide adịgide',
|
||||
|
||||
|
||||
// About Settings
|
||||
aboutShakespeare: 'Gbasara Shakespeare',
|
||||
aboutShakespeareDescription: 'Ozi gbasara Shakespeare.',
|
||||
|
||||
+10
-52
@@ -174,63 +174,21 @@ export default function Clone() {
|
||||
}, [handleClone, searchParams]);
|
||||
|
||||
const handleImportZip = async (
|
||||
data: { file: File; projectId?: string } | { projects: { id: string; files: { [path: string]: Uint8Array } }[] },
|
||||
data: { file: File; projectId?: string },
|
||||
overwrite: boolean
|
||||
) => {
|
||||
try {
|
||||
if ('file' in data) {
|
||||
// Single project import from ZIP file
|
||||
const project = await projectsManager.importProjectFromZip(data.file, data.projectId, overwrite);
|
||||
// Single project import from ZIP file
|
||||
const project = await projectsManager.importProjectFromZip(data.file, data.projectId, overwrite);
|
||||
|
||||
// Show success toast
|
||||
toast({
|
||||
title: overwrite ? "Project Overwritten Successfully" : "Project Imported Successfully",
|
||||
description: `"${project.name}" has been ${overwrite ? 'overwritten' : 'imported'} and is ready to use.`,
|
||||
});
|
||||
// Show success toast
|
||||
toast({
|
||||
title: overwrite ? "Project Overwritten Successfully" : "Project Imported Successfully",
|
||||
description: `"${project.name}" has been ${overwrite ? 'overwritten' : 'imported'} and is ready to use.`,
|
||||
});
|
||||
|
||||
// Navigate to the imported project
|
||||
navigate(`/project/${project.id}`);
|
||||
} else {
|
||||
// Bulk project import
|
||||
let successCount = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const project of data.projects) {
|
||||
try {
|
||||
await projectsManager.importProject(
|
||||
{ files: project.files },
|
||||
{ projectId: project.id, overwrite, projectName: project.id }
|
||||
);
|
||||
successCount++;
|
||||
} catch (error) {
|
||||
console.error(`Failed to import project ${project.id}:`, error);
|
||||
errors.push(`${project.id}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Show appropriate toast based on results
|
||||
if (successCount === data.projects.length) {
|
||||
toast({
|
||||
title: "All Projects Imported Successfully",
|
||||
description: `${successCount} project${successCount !== 1 ? 's' : ''} imported successfully.`,
|
||||
});
|
||||
} else if (successCount > 0) {
|
||||
toast({
|
||||
title: "Partial Import Success",
|
||||
description: `${successCount} of ${data.projects.length} projects imported. ${errors.length} failed.`,
|
||||
variant: "destructive",
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Import Failed",
|
||||
description: "All projects failed to import. Check console for details.",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
|
||||
// Navigate to homepage to see all projects
|
||||
navigate('/');
|
||||
}
|
||||
// Navigate to the imported project
|
||||
navigate(`/project/${project.id}`);
|
||||
} catch (error) {
|
||||
console.error('Failed to import project:', error);
|
||||
throw error; // Re-throw to let the dialog handle the error display
|
||||
|
||||
Reference in new issue
Block a user