目录

File Exfiltration Control

Since API version 6.0.0(20), the capabilities of setting and obtaining approval information and configuring the single-pass or dual-pass cross-workspace transfer policies are supported.

Use Cases

In file transfer scenarios, you can set the single-pass or dual-pass transfer policy for workspaces to flexibly configure the permission for sending app files between workspaces, implementing secure and controllable cross-workspace file sharing. If file outbound needs to be controlled by the approval process, the system supports the configuration of approval information. In addition, the approval status synchronization API can be called to obtain the approval result in real time, ensuring the security and compliance of the outbound process.

In single-pass mode, files can be sent only from a personal workspace to an enterprise workspace, and reverse transfer is prohibited.

In dual-pass mode, files can be transferred between a personal workspace and an enterprise workspace in both directions.

API Description

For details about these APIs, please refer to the API Reference.

API Description
policyPush(policyContext: string): void Configures a single-pass or dual-pass cross-workspace data transfer policy.
setAuditInfo(transactionNum: string, info: AuditInfo): number Sets review information and returns the result.
getAuditInfo(transactionNum: string): AuditInfo Obtains review information.

Development Procedure

  1. Import the dependencies of the API module for file outbound control.
import { fileTransfer } from '@kit.EnterpriseSpaceKit';
import { ErrCode } from '../../common/ErrCode';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Encapsulate the API for file outbound control.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

export enum EAuditAbilityType {
  SUBMIT = 'submit',
  QUERY = 'query'
}

export enum EAuditStatus {
  WAITING_APPROVAL = '1',
  CANCEL_APPROVAL = '2',
  REJECT_APPROVAL = '3',
  ACCEPT_APPROVAL = '4'
}

export interface FileItem {
  name: string;
  size: number;
  url: string;
  transactionNum: string;
  selected: boolean;
}


export interface FileParams {
  routerUri: string,
  fileList: FileItem[]
}


export interface ISpaceTransferParam {
  fileParams: FileParams,
  action: EAuditAbilityType,
  fileList: FileItem[]
}

export class FileTransferControlApi {
  static setAuditInfo(transactionNum: string, dstInfo: fileTransfer.AuditInfo): number {
    try {
      const ret: number = fileTransfer.setAuditInfo(transactionNum, dstInfo);
      hilog.info(DOMAIN, TAG, `Succeeded in setting audit info, number:`, ret);
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set audit info. Code: ${err?.code}, message: ${err?.message}`);
      return ErrCode.ERR;
    }
  }

  static getAuditInfo(transactionNum: string): fileTransfer.AuditInfo | undefined {
    try {
      const auditInfo: fileTransfer.AuditInfo = fileTransfer.getAuditInfo(transactionNum);
      hilog.info(DOMAIN, TAG, `Succeeded in getting audit info:` + JSON.stringify(auditInfo));
      return auditInfo;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to get audit info. Code: ${err.code}, message: ${err.message}`);
      return undefined;
    }
  }

  static policyPush(policyContext: string): number {
    try {
      fileTransfer.policyPush(policyContext);
      hilog.info(DOMAIN, TAG, `Succeeded in pushing policy`);
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to push policy. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }
}
  1. Import the dependencies of the service implementation for file outbound control.
import { router } from '@kit.ArkUI';
import { EAuditAbilityType, FileItem, FileParams } from '../api/FileTransferControlApi';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { Want } from '@kit.AbilityKit';
  1. Implement the file outbound control service.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct FileTransferPage {
  async FileTransferControl(type: string, uri: string): Promise<void> {
    // Preset the files to be approved and their enterprise app information. The data is transferred by the workspace transfer app.
    const file: FileItem = {
      name: '',
      size: 1024,
      url: '',
      transactionNum: '123',
      selected: true
    };

    // Simulate the workspace transfer app to start the enterprise app.
    const want: Want = {
      bundleName: 'com.samples.spacemanager', // Enterprise app to be started.
      abilityName: 'EntryAbility', // Ability of the enterprise app to be started.
      parameters: {
        fileParams: {
          routerUri: uri,
          fileList: [file]
        },
        action: EAuditAbilityType.SUBMIT,
        fileList: [file]
      }
    };

    // The actual enterprise app is started by context.startAbility(want). You only need to pay attention to the processing after the ability of the enterprise app is started.
    try {
      let param: FileParams | undefined;
      let action: string | undefined;
      if (want.parameters) {
        // After the enterprise app is started, obtain the list of files to be transferred.
        param = want.parameters[`fileParams`] as FileParams;
        action = want.parameters['action'] as string;
      }
      let fileList = param?.fileList ?? [];
      let routerUri = param?.routerUri;
      AppStorage.setOrCreate('fileList', fileList);
      if (action === EAuditAbilityType.SUBMIT) {
        // Pre-processing for submitting review information

        // Start the enterprise app review service.
        this.getUIContext().getRouter().pushUrl({ url: `filetransfercontrol/pages/${routerUri}` });
      } else if (action === EAuditAbilityType.QUERY) {
        // Pre-processing for querying review information

        // Start the enterprise app query service.
        this.getUIContext().getRouter().pushUrl({ url: `filetransfercontrol/pages/${routerUri}` });
      } else {
        // Error handling action.
        hilog.error(DOMAIN, TAG, 'Invalid action type!');
      }
    } catch (err) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, `Failed to audit files. Code: ${err.code}, message: ${err.message}`);
    }
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.setPolicy'))
          .buttonCommonStyle()
          .onClick(() => {
            this.getUIContext().getRouter().pushUrl({ url: 'filetransfercontrol/pages/SavePolicyPage' });
          })
      }

      Row() {
        Button($r('app.string.FileTransferControl_Start_Submit'))
          .buttonCommonStyle()
          .onClick(() => {
            this.FileTransferControl(EAuditAbilityType.SUBMIT, 'SubmitPage');
          })
      }

      Row() {
        Button($r('app.string.FileTransferControl_Start_Query'))
          .buttonCommonStyle()
          .onClick(() => {
            this.FileTransferControl(EAuditAbilityType.QUERY, 'QueryPage');
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(400)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 5 })
}
  1. Import dependencies related to the save policy page.
import { router } from '@kit.ArkUI';
import { ErrCode } from '../../common/ErrCode';
import { FileTransferControlApi } from '../api/FileTransferControlApi';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Process services on the save policy page.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry()
@Component
struct SavePolicyPage {
  SavePolicy() {
    let policyContext =
      '{' +
        '  "config": {' +
        '    "inEnable": "1",' +
        '    "incoming_check": {' +
        '      "data_list": [' +
        '        {' +
        '          "allow": "VirusCheck.Result == 0",' +
        '          "approval": "",' +
        '          "check_point": "VirusCheck",' +
        '          "check_point_name": "VirusCheck_in",' +
        '          "is_enable": "true",' +
        '          "forbidden": "VirusCheck.Result == 1",' +
        '          "order": "0"' +
        '        }' +
        '      ]' +
        '    },' +
        '    "outEnable": "1",' +
        '    "outgoing_check": {' +
        '      "data_list": [' +
        '        {' +
        '          "allow": "SecurityCheck.Result == 3 or SecurityCheck.Result == 4 or SecurityCheck.Result == 6 or SecurityCheck.Result == 7",' +
        '          "approval": "SecurityCheck.Result == 10",' +
        '          "check_point": "SecurityCheck",' +
        '          "check_point_name": "SecurityCheck_out",' +
        '          "is_enable": "true",' +
        '          "forbidden": "SecurityCheck.Result == 0 or SecurityCheck.Result == 1 or SecurityCheck.Result == 12 or SecurityCheck.Result == 2 or SecurityCheck.Result == 5 or SecurityCheck.Result == 8 or SecurityCheck.Result == 9 or SecurityCheck.Result == 11",' +
        '          "order": "0"' +
        '        }' +
        '      ]' +
        '    },' +
        '    "checkpoint_config": {' +
        '      "data_list": [' +
        '        {' +
        '          "check_point_name": "SecurityCheck",' +
        '          "bundle_name": "com.example.enterprisespacekit_samplecode_clientdemo_arkts",' +
        '          "ability_name": "TestScanAbility",' +
        '          "func_code": "2",' +
        '          "type": "2"' +
        '        },' +
        '        {' +
        '          "check_point_name": "VirusCheck",' +
        '          "bundle_name": "com.example.enterprisespacekit_samplecode_clientdemo_arkts",' +
        '          "ability_name": "TestScanAbility",' +
        '          "func_code": "3",' +
        '          "type": "1"' +
        '        }' +
        '      ]' +
        '    },' +
        '    "approvalpoint_config": {' +
        '      "data_list": [' +
        '        {' +
        '          "bundle_name": "com.example.enterprisespacekit_samplecode_clientdemo_arkts",' +
        '          "ability_name": "TestApprovalAbility"' +
        '        }' +
        '      ]' +
        '    }' +
        '  }' +
        '}';
    if (FileTransferControlApi.policyPush(policyContext) !== ErrCode.OK) {
      // Handle exceptions and errors.
      hilog.error(DOMAIN, TAG, 'Failed to push policy!');
      return;
    }

    // Process the post-logic.
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.savePolicy'))
          .buttonCommonStyle()
          .onClick(() => {
            this.SavePolicy();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 20 })
}
  1. Import dependencies related to the approval page.
import { router } from '@kit.ArkUI';
import { fileTransfer } from '@kit.EnterpriseSpaceKit';
import { ErrCode } from '../../common/ErrCode';
import { EAuditStatus, FileItem, FileTransferControlApi } from '../api/FileTransferControlApi';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Implement services on the approval page.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct SubmitPage {
  private fileList: FileItem[] = AppStorage.get<FileItem[]>('fileList') || [];

  CheckAuditInfo() {
    // The enterprise app performs pre-check.

    // After the check is passed, set the approval status.
    this.fileList?.forEach(item => {
      const transactionNum: string = item.transactionNum; // Transaction number automatically generated by the database, which must exist in the database.
      const srcName = item.name; // Name of the file to be approved.
      const srcSize = item.size; // Size of the file to be approved.
      const srcUrl = item.url; // URL of the file to be approved.
      const srcSelected = item.selected; // Whether the file to be approved is selected.

      // File filtering

      const dstInfo: fileTransfer.AuditInfo = {
        auditId: 'A123456', // Unique ID of an approval item defined by the enterprise app.
        userId: 'userId', // User ID.
        userName: 'userName', // User name.
        time: new Date().getTime(), // Approval timestamp.
        comments: 'Waiting approval', // Approval comments.
        status: EAuditStatus.WAITING_APPROVAL // File approval status.
      };

      if (FileTransferControlApi.setAuditInfo(transactionNum, dstInfo) !== ErrCode.OK) {
        // If the setting fails, the enterprise app processes the failure logic.
        hilog.error(DOMAIN, TAG, 'Failed to set audit info!');
        return;
      }

      // If the setting is successful, the enterprise app processes the post-logic.
      return;
    })
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.submit'))
          .buttonCommonStyle()
          .onClick(() => {
            this.CheckAuditInfo();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 20 })
}
  1. Import dependencies related to the query approval page.
import { router } from '@kit.ArkUI';
import { fileTransfer } from '@kit.EnterpriseSpaceKit';
import { FileItem, FileTransferControlApi } from '../api/FileTransferControlApi';
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Implement services on the query approval page.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct QueryPage {
  private fileList: FileItem[] = AppStorage.get<FileItem[]>('fileList') || [];

  QueryAuditInfo() {
    // The enterprise app performs pre-check.

    // ...

    // After the check is passed, obtain the approval information and process other services.
    this.fileList?.forEach(item => {
      const transactionNum: string = item.transactionNum; // Transaction number automatically generated by the database, which must exist in the database.
      const srcName = item.name; // Name of the file to be approved.
      const srcSize = item.size; // Size of the file to be approved.
      const srcUrl = item.url; // URL of the file to be approved.
      const srcSelected = item.selected; // Whether the file to be approved is selected.

      // File filtering

      let auditInfo: fileTransfer.AuditInfo | undefined = FileTransferControlApi.getAuditInfo(transactionNum);
      if (auditInfo === undefined) {
        // The enterprise app processes the error logic.
        hilog.error(DOMAIN, TAG, 'Failed to get audit info!');
        return;
      }

      // The enterprise app uses auditInfo to process the correct logic.
    })
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.query'))
          .buttonCommonStyle()
          .onClick(() => {
            this.QueryAuditInfo();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 5 })
}