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

/**
 * @module lib/rules/protect/nosqli/nosql-scanner/MongoDBScanner
 * Tokenize MongoDB queries and searches for injections.
 */

const { BaseScanner } = require('../../base-scanner');

module.exports = class MongoDBScanner extends BaseScanner {
  /**
   * Whether the cursor is on dot operator.
   * @param   {ScanState} state   The current scan state of the parser.
   * @returns {Boolean}          Whether the character is an '.' operator
   */
  isDotOperator({ codeString: query, index: idx }) {
    return query && query[idx + 1] === '.';
  }

  /**
   * Overloading because we have additional operators in the language.
   * @param   {ScanState} state   The current scan state of the parser.
   * @returns {Boolean}          Whether the character is an operator
   */
  isOperator(state) {
    return super.isOperator(state) || this.isDotOperator(state);
  }

  /**
   * Whether the analysis at the current state suggests that
   * the next token is the start of a line comment;
   * @param   {ScanState} state   The current scan state of the parser.
   * @returns {Boolean}          Whether next token is line comment start
   */
  isLineCommentStart({ codeString: query, index: idx }) {
    if (/<--/.test(query)) {
      return true;
    }
    if (query && query[idx + 1] === '/' && query[idx + 2] === '/') {
      return true;
    }

    return false;
  }

  /**
   * Whether the analysis at the current state suggests that
   * the next token is the start of a block comment.
   * @param   {ScanState} current   The current scan state of the parser.
   * @returns {Boolean}          Whether next token is block comment start
   */
  isBlockCommentStart(current) {
    return false;
  }

  /**
   * Whether a single quote can be an escape character within a single quote.
   * @returns {Boolean} true by default.
   */
  canSingleQuoteEscapeInSingleQuote() {
    return false;
  }

  /**
   * Whether a double quote can be an escape character within a double quote.
   * @returns {Boolean} false by default.
   */
  canDoubleQuoteEscapeInDoubleQuote() {
    return false;
  }
};
