/**
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';

const {
  SINK_TYPES,
  IMPORTANCE,
  CMD_INJECTION_SEMANTIC_TYPES
} = require('../../../constants');
const Rule = require('../');
const UserInputFactory = require('../../../reporter/models/utils/user-input-factory');
const DangerousPathsScanner = require('./dangerous-paths-scanner');

class CMDInjectionSemanticDangerousPathsRule extends Rule {
  constructor(policy, agent) {
    super(policy, agent);
    this.id = 'cmd-injection-semantic-dangerous-paths';
    this.name = 'Command Injection Dangerous Paths';
    this.applicableSinks = [SINK_TYPES.COMMAND];
  }
  /**
   * By returning `false` this rule will bypass input classification stage.
   * @returns {Boolean}
   */
  appliesToInputType() {
    return false;
  }

  /**
   * @param {ApplicationContext} event Sink event
   * @param {Request} request Request model
   * @param {Samples} samples Samples cache
   */
  evaluateAtSink({ event, request, samples }) {
    const { data: command } = event;
    const scanner = new DangerousPathsScanner({ command });
    const input = scanner.containsDangerousPath(command);
    if (input) {
      const sample = this.createAndSaveSample({
        input: UserInputFactory.makeOne({ value: command }),
        attributes: { effective: true },
        classification: IMPORTANCE.DEFINITE,
        event,
        samples,
        request
      });

      this.appendAttackDetails(sample, {
        command,
        findings: [CMD_INJECTION_SEMANTIC_TYPES.PATH_ARGUMENT]
      });

      this.blockRequest(sample);
    }
  }

  /**
   * Check for dangerous paths in the input command using the agent library.
   * @param {ApplicationContext} event Sink event
   * @param {Request} request Request model
   * @param {Samples} samples Samples cache
   */
  evaluateAtSinkForLib({ event, request, samples }) {
    const { data: command } = event;
    const containsDangerousPath = this.agent.agentLib.containsDangerousPath(
      command
    );
    if (containsDangerousPath) {
      const sample = this.createAndSaveSample({
        input: UserInputFactory.makeOne({ value: command }),
        attributes: { effective: true },
        classification: IMPORTANCE.DEFINITE,
        event,
        samples,
        request
      });

      this.appendAttackDetails(sample, {
        command,
        findings: [CMD_INJECTION_SEMANTIC_TYPES.PATH_ARGUMENT]
      });

      this.blockRequest(sample);
    }
  }

  /**
   * Builds the details for TS UI rendering.
   * @param   {Sample} sample   N/a in this rule's case
   * @param   {Object} sinkData
   * @param   {String} sinkData.command The command string
   * @param   {String[]} sinkData.findings Path arg flag
   * @returns {Object}
   */
  buildDetails(sample, { command, findings }) {
    return { command, findings };
  }
}

module.exports = CMDInjectionSemanticDangerousPathsRule;
