Skip to content
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
45 changes: 36 additions & 9 deletions src/lib/import-export/export/exporters/default-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class DefaultExporter extends EventEmitter implements Exporter {
private backup: BackupContents;
private readonly options: ExportOptions;

private isExcludedPath( pathToCheck: string ) {
const pathsToExclude = [
isExactPathExcluded( pathToCheck: string ) {
const PATHS_TO_EXCLUDE = [
'wp-content/mu-plugins/sqlite-database-integration',
'wp-content/database',
'wp-content/db.php',
Expand All @@ -44,11 +44,35 @@ export class DefaultExporter extends EventEmitter implements Exporter {
'wp-content/mu-plugins/0-https-for-reverse-proxy.php',
'wp-content/mu-plugins/0-sqlite-command.php',
];
return pathsToExclude.some( ( pathToExclude ) =>

return PATHS_TO_EXCLUDE.some( ( pathToExclude ) =>
pathToCheck.startsWith( path.normalize( pathToExclude ) )
);
}

// Look for disallowed directory names in a given path. If found, determine whether that part of
// the path is a directory or not.
isPathExcludedByPattern( pathToCheck: string ) {
const DIRECTORY_NAMES_TO_EXCLUDE = [ '.git', 'node_modules', 'cache' ];
const pathParts = pathToCheck.split( path.sep );

for ( const directoryName of DIRECTORY_NAMES_TO_EXCLUDE ) {
if ( ! pathParts.includes( directoryName ) ) {
continue;
}
const offenderIndex = pathToCheck.lastIndexOf( directoryName );
const offenderPath = pathToCheck.substring( 0, offenderIndex + directoryName.length );
try {
const stat = fs.statSync( offenderPath );
return stat.isDirectory();
} catch ( error ) {
return false;
}
}

return false;
}

constructor( options: ExportOptions ) {
super();
this.options = options;
Expand All @@ -57,6 +81,7 @@ export class DefaultExporter extends EventEmitter implements Exporter {
sqlFiles: [],
};
}

async canHandle(): Promise< boolean > {
const supportedExtension = [ 'tar.gz', 'tzg', 'zip' ].find( ( ext ) =>
this.options.backupFile.endsWith( ext )
Expand Down Expand Up @@ -185,19 +210,21 @@ export class DefaultExporter extends EventEmitter implements Exporter {
const stat = await fsPromises.stat( fullPath );
if ( stat.isDirectory() ) {
this.archiveBuilder.directory( fullPath, archivePath, ( entry ) => {
const fullArchivePath = path.join( archivePath, entry.name );
const entryPathRelativeToArchiveRoot = path.join( archivePath, entry.name );
const fullEntryPathOnDisk = path.join(
this.options.site.path,
entryPathRelativeToArchiveRoot
);
if (
this.isExcludedPath( fullArchivePath ) ||
entry.name.includes( '.git' ) ||
entry.name.includes( 'node_modules' ) ||
entry.name.includes( 'cache' )
this.isExactPathExcluded( entryPathRelativeToArchiveRoot ) ||
this.isPathExcludedByPattern( fullEntryPathOnDisk )
) {
return false;
}
return entry;
} );
} else {
if ( this.isExcludedPath( archivePath ) ) {
if ( this.isExactPathExcluded( archivePath ) ) {
continue;
}
this.archiveBuilder.file( fullPath, { name: archivePath } );
Expand Down
152 changes: 152 additions & 0 deletions src/lib/import-export/tests/export/exporters/default-exporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ platformTestSuite( 'DefaultExporter', ( { normalize } ) => {

( fs.existsSync as jest.Mock ).mockImplementation( pathExistsMockImplementation );

( fs.statSync as jest.Mock ).mockImplementation( ( filePath: string ) => {
const normalizedPath = normalize( filePath );
if (
mockFiles.some(
( file ) => normalizedPath === normalize( path.join( file.path, file.name ) )
)
) {
return { isDirectory: () => false, isFile: () => true };
} else if ( pathExistsMockImplementation( normalizedPath ) ) {
return { isDirectory: () => true, isFile: () => false };
}
throw new Error( `File not found: ${ normalizedPath }` );
} );

mockBackup = {
backupFile: normalize( '/path/to/backup.tar.gz' ),
sqlFiles: [ normalize( '/tmp/studio_export_123/file.sql' ) ],
Expand Down Expand Up @@ -639,4 +653,142 @@ platformTestSuite( 'DefaultExporter', ( { normalize } ) => {
{ name: 'wp-content/mu-plugins/sqlite-database-integration/example-load.php' }
);
} );

describe( 'isExactPathExcluded', () => {
it( 'should exclude exact paths from PATHS_TO_EXCLUDE list', () => {
const exporter = new DefaultExporter( mockOptions );

expect( exporter.isExactPathExcluded( normalize( 'wp-content/database' ) ) ).toBe( true );
expect( exporter.isExactPathExcluded( normalize( 'wp-content/db.php' ) ) ).toBe( true );
expect( exporter.isExactPathExcluded( normalize( 'wp-content/debug.log' ) ) ).toBe( true );
expect(
exporter.isExactPathExcluded(
normalize( 'wp-content/mu-plugins/sqlite-database-integration' )
)
).toBe( true );
expect(
exporter.isExactPathExcluded(
normalize( 'wp-content/mu-plugins/0-allowed-redirect-hosts.php' )
)
).toBe( true );
} );

it( 'should return false for paths not in the exclusion list', () => {
const exporter = new DefaultExporter( mockOptions );

expect( exporter.isExactPathExcluded( normalize( 'wp-content/plugins' ) ) ).toBe( false );
expect( exporter.isExactPathExcluded( normalize( 'wp-content/themes' ) ) ).toBe( false );
expect( exporter.isExactPathExcluded( normalize( 'wp-content/uploads' ) ) ).toBe( false );
expect( exporter.isExactPathExcluded( normalize( 'wp-config.php' ) ) ).toBe( false );
} );

it( 'should match paths that start with excluded prefixes', () => {
const exporter = new DefaultExporter( mockOptions );

expect(
exporter.isExactPathExcluded( normalize( 'wp-content/database/something.sql' ) )
).toBe( true );
expect(
exporter.isExactPathExcluded(
normalize( 'wp-content/mu-plugins/sqlite-database-integration/load.php' )
)
).toBe( true );
} );
} );

describe( 'isPathExcludedByPattern', () => {
it( 'should exclude disallowed directories based on their names', () => {
( fs.statSync as jest.Mock ).mockReturnValue( {
isDirectory: () => true,
isFile: () => false,
} );

const exporter = new DefaultExporter( mockOptions );

expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/.git' ) )
).toBe( true );
expect(
exporter.isPathExcludedByPattern(
normalize( '/path/to/site/wp-content/node_modules/hello' )
)
).toBe( true );
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/cache' ) )
).toBe( true );
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/my-cache' ) )
).toBe( false );
} );

it( 'should return false for non-excluded directories', () => {
( fs.statSync as jest.Mock ).mockReturnValue( {
isDirectory: () => true,
isFile: () => false,
} );

const exporter = new DefaultExporter( mockOptions );

expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/uploads' ) )
).toBe( false );
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/plugins' ) )
).toBe( false );
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/themes' ) )
).toBe( false );
} );

it( 'should return false for non-existent paths (stat fails)', () => {
const exporter = new DefaultExporter( mockOptions );

// Paths that don't exist in mockFiles will cause statSync to throw, returning false
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/nonexistent' ) )
).toBe( false );
expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/nonexistent/.git' ) )
).toBe( false );
} );

it( 'should return false for files (not directories)', () => {
( fs.statSync as jest.Mock ).mockReturnValue( {
isDirectory: () => false,
isFile: () => true,
} );

const exporter = new DefaultExporter( mockOptions );

expect(
exporter.isPathExcludedByPattern(
normalize( '/path/to/site/wp-content/uploads/file1.jpg' )
)
).toBe( false );
expect( exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-config.php' ) ) ).toBe(
false
);
expect( exporter.isPathExcludedByPattern( normalize( '/path/to/site/node_modules' ) ) ).toBe(
false
);
} );

it( 'should handle directory names found at any position in the path', () => {
( fs.statSync as jest.Mock ).mockReturnValue( {
isDirectory: () => true,
isFile: () => false,
} );

const exporter = new DefaultExporter( mockOptions );

expect(
exporter.isPathExcludedByPattern( normalize( '/path/to/site/wp-content/.git' ) )
).toBe( true );
expect(
exporter.isPathExcludedByPattern(
normalize( '/path/to/site/wp-content/plugins/akismet/node_modules/webpack/index.js' )
)
).toBe( true );
} );
} );
} );