Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): audit a storage import that aborts partway

Yudhi Armyndharis223ec2a5b9e4
diff --git a/src/modules/infra/infra-storage.controller.spec.ts b/src/modules/infra/infra-storage.controller.spec.ts
index 518ee4d2f..89d292bd4 100644
--- a/src/modules/infra/infra-storage.controller.spec.ts
+++ b/src/modules/infra/infra-storage.controller.spec.ts
@@ -336,6 +336,30 @@ describe('InfraStorageController audit trail (light-dependency handlers)', () =>
}
});
+ it('importStorage records an aborted import as a warning, since the entries before the abort were kept', async () => {
+ const audit = { logInfo: jest.fn().mockResolvedValue(null), logWarn: jest.fn().mockResolvedValue(null) };
+ const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue('/srv/openwa');
+ (fs.existsSync as jest.Mock).mockImplementation((p: string) => p === '/srv/openwa/data/exports/x.tar.gz');
+ try {
+ const storageService = {
+ importFromStream: jest.fn().mockRejectedValue(new Error('archive exceeds 100000 entries')),
+ getCurrentStorageType: () => 'local',
+ };
+ await expect(
+ new InfraStorageController(storageService as never, audit as never).importStorage({
+ filePath: 'data/exports/x.tar.gz',
+ }),
+ ).rejects.toBeInstanceOf(BadRequestException);
+ expect(audit.logInfo).not.toHaveBeenCalled();
+ expect(audit.logWarn).toHaveBeenCalledWith(AuditAction.INFRA_STORAGE_IMPORTED, {
+ metadata: { aborted: true, error: 'archive exceeds 100000 entries', storageType: 'local' },
+ });
+ } finally {
+ cwdSpy.mockRestore();
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ }
+ });
+
it('exportStorage emits INFRA_STORAGE_EXPORTED', async () => {
const audit = makeAudit();
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'owa-audit-'));
diff --git a/src/modules/infra/infra-storage.controller.ts b/src/modules/infra/infra-storage.controller.ts
index 3742f4e17..dc91165d8 100644
--- a/src/modules/infra/infra-storage.controller.ts
+++ b/src/modules/infra/infra-storage.controller.ts
@@ -266,7 +266,12 @@ export class InfraStorageController implements OnApplicationBootstrap {
try {
({ imported: count, failed } = await this.storageService.importFromStream(readStream));
} catch (error) {
- throw new BadRequestException(`Storage import failed: ${error instanceof Error ? error.message : String(error)}`);
+ const message = error instanceof Error ? error.message : String(error);
+ // An abort keeps the entries written before it (there is no rollback), so it is audited too.
+ await this.auditService?.logWarn(AuditAction.INFRA_STORAGE_IMPORTED, {
+ metadata: { aborted: true, error: message, storageType: this.storageService.getCurrentStorageType() },
+ });
+ throw new BadRequestException(`Storage import failed: ${message}`);
}
const storageType = this.storageService.getCurrentStorageType();