async handleGcsObject()

in cloudrun-malware-scanner/scanner.ts [108:175]


  async handleGcsObject(
    storageObject: StorageObjectData,
  ): Promise<ScanResponse> {
    try {
      let bucketDefs: BucketDefs | undefined;

      try {
        this.validateStorageObject(storageObject);
        bucketDefs = this.config.buckets.filter(
          (bucketDefs) => bucketDefs.unscanned === storageObject.bucket,
        )[0];
        if (bucketDefs == null) {
          throw new Error(
            `Request has bucket name ${storageObject.bucket} which is not an unscanned bucket in config`,
          );
        }
      } catch (e) {
        logger.error(`Ignoring request: ${e as Error}`);
        this.metricsClient.writeScanFailed();
        return {message: 'ignoring invalid request', status: 'ignored'};
      }

      // Check for zero length file:
      const fileSize = parseInt(String(storageObject.size));
      if (fileSize === 0 && this.config.ignoreZeroLengthFiles) {
        logger.info(
          {
            scanStatus: {
              bucket: storageObject.bucket,
              file: storageObject.name,
              fileSize,
              status: 'ignored',
              result: 'zero length file',
            },
          },
          `Scan status for gs://${storageObject.bucket}/${storageObject.name}: IGNORED (zero length file})`,
        );
        this.metricsClient.writeScanIgnored(
          bucketDefs.unscanned,
          bucketDefs.clean,
          fileSize,
          'ZERO_LENGTH_FILE',
        );
        return {status: 'ignored', message: 'zero_length_file'};
      }

      // Check if the file is too big to process
      if (fileSize > MAX_FILE_SIZE) {
        logger.info(
          {
            scanStatus: {
              bucket: storageObject.bucket,
              file: storageObject.name,
              fileSize,
              status: 'ignored',
              result: 'file too large',
            },
          },
          `Scan status for gs://${storageObject.bucket}/${storageObject.name}: IGNORED (file too large at ${fileSize} bytes})`,
        );
        this.metricsClient.writeScanIgnored(
          bucketDefs.unscanned,
          bucketDefs.clean,
          fileSize,
          'FILE_TOO_LARGE',
        );
        return {status: 'ignored', message: 'file_too_large'};
      }