Skip to content

Commit

Permalink
ref: feedback/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy committed Nov 19, 2024
1 parent dba9ed4 commit ddc02cb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</div>
<div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:29px;text-align:center;color:#000000;">
<span style="color: #FF0000; font-weight: 600;">
Issue: ${ it.notifications[0].item.av_status === "scan_failed" ? "Scan Failed 🔍" : it.notifications[0].item.av_status === "malicious" ? "Malicious Content Detected ⚠️" : "Fichier trop volumineux pour être analysé 🚫" }
Issue: ${ it.notifications[0].item.av_status === "scan_failed" ? "Scan Failed 🔍" : it.notifications[0].item.av_status === "malicious" ? "Malicious Content Detected ⚠️" : "File too large to be scanned 🚫" }
</span>
</div>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</div>
<div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:29px;text-align:center;color:#000000;">
<span style="color: #FF0000; font-weight: 600;">
Problème : ${it.notifications[0].item.av_status === "scan_failed" ? "Échec de l'analyse 🔍" : "Contenu malveillant détecté ⚠️"}
Problème: ${ it.notifications[0].item.av_status === "scan_failed" ? "Échec de l'analyse 🔍" : it.notifications[0].item.av_status === "malicious" ? "Contenu malveillant détecté ⚠️" : "Fichier trop volumineux pour être analysé 🚫" }
</span>
</div>
</td>
Expand Down
2 changes: 1 addition & 1 deletion tdrive/backend/node/src/services/av/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AVServiceImpl implements TdriveServiceProvider, Initializable {
this.av.scanStream(readableStream, async (err, { isInfected, viruses }) => {
if (err) {
await onScanComplete("scan_failed");
this.logger.info(`Scan failed for item ${item.id} due to error: ${err.message}`);
this.logger.error(`Scan failed for item ${item.id} due to error: ${err.message}`);
} else if (isInfected) {
await onScanComplete("malicious");
this.logger.info(`Item ${item.id} is malicious. Viruses found: ${viruses.join(", ")}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import * as UUIDTools from "../../../utils/uuid";

export const TYPE = "drive_files";
export type DriveScope = "personal" | "shared";
export type AVStatus = "uploaded" | "scanning" | "scan_failed" | "safe" | "malicious" | "skipped";
export type AVStatusSafe = "safe";
export type AVStatusUnsafe = "uploaded" | "scanning" | "scan_failed" | "malicious" | "skipped";
export type AVStatus = AVStatusSafe | AVStatusUnsafe;

/**
* This represents an item in the file hierarchy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DocumentsEngine implements Initializable {
const emailTemplate = eventToTemplateMap[event];

if (!emailTemplate) {
logger.error(`Unknown event type: ${event}`);
logger.error(`Error dispatching document event. Unknown event type: ${event}`);
return; // Early return on unknown event type
}

Expand Down Expand Up @@ -72,9 +72,9 @@ export class DocumentsEngine implements Initializable {
);

localEventBus.subscribe(
DocumentEvents.DOCUEMNT_AV_SCAN_ALERT,
DocumentEvents.DOCUMENT_AV_SCAN_ALERT,
async (e: NotificationPayloadType) => {
await this.DispatchDocumentEvent(e, DocumentEvents.DOCUEMNT_AV_SCAN_ALERT);
await this.DispatchDocumentEvent(e, DocumentEvents.DOCUMENT_AV_SCAN_ALERT);
},
);

Expand All @@ -90,6 +90,6 @@ export class DocumentsEngine implements Initializable {
}

notifyDocumentAVScanAlert(notificationPayload: NotificationPayloadType) {
localEventBus.publish(DocumentEvents.DOCUEMNT_AV_SCAN_ALERT, notificationPayload);
localEventBus.publish(DocumentEvents.DOCUMENT_AV_SCAN_ALERT, notificationPayload);
}
}
41 changes: 25 additions & 16 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,6 @@ export class DocumentsService {
* @returns {Promise<boolean>} - the check result
*/
containsMaliciousFiles = async (id: string, context: DriveExecutionContext): Promise<boolean> => {
const unsafeStatuses = ["malicious", "scanning", "scan_failed", "skipped"];

if (!context) {
this.logger.error("Invalid execution context");
return null;
Expand Down Expand Up @@ -1115,8 +1113,9 @@ export class DocumentsService {

// Check files in the current directory
const maliciousFiles = entities.filter(
child => !child.is_directory && unsafeStatuses.includes(child.av_status),
child => !child.is_directory && child.av_status !== "safe",
);

if (maliciousFiles.length > 0) {
return true;
}
Expand Down Expand Up @@ -1146,15 +1145,15 @@ export class DocumentsService {
*/
reScan = async (id: string, context: DriveExecutionContext): Promise<DriveFile> => {
if (!context) {
this.logger.error("invalid execution context");
return null;
this.logger.error("Invalid execution context");
throw new Error("Execution context is required"); // Explicit error to indicate a fatal issue
}

try {
const hasAccess = await checkAccess(id, null, "write", this.repository, context);
if (!hasAccess) {
this.logger.error("user does not have access drive item ", id);
throw Error("user does not have access to this item");
this.logger.warn(`Access denied for user to drive item ${id}`);
throw new Error("User does not have access to this item");
}

const item = await this.repository.findOne(
Expand All @@ -1167,40 +1166,50 @@ export class DocumentsService {
);

if (!item) {
throw Error("Drive item not found");
this.logger.warn(`Drive item ${id} not found`);
throw new Error("Drive item not found");
}

if (item.is_directory) {
throw Error("cannot create version for a directory");
this.logger.warn(`Attempted to rescan a directory ${id}`);
throw new Error("Cannot rescan a directory");
}

// If AV feature is enabled, scan the file
if (globalResolver.services.av?.avEnabled) {
try {
item.av_status = await globalResolver.services.av.scanDocument(
item,
item.last_version_cache,
async (av_status: AVStatus) => {
// Update the AV status of the file
await this.handleAVStatusUpdate(item, av_status, context);
},
context,
);

await this.repository.save(item);

if (item.av_status === "skipped") {
// Notify the user that the document has been skipped
this.logger.info(`AV scan skipped for file ${item.id}`);
await this.notifyAVScanAlert(item, context);
}
} catch (error) {
} catch (scanError) {
this.logger.error(
`Error scanning file ${item.last_version_cache.file_metadata.external_id}`,
`Error scanning file ${item.last_version_cache.file_metadata.external_id}: ${scanError.message}`,
);
throw new CrudException("Error scanning file", 500);
}
} else {
this.logger.error("AV scanning is not enabled");
throw new Error("An unexpected error occurred. Please try again later.");
}

return item;
} catch (error) {
logger.error({ error: `${error}` }, "Failed to create Drive item version");
CrudException.throwMe(error, new CrudException("Failed to create Drive item version", 500));
this.logger.error(
{ error: error.message, stack: error.stack },
`Failed to rescan drive item ${id}`,
);
throw new CrudException("Failed to rescan the drive item", 500);
}
};

Expand Down
4 changes: 2 additions & 2 deletions tdrive/backend/node/src/services/documents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ export type DriveTdriveTab = {
export enum DocumentEvents {
DOCUMENT_SAHRED = "document_shared",
DOCUMENT_VERSION_UPDATED = "document_version_updated",
DOCUEMNT_AV_SCAN_ALERT = "document_av_scan_alert",
DOCUMENT_AV_SCAN_ALERT = "document_av_scan_alert",
}

export const eventToTemplateMap: Record<string, any> = {
[DocumentEvents.DOCUEMNT_AV_SCAN_ALERT]: "notification-document-av-scan-alert",
[DocumentEvents.DOCUMENT_AV_SCAN_ALERT]: "notification-document-av-scan-alert",
[DocumentEvents.DOCUMENT_VERSION_UPDATED]: "notification-document-version-updated",
[DocumentEvents.DOCUMENT_SAHRED]: "notification-document-shared",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ describe("the Drive Search feature", () => {
//when
let documents = await userOne.searchDocument(filters);

console.log("🚀🚀 documents", documents.entities);

//then
expect(documents.entities).toHaveLength(3);
expect(documents.entities[0].name.includes("pdf")).toBeTruthy();
Expand Down

0 comments on commit ddc02cb

Please sign in to comment.