Files
privacy.sexy/src/infrastructure/ReadbackFileWriter/ReadbackFileWriter.ts
undergroundwires f03fc24098 Add AD detection on desktop app #264, #304
This commit addresses issues #264 and #304, where users were not
receiving error messages when script execution failed due to
antivirus intervention, particularly with Microsoft Defender.
Now, desktop app users will see a detailed error message with
guidance on next steps if script saving or execution fails due
to antivirus removal.

Key changes:

- Implement a check to detect failure in file writing,
  including reading the written file back. This method effectively
  detects antivirus interventions, as the read operation triggers
  an antivirus scan, leading to file deletion by the antivirus.
- Introduce a specific error message for scenarios where an
  antivirus intervention is detected.
2024-01-16 22:26:28 +01:00

60 lines
2.0 KiB
TypeScript

/**
* It defines the contract for file writing operations with an added layer of
* verification. This approach is useful in environments where file write operations
* might be silently intercepted or manipulated by external factors, such as antivirus software.
*
* This additional verification provides a more reliable and transparent file writing
* process, enhancing the application's resilience against external disruptions and
* improving the overall user experience. It enables the application to notify users
* of potential issues, such as antivirus interventions, and offer guidance on how to
* resolve them.
*/
export interface ReadbackFileWriter {
writeAndVerifyFile(filePath: string, fileContents: string): Promise<FileWriteOutcome>;
}
export type FileWriteOutcome = SuccessfulFileWrite | FailedFileWrite;
export type FileWriteErrorType =
| UnionOfConstArray<typeof FileWriteOperationErrors>
| UnionOfConstArray<typeof FileReadbackVerificationErrors>;
export const FileWriteOperationErrors = [
'WriteOperationFailed',
] as const;
export const FileReadbackVerificationErrors = [
'FileExistenceVerificationFailed',
'ContentVerificationFailed',
/*
This error indicates a failure in verifying the contents of a written file.
This error often occurs when antivirus software falsely identifies a script as harmful and
either alters or removes it during the readback process. This verification step is crucial
for detecting and handling such antivirus interventions.
*/
'ReadVerificationFailed',
] as const;
interface FileWriteStatus {
readonly success: boolean;
readonly error?: FileWriteError;
}
export interface SuccessfulFileWrite extends FileWriteStatus {
readonly success: true;
readonly error?: undefined;
}
export interface FailedFileWrite extends FileWriteStatus {
readonly success: false;
readonly error: FileWriteError;
}
export interface FileWriteError {
readonly type: FileWriteErrorType;
readonly message: string;
}
type UnionOfConstArray<T extends ReadonlyArray<unknown>> = T[number];