/**
Copyright: 2024 Contrast Security, Inc
  Contact: support@contrastsecurity.com
  License: Commercial

  NOTICE: This Software and the patented inventions embodied within may only be
  used as part of Contrast Security’s commercial offerings. Even though it is
  made available through public repositories, use of this Software is subject to
  the applicable End User Licensing Agreement found at
  https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
  between Contrast Security and the End User. The Software may not be reverse
  engineered, modified, repackaged, sold, redistributed or otherwise used in a
  way not consistent with the End User License Agreement.
*/
'use strict';

/**
 * InFilter hooks for fs module methods.
 * @module lib/rules/protect/in-filters/fs
 */

const BaseSensor = require('../../hooks/frameworks/base');
const constants = require('../../constants');
const patcher = require('../../hooks/patcher');
const { PATCH_TYPES } = require('../../constants');
const { emitSinkEvent } = require('../../hooks/frameworks/common');

const { SINK_TYPES } = constants;
const ID = 'fs';

class FileSystemSensor extends BaseSensor {
  constructor(agent) {
    super({ id: ID, agent });
  }

  install({ ModuleHook }) {
    const fsMethods = (module.exports.fsMethods = [
      { method: 'access' },
      { method: 'accessSync' },
      { method: 'appendFile' },
      { method: 'appendFileSync' },
      { method: 'chmod' },
      { method: 'chmodSync' },
      { method: 'chown' },
      { method: 'chownSync' },
      { method: 'copyFile', indexes: [0, 1] },
      { method: 'copyFileSync', indexes: [0, 1] },
      { method: 'createReadStream' },
      { method: 'createWriteStream' },
      { method: 'lchmod' },
      { method: 'lchmodSync' },
      { method: 'lchown' },
      { method: 'lchownSync' },
      { method: 'mkdir' },
      { method: 'mkdirSync' },
      { method: 'open' },
      { method: 'openSync' },
      { method: 'readFile' },
      { method: 'readFileSync' },
      { method: 'readdir' },
      { method: 'readdirSync' },
      { method: 'readlink' },
      { method: 'readlinkSync' },
      { method: 'rename', indexes: [0, 1] },
      { method: 'renameSync', indexes: [0, 1] },
      { method: 'rmdir' },
      { method: 'rmdirSync' },
      { method: 'symlink', indexes: [0, 1] },
      { method: 'symlinkSync', indexes: [0, 1] },
      { method: 'truncate' },
      { method: 'truncateSync' },
      { method: 'unlink' },
      { method: 'unlinkSync' },
      { method: 'writeFile' },
      { method: 'writeFileSync' }
    ]);

    ModuleHook.resolve({ name: 'fs' }, (fs) => {
      fsMethods.forEach(({ method, indexes = [0] }) =>
        patcher.patch(fs, method, {
          name: 'fs',
          patchType: PATCH_TYPES.PROTECT_SINK,
          alwaysRun: true,
          pre: (wrapCtx) => {
            indexes.forEach((idx) => {
              emitSinkEvent(wrapCtx.args[idx], SINK_TYPES.FILE_PATH, ID);
            });
          }
        })
      );
    });

    return this;
  }
}

module.exports = FileSystemSensor;
