<commit_before>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ****************************************************************************/

/**
 * @file
 * @brief IronBee
 *
 * @author <NAME> <<EMAIL>>
 */

#include "ironbee_config_auto.h"

#include "rule_engine_private.h"

#include <ironbee/action.h>
#include <ironbee/bytestr.h>
#include <ironbee/core.h>
#include <ironbee/debug.h>
#include <ironbee/escape.h>
#include <ironbee/field.h>
#include <ironbee/mpool.h>
#include <ironbee/operator.h>
#include <ironbee/rule_engine.h>
#include <ironbee/transformation.h>
#include <ironbee/util.h>

#include <assert.h>
#include <inttypes.h>

/**
 * Prefix used for all rule engine log entries.
 */
//static const char *LOG_PREFIX = "RULE_ENG";

/**
 * Length of buffer used for formatting fields
 */
static const size_t MAX_FIELD_BUF = 64;
static const size_t PREFIX_BUFSIZE = 32;
static const size_t REV_BUFSIZE = 16;

/**
 * If any of these flags are set, enable rule logging
 */
#define RULE_LOG_FLAG_RULE_ENABLE                            \
    ( IB_RULE_LOG_FLAG_RULE |                                \
      IB_RULE_LOG_FLAG_TARGET |                              \
      IB_RULE_LOG_FLAG_TFN |                                 \
      IB_RULE_LOG_FLAG_OPERATOR |                            \
      IB_RULE_LOG_FLAG_ACTION |                              \
      IB_RULE_LOG_FLAG_EVENT |                               \
      IB_RULE_LOG_FLAG_AUDIT |                               \
      IB_RULE_LOG_FLAG_TIMING )

/**
 * If any of these flags are set, enable target gathering
 */
#define RULE_LOG_FLAG_TARGET_ENABLE                  \
    ( IB_RULE_LOG_FLAG_TARGET |                      \
      IB_RULE_LOG_FLAG_TFN |                         \
      IB_RULE_LOG_FLAG_OPERATOR |                    \
      IB_RULE_LOG_FLAG_ACTION )

/**
 * If any of these flags are set, enable result gathering
 */
#define RULE_LOG_FLAG_RESULT_ENABLE                  \
    ( IB_RULE_LOG_FLAG_TARGET |                      \
      IB_RULE_LOG_FLAG_OPERATOR |                    \
      IB_RULE_LOG_FLAG_ACTION )

/**
 * Mapping of valid rule logging names to flag values.
 */
static IB_STRVAL_MAP(flags_map) = {
    IB_STRVAL_PAIR("tx", IB_RULE_LOG_FLAG_TX),
    IB_STRVAL_PAIR("requestLine", IB_RULE_LOG_FLAG_REQ_LINE),
    IB_STRVAL_PAIR("requestHeader", IB_RULE_LOG_FLAG_REQ_HEADER),
    IB_STRVAL_PAIR("requestBody", IB_RULE_LOG_FLAG_REQ_BODY),
    IB_STRVAL_PAIR("responseLine", IB_RULE_LOG_FLAG_RSP_LINE),
    IB_STRVAL_PAIR("responseHeader", IB_RULE_LOG_FLAG_RSP_HEADER),
    IB_STRVAL_PAIR("responseBody", IB_RULE_LOG_FLAG_RSP_BODY),
    IB_STRVAL_PAIR("phase", IB_RULE_LOG_FLAG_PHASE),
    IB_STRVAL_PAIR("rule", IB_RULE_LOG_FLAG_RULE),
    IB_STRVAL_PAIR("target", IB_RULE_LOG_FLAG_TARGET),
    IB_STRVAL_PAIR("transformation", IB_RULE_LOG_FLAG_TFN),
    IB_STRVAL_PAIR("operator", IB_RULE_LOG_FLAG_OPERATOR),
    IB_STRVAL_PAIR("action", IB_RULE_LOG_FLAG_ACTION),
    IB_STRVAL_PAIR("event", IB_RULE_LOG_FLAG_EVENT),
    IB_STRVAL_PAIR("audit", IB_RULE_LOG_FLAG_AUDIT),
    IB_STRVAL_PAIR("timing", IB_RULE_LOG_FLAG_TIMING),

    IB_STRVAL_PAIR("allRules", IB_RULE_LOG_FLAG_MODE_ALL),
    IB_STRVAL_PAIR("actionableRulesOnly", IB_RULE_LOG_FLAG_MODE_ACT),
    IB_STRVAL_PAIR("operatorErrorOnly", IB_RULE_LOG_FLAG_MODE_ERROR),
    IB_STRVAL_PAIR("returnedTrueOnly", IB_RULE_LOG_FLAG_MODE_TRUE),
    IB_STRVAL_PAIR("returnedFalseOnly", IB_RULE_LOG_FLAG_MODE_FALSE),

    /* End */
    IB_STRVAL_PAIR_LAST
};

/**
 * Generic Logger for rules.
 *
 * @warning There is currently a 1024 byte formatter limit when prefixing the
 *          log header data.
 *
 * @param[in] rule_log_level Rule debug log level
 * @param[in] log_level Log level to log at
 * @param[in] tx Transaction information
 * @param[in] rule Rule to log (or NULL)
 * @param[in] target Rule target (or NULL)
 * @param[in] file Filename (or NULL)
 * @param[in] line Line number (or 0)
 * @param[in] fmt Printf-like format string
 * @param[in] ap Argument list
 *
 * @note: Separate prototype for the format attribute handling
 */
static void rule_vlog_tx(
    ib_rule_dlog_level_t rule_log_level,
    ib_log_level_t log_level,
    const ib_tx_t *tx,
    const ib_rule_t *rule,
    const ib_rule_target_t *target,
    const char *file,
    int line,
    const char *fmt,
    va_list ap
) VPRINTF_ATTRIBUTE(8);

static void rule_vlog_tx(
    ib_rule_dlog_level_t rule_log_level,
    ib_log_level_t log_level,
    const ib_tx_t *tx,
    const ib_rule_t *rule,
    const ib_rule_target_t *target,
    const char *file,
    int line,
    const char *fmt,
    va_list ap
)
{
    IB_FTRACE_INIT();
    char *fmtbuf = NULL;
    size_t fmtlen = PREFIX_BUFSIZE + 1;
    void *freeptr = NULL;
    ib_rule_dlog_level_t dlog_level =
        (tx->ctx == NULL) ? IB_RULE_DLOG_INFO : ib_rule_dlog_level(tx->ctx);

    /* Ignore this message? */
    if (rule_log_level > dlog_level) {
        IB_FTRACE_RET_VOID();
    }

    /* Build a new format buffer with rule ID and target field name */

    /* Calculate the prefix length */
    if (rule != NULL) {
        fmtlen += strlen(rule->meta.id) + 16;
    }
    else {
        fmtlen += 4;
    }

    /* Using the length, build a new format buffer */<commit_msg>Hopefully fixed a buffer overflow in my previous commit<commit_after>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ****************************************************************************/

/**
 * @file
 * @brief IronBee
 *
 * @author <NAME> <<EMAIL>>
 */

#include "ironbee_config_auto.h"

#include "rule_engine_private.h"

#include <ironbee/action.h>
#include <ironbee/bytestr.h>
#include <ironbee/core.h>
#include <ironbee/debug.h>
#include <ironbee/escape.h>
#include <ironbee/field.h>
#include <ironbee/mpool.h>
#include <ironbee/operator.h>
#include <ironbee/rule_engine.h>
#include <ironbee/transformation.h>
#include <ironbee/util.h>

#include <assert.h>
#include <inttypes.h>

/**
 * Prefix used for all rule engine log entries.
 */
//static const char *LOG_PREFIX = "RULE_ENG";

/**
 * Length of buffer used for formatting fields
 */
static const size_t MAX_FIELD_BUF = 64;
static const size_t PREFIX_BUFSIZE = 32;
static const size_t REV_BUFSIZE = 16;

/**
 * If any of these flags are set, enable rule logging
 */
#define RULE_LOG_FLAG_RULE_ENABLE                            \
    ( IB_RULE_LOG_FLAG_RULE |                                \
      IB_RULE_LOG_FLAG_TARGET |                              \
      IB_RULE_LOG_FLAG_TFN |                                 \
      IB_RULE_LOG_FLAG_OPERATOR |                            \
      IB_RULE_LOG_FLAG_ACTION |                              \
      IB_RULE_LOG_FLAG_EVENT |                               \
      IB_RULE_LOG_FLAG_AUDIT |                               \
      IB_RULE_LOG_FLAG_TIMING )

/**
 * If any of these flags are set, enable target gathering
 */
#define RULE_LOG_FLAG_TARGET_ENABLE                  \
    ( IB_RULE_LOG_FLAG_TARGET |                      \
      IB_RULE_LOG_FLAG_TFN |                         \
      IB_RULE_LOG_FLAG_OPERATOR |                    \
      IB_RULE_LOG_FLAG_ACTION )

/**
 * If any of these flags are set, enable result gathering
 */
#define RULE_LOG_FLAG_RESULT_ENABLE                  \
    ( IB_RULE_LOG_FLAG_TARGET |                      \
      IB_RULE_LOG_FLAG_OPERATOR |                    \
      IB_RULE_LOG_FLAG_ACTION )

/**
 * Mapping of valid rule logging names to flag values.
 */
static IB_STRVAL_MAP(flags_map) = {
    IB_STRVAL_PAIR("tx", IB_RULE_LOG_FLAG_TX),
    IB_STRVAL_PAIR("requestLine", IB_RULE_LOG_FLAG_REQ_LINE),
    IB_STRVAL_PAIR("requestHeader", IB_RULE_LOG_FLAG_REQ_HEADER),
    IB_STRVAL_PAIR("requestBody", IB_RULE_LOG_FLAG_REQ_BODY),
    IB_STRVAL_PAIR("responseLine", IB_RULE_LOG_FLAG_RSP_LINE),
    IB_STRVAL_PAIR("responseHeader", IB_RULE_LOG_FLAG_RSP_HEADER),
    IB_STRVAL_PAIR("responseBody", IB_RULE_LOG_FLAG_RSP_BODY),
    IB_STRVAL_PAIR("phase", IB_RULE_LOG_FLAG_PHASE),
    IB_STRVAL_PAIR("rule", IB_RULE_LOG_FLAG_RULE),
    IB_STRVAL_PAIR("target", IB_RULE_LOG_FLAG_TARGET),
    IB_STRVAL_PAIR("transformation", IB_RULE_LOG_FLAG_TFN),
    IB_STRVAL_PAIR("operator", IB_RULE_LOG_FLAG_OPERATOR),
    IB_STRVAL_PAIR("action", IB_RULE_LOG_FLAG_ACTION),
    IB_STRVAL_PAIR("event", IB_RULE_LOG_FLAG_EVENT),
    IB_STRVAL_PAIR("audit", IB_RULE_LOG_FLAG_AUDIT),
    IB_STRVAL_PAIR("timing", IB_RULE_LOG_FLAG_TIMING),

    IB_STRVAL_PAIR("allRules", IB_RULE_LOG_FLAG_MODE_ALL),
    IB_STRVAL_PAIR("actionableRulesOnly", IB_RULE_LOG_FLAG_MODE_ACT),
    IB_STRVAL_PAIR("operatorErrorOnly", IB_RULE_LOG_FLAG_MODE_ERROR),
    IB_STRVAL_PAIR("returnedTrueOnly", IB_RULE_LOG_FLAG_MODE_TRUE),
    IB_STRVAL_PAIR("returnedFalseOnly", IB_RULE_LOG_FLAG_MODE_FALSE),

    /* End */
    IB_STRVAL_PAIR_LAST
};

/**
 * Generic Logger for rules.
 *
 * @warning There is currently a 1024 byte formatter limit when prefixing the
 *          log header data.
 *
 * @param[in] rule_log_level Rule debug log level
 * @param[in] log_level Log level to log at
 * @param[in] tx Transaction information
 * @param[in] rule Rule to log (or NULL)
 * @param[in] target Rule target (or NULL)
 * @param[in] file Filename (or NULL)
 * @param[in] line Line number (or 0)
 * @param[in] fmt Printf-like format string
 * @param[in] ap Argument list
 *
 * @note: Separate prototype for the format attribute handling
 */
static void rule_vlog_tx(
    ib_rule_dlog_level_t rule_log_level,
    ib_log_level_t log_level,
    const ib_tx_t *tx,
    const ib_rule_t *rule,
    const ib_rule_target_t *target,
    const char *file,
    int line,
    const char *fmt,
    va_list ap
) VPRINTF_ATTRIBUTE(8);

static void rule_vlog_tx(
    ib_rule_dlog_level_t rule_log_level,
    ib_log_level_t log_level,
    const ib_tx_t *tx,
    const ib_rule_t *rule,
    const ib_rule_target_t *target,
    const char *file,
    int line,
    const char *fmt,
    va_list ap
)
{
    IB_FTRACE_INIT();
    char *fmtbuf = NULL;
    size_t fmtlen = PREFIX_BUFSIZE + 1;
    void *freeptr = NULL;
    ib_rule_dlog_level_t dlog_level =
        (tx->ctx == NULL) ? IB_RULE_DLOG_INFO : ib_rule_dlog_level(tx->ctx);

    /* Ignore this message? */
    if (rule_log_level > dlog_level) {
        IB_FTRACE_RET_VOID();
    }

    /* Build a new format buffer with rule ID and target field name */

    /* Calculate the prefix length */
    if (rule != NULL) {
        fmtlen += strlen(rule->meta.id) + 24;
    }
    else {
        fmtlen += 8;
    }

    /* Using the length, build a new format buffer */
{'fraction_non_alphanumeric': 0.11257189811010682, 'fraction_numerical': 0.003286770747740345, 'mean_word_length': 3.063772954924875, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5354690', 'n_tokens_mistral': 4271, 'n_tokens_neox': 4037, 'n_words': 956}