/**
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 { INPUT_TYPES } = require('../../constants');
const {
  emitSourceEvent,
  emitMultipartNameSource,
  getRawRequest
} = require('../../hooks/frameworks/common');
const agentEmitter = require('../../agent-emitter');
const {
  constants: { EVENTS }
} = require('../../core/hapi');

class HapiProtectSources {
  constructor() {
    this.id = 'hapi';
    this.registerCookieHandler();
    this.registerSourceHandler();
    this.registerMultipartHandler();
  }

  /**
   * Registers a handler that will emit all the source events for hapi
   * when the onPreHandler hook point in hapi occurs
   */
  registerSourceHandler(request, reply) {
    agentEmitter.on(EVENTS.HAPI_PRE_HANDLER, ({ request, h }) => {
      /**
       * We can get at all of this data sooner, however this is the first
       * extension point called after validation. The default validation behavior
       * is to return a 400. We are not sure until after validation whether any of these
       * values will be used.
       */
      emitSourceEvent(
        request,
        getRawRequest(request),
        getRawRequest(h),
        'payload',
        INPUT_TYPES.BODY,
        this.id
      );

      emitSourceEvent(
        request,
        getRawRequest(request),
        getRawRequest(h),
        'query',
        INPUT_TYPES.QUERYSTRING,
        this.id
      );
      emitSourceEvent(
        request,
        getRawRequest(request),
        getRawRequest(h),
        'params',
        INPUT_TYPES.URL_PARAMETER,
        this.id
      );
    });
  }

  registerCookieHandler() {
    agentEmitter.on(EVENTS.HAPI_PRE_AUTH, (request, h) => {
      emitSourceEvent(
        request,
        getRawRequest(request),
        getRawRequest(h),
        'state',
        INPUT_TYPES.COOKIE_VALUE,
        this.id
      );
    });
  }

  registerMultipartHandler() {
    agentEmitter.on(EVENTS.HAPI_MULTI_PART, (args) => {
      const [eventName, part] = args;
      // note: this will emit multiple file upload events during larger files.
      //       given the domain of this as being part of unsafe-file-upload (which is BAP)
      //       this isn't much of a concern.
      if (eventName == 'part') {
        emitMultipartNameSource({
          id: 'pez',
          name: part.filename
        });
      }
    });
  }
}

module.exports = HapiProtectSources;
