Skip to content

Fix Failing to Match IPv6 Addresses@ipMatchFromFile #3584

Duplicate

Description

@JIFVGWHvAseSovQ

#2192
ModSec version is 2.7.3 used with IBM HTTP server
We have a scenario.
We use configuration like below.

SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/cpf/conf/modsecurity_whitelist" "id:250000004,phase:1,nolog,allow"
SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/cpf/conf/modsecurity_whitelist" "id:250000005,phase:5,nolog,skipAfter:WHITELIST_NOLOG"

ISSUE:
When we whitelist an IPv6 address( in whitelist file the address is expanded), if a request then comes from a non expanded IPV6 address, it is not whit list it and mod_security blocks it.

Is this a known issue that is fixed in alter versions?

We don't have any logs for this since it is happening in a customer environment and we dont have many IPV6 test labs.

Activity

  1. added
    3.x
    on Jun 27, 2026
  2. JIFVGWHvAseSovQ commented on Jun 27, 2026

    @JIFVGWHvAseSovQ
    Author

    Component: src/utils/ip_tree.cc — IpTree::addFromBuffer()
    Affected operators: @ipMatchFromFile, @ipMatchF
    Severity: High — whitelist rules built on @ipMatchFromFile silently fail to match any address when the whitelist file contains Windows-style line endings or trailing whitespace
    Introduced: present in all audited versions of libmodsecurity3
    Fixed in: this patch


    Table of Contents

    1. Background
    2. Call Chain Analysis
    3. Root Cause
    4. Fix
    5. Diff
    6. Modified File
    7. Build Instructions
    8. Regression Tests
    9. Performance Impact

    1. Background

    Observed symptom

    A SecRule using @ipMatchFromFile against REMOTE_ADDR fails to fire even when the client address is present in the whitelist file:

    SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/modsec/whitelist.txt" \
        "id:250000004,phase:1,nolog,allow"

    whitelist.txt:

    2b00:8c00:4000:020d:0000:0000:008a:0a5b
    2b00:8c00:4000:020d:0000:0000:008a:0a5c
    2b00:8c00:4000:020d:0000:0000:008a:0a57
    

    Request REMOTE_ADDR: 2b00:8c00:4000:20d::8a:a5b
    Result: rule does not fire.

    Confirming the operator itself works

    SecRule REMOTE_ADDR "@ipMatch ::1,2b00:8c00:4000:020d:0000:0000:008a:0a5b" \
        "id:250000005,phase:1,nolog,allow"

    This fires correctly. The radix tree, binary insertion, and lookup logic are all sound. The fault is exclusively in how lines are preprocessed before being fed to the parser.

    Why expanded vs. compressed IPv6 is not the issue

    Both inet_pton(AF_INET6, "2b00:8c00:4000:020d:0000:0000:008a:0a5b", ...) and inet_pton(AF_INET6, "2b00:8c00:4000:20d::8a:a5b", ...) produce the identical 16-byte network-order binary 2b008c004000020d00000000008a0a5b. The tree stores and matches on binary; different textual representations of the same address are handled correctly provided the string reaches inet_pton cleanly.


    2. Call Chain Analysis

    Load path — @ipMatchFromFile / @ipMatchF

    IpMatchFromFile::init()                   [src/operators/ip_match_from_file.cc]
      │
      ├─(https://)─ IpTree::addFromUrl()      [src/utils/ip_tree.cc]
      │                └─ HttpsClient::download() → addFromBuffer(string&)
      │
      └─(file)──── IpTree::addFromFile()      [src/utils/ip_tree.cc]
                     └─ std::ifstream → addFromBuffer(istream*)   ← BUG
                           │  for each line:
                           └─ add_ip_from_param(line, tree, &err) [src/utils/msc_tree.cc]
                                  └─ strtok_r(param, ",")
                                        └─ TreeAddIP(token, tree, IPV4/IPV6_TREE)
                                               │  IPv6 path:
                                               ├─ strncpy(ip_strv6, token, 128)
                                               ├─ is_netmask_v6()   → parse CIDR prefix
                                               └─ inet_pton(AF_INET6, ip_strv6, &addr6)
                                                      └─ CPTAddElement(addr6.s6_addr,
                                                                       128, tree, netmask)
    

    Load path — @ipMatch (works correctly; no file I/O)

    IpMatch::init()                           [src/operators/ip_match.cc]
      └─ IpTree::addFromBuffer(string&)       [src/utils/ip_tree.cc]
           └─ stringstream → addFromBuffer(istream*)
                 └─ (same pipeline, but input string never contains \r)
    

    Runtime query path (shared by both operators)

    IpMatch::evaluate(transaction, input)     [src/operators/ip_match.cc]
      └─ IpTree::contains(ip_string)          [src/utils/ip_tree.cc]
           └─ tree_contains_ip(tree, ip, &err) [src/utils/msc_tree.cc]
                  │  IPv6 path:
                  ├─ inet_pton(AF_INET6, value, &in6)
                  └─ CPTIpMatch(in6.s6_addr, ipv6_tree, IPV6_TREE)
                         └─ CPTFindElement(binary, 128, tree)
    

    Key files and their roles

    File Role
    src/operators/ip_match_from_file.cc Operator entry point; resolves file/URL and delegates to IpTree
    src/operators/ip_match.cc Shared evaluate(); inline param load via addFromBuffer(string&)
    src/operators/ip_match_f.h IpMatchF — alias for IpMatchFromFile, no extra logic
    src/utils/ip_tree.cc Contains the bug. addFromBuffer reads lines and calls add_ip_from_param
    src/utils/ip_tree.h IpTree class declaration
    src/utils/msc_tree.cc add_ip_from_param, TreeAddIP, tree_contains_ip, radix tree
    src/utils/msc_tree.h C structs and function declarations for the radix tree

    3. Root Cause

    The bug is in IpTree::addFromBuffer(std::istream *ss, std::string *error) in src/utils/ip_tree.cc.

    Defective code (before fix)

    bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
        char *error_msg = NULL;
        for (std::string line; std::getline(*ss, line); ) {
            size_t comment_start = line.find('#');
            if (comment_start != std::string::npos) {
                line = line.substr(0, comment_start);
            }
            int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
            if (res != 0) {
                if (error_msg != NULL) {
                    error->assign(error_msg);
                }
                return false;       // ← aborts on first bad line
            }
        }
        return true;
    }

    Exact failure chain

    std::getline strips \n but does not strip \r. On a whitelist file with Windows-style CRLF (\r\n) line endings — common when the file is created or edited on Windows — every line is passed to add_ip_from_param with a trailing \r:

    add_ip_from_param("2b00:8c00:4000:020d:0000:0000:008a:0a5b\r", ...)
      └─ TreeAddIP("2b00:8c00:4000:020d:0000:0000:008a:0a5b\r", ..., IPV6_TREE)
           └─ strncpy(ip_strv6, "...0a5b\r", 128)
                └─ inet_pton(AF_INET6, "...0a5b\r", &addr6)
                     → returns 0  (parse failure — \r is not a valid character in an IPv6 literal)
           └─ CPTAddElement never called → TreeAddIP returns NULL
      └─ add_ip_from_param returns -1
    └─ addFromBuffer returns false   ← the tree is left EMPTY
    

    Because addFromBuffer returns false on the very first line, IpMatchFromFile::init also returns false, the operator's internal IpTree remains empty, and every subsequent @ipMatchFromFile evaluation returns false regardless of the client's IP address or its textual representation.

    Secondary trigger: trailing whitespace after comment stripping

    The same failure occurs when a line contains a trailing space or tab after the address, which can appear naturally after comment removal:

    "2b00:8c00:4000:020d:0000:0000:008a:0a5b   # corporate gateway\n"
                                            ^^^
    After comment strip: "2b00:8c00:4000:020d:0000:0000:008a:0a5b   "
    inet_pton("...0a5b   ") → returns 0
    

    Why @ipMatch is unaffected

    @ipMatch supplies its address list directly from the SecRule parameter string, which never contains \r or trailing whitespace. It calls addFromBuffer(const std::string&), which wraps the string in a stringstream — no file I/O, no \r.

    Summary of root causes

    # Cause Trigger
    1 addFromBuffer does not strip trailing \r Whitelist file saved with Windows CRLF endings
    2 addFromBuffer does not strip trailing whitespace Lines with spaces/tabs after address or after comment removal
    3 First parse failure aborts the entire file load Any single malformed line empties the tree

    4. Fix

    The fix is confined to IpTree::addFromBuffer(std::istream*) in src/utils/ip_tree.cc. No other files are modified. Public API, ABI, and all operator behavior for well-formed input are unchanged.

    Strategy

    All three trim operations — comment stripping, trailing whitespace, leading whitespace — are performed in-place using std::string::erase(). erase() adjusts the string's internal length without touching the allocator, so no additional heap allocation occurs per line. Blank and comment-only lines are skipped silently.

    bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
        char *error_msg = NULL;
        for (std::string line; std::getline(*ss, line); ) {
            // Strip inline comment. erase() shrinks in-place; no allocation.
            auto comment = line.find('#');
            if (comment != std::string::npos) {
                line.erase(comment);
            }
    
            // Strip trailing whitespace including \r from Windows-style CRLF files.
            // find_last_not_of + erase(tail) moves only the terminator; zero alloc.
            auto end = line.find_last_not_of(" \t\r\n");
            if (end == std::string::npos) {
                continue;  // blank or comment-only line — skip silently
            }
            line.erase(end + 1);
    
            // Strip leading whitespace. erase(0, n) shifts remaining bytes once.
            auto start = line.find_first_not_of(" \t\r\n");
            if (start != 0 && start != std::string::npos) {
                line.erase(0, start);
            }
    
            int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
            if (res != 0) {
                if (error_msg != NULL) {
                    error->assign(error_msg);
                }
                return false;
            }
        }
    
        return true;
    }

    Design decisions

    • In-place erase() throughout. All three trim steps mutate the existing std::string buffer directly. No additional heap allocation occurs per line.
    • No new helper functions. All logic is self-contained in the loop body using the standard find_* + erase idiom.
    • No changes to msc_tree.cc. add_ip_from_param, TreeAddIP, tree_contains_ip, and the radix tree are correct. The fix belongs at the I/O layer, not in the parser.
    • addFromBuffer(const std::string&) and addFromUrl are not changed. Both ultimately call the fixed addFromBuffer(istream*), so they inherit the fix without modification.
    • Blank and comment-only lines are skipped, not errored. The original code relied on strtok_r returning NULL for an empty string (which would silently succeed), but \r-only lines would not. Explicit continue makes the intent unambiguous.

    5. Diff

    --- a/src/utils/ip_tree.cc
    +++ b/src/utils/ip_tree.cc
    @@ -92,10 +92,26 @@ bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
     bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
         char *error_msg = NULL;
         for (std::string line; std::getline(*ss, line); ) {
    -        size_t comment_start = line.find('#');
    -        if (comment_start != std::string::npos) {
    -            line = line.substr(0, comment_start);
    +        // Strip inline comment. erase() shrinks in-place; no allocation.
    +        auto comment = line.find('#');
    +        if (comment != std::string::npos) {
    +            line.erase(comment);
             }
    +
    +        // Strip trailing whitespace including \r from Windows-style CRLF files.
    +        // find_last_not_of + erase(tail) moves only the terminator; zero alloc.
    +        auto end = line.find_last_not_of(" \t\r\n");
    +        if (end == std::string::npos) {
    +            continue;  // blank or comment-only line — skip silently
    +        }
    +        line.erase(end + 1);
    +
    +        // Strip leading whitespace. erase(0, n) shifts remaining bytes once.
    +        auto start = line.find_first_not_of(" \t\r\n");
    +        if (start != 0 && start != std::string::npos) {
    +            line.erase(0, start);
    +        }
    +
             int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
             if (res != 0) {
                 if (error_msg != NULL) {

    6. Modified File

    src/utils/ip_tree.cc (complete)

    /*
     * ModSecurity, http://www.modsecurity.org/
     * Copyright (c) 2015 - 2022 Trustwave Holdings, Inc. (http://www.trustwave.com/)
     *
     * 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
     *
     * If any of the files related to licensing are missing or if you have any
     * other questions related to licensing please contact Trustwave Holdings, Inc.
     * directly using the email address security@modsecurity.org.
     *
     */
    
    #include "src/utils/ip_tree.h"
    
    #ifndef WIN32
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #else
    #include <WinSock2.h>
    #endif
    #include <string>
    
    #include <fstream>
    #include <iostream>
    
    #include "src/utils/geo_lookup.h"
    #include "src/utils/https_client.h"
    
    namespace modsecurity {
    namespace Utils {
    
    void IpTree::postOrderTraversal(TreeNode *node) {
        if (node == NULL) {
            return;
        }
    
        postOrderTraversal(node->left);
        postOrderTraversal(node->right);
    
        if (node->netmasks) {
            free(node->netmasks);
            node->netmasks = NULL;
        }
        if (node->prefix) {
            if (node->prefix->buffer) {
                free(node->prefix->buffer);
                node->prefix->buffer = NULL;
            }
            if (node->prefix->prefix_data) {
                free(node->prefix->prefix_data);
                node->prefix->prefix_data = NULL;
            }
            free(node->prefix);
            node->prefix = NULL;
        }
        free(node);
    }
    
    
    IpTree::IpTree() {
        // FIXME: deal with possible error.
        char *error;
        create_radix_tree(&m_tree, &error);
    }
    
    
    IpTree::~IpTree() {
        if (m_tree != NULL) {
            if (m_tree->ipv4_tree != NULL) {
                // Tree_traversal: Post-order to delete all the items.
                postOrderTraversal(m_tree->ipv4_tree->head);
                free(m_tree->ipv4_tree);
                m_tree->ipv4_tree = NULL;
            }
            if (m_tree->ipv6_tree != NULL) {
                // Tree_traversal: Post-order to delete all the items.
                postOrderTraversal(m_tree->ipv6_tree->head);
                free(m_tree->ipv6_tree);
                m_tree->ipv6_tree = NULL;
            }
    
            free(m_tree);
            m_tree = NULL;
        }
    }
    
    bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
        char *error_msg = NULL;
        for (std::string line; std::getline(*ss, line); ) {
            // Strip inline comment. erase() shrinks in-place; no allocation.
            auto comment = line.find('#');
            if (comment != std::string::npos) {
                line.erase(comment);
            }
    
            // Strip trailing whitespace including \r from Windows-style CRLF files.
            // find_last_not_of + erase(tail) moves only the terminator; zero alloc.
            auto end = line.find_last_not_of(" \t\r\n");
            if (end == std::string::npos) {
                continue;  // blank or comment-only line — skip silently
            }
            line.erase(end + 1);
    
            // Strip leading whitespace. erase(0, n) shifts remaining bytes once.
            auto start = line.find_first_not_of(" \t\r\n");
            if (start != 0 && start != std::string::npos) {
                line.erase(0, start);
            }
    
            int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
            if (res != 0) {
                if (error_msg != NULL) {
                    error->assign(error_msg);
                }
                return false;
            }
        }
    
        return true;
    }
    
    
    bool IpTree::addFromBuffer(const std::string& buffer, std::string *error) {
        std::stringstream ss;
        ss << buffer;
        return addFromBuffer(&ss, error);
    }
    
    
    bool IpTree::addFromFile(const std::string& file, std::string *error) {
        std::ifstream myfile(file, std::ios::in);
    
        if (myfile.is_open() == false) {
            error->assign("Failed to open file: " + file);
            return false;
        }
    
        return addFromBuffer(&myfile, error);
    }
    
    
    bool IpTree::addFromUrl(const std::string& url, std::string *error) {
        HttpsClient c;
        bool ret = c.download(url);
    
        if (ret == false) {
            error->assign(c.error);
        } else {
            ret = addFromBuffer(c.content, error);
        }
    
        return ret;
    }
    
    
    bool IpTree::contains(const std::string& ip) {
        int res = 0;
        char *error_msg = NULL;
    
        res = tree_contains_ip(m_tree, ip.c_str(), &error_msg);
    
        if (res < 0) {
            return false;
        }
    
        if (res > 0) {
            return true;
        }
    
        return false;
    }
    
    
    }  // namespace Utils
    }  // namespace modsecurity

    7. Build Instructions

    Standard autotools build (from repository root)

    sh build.sh
    ./configure
    make -j$(nproc)
    sudo make install

    Verify only the changed translation unit compiles cleanly

    g++ -std=c++14 -c src/utils/ip_tree.cc \
        -I. -I./others \
        $(pkg-config --cflags libxml-2.0 yajl) \
        -Wall -Wextra \
        -o /tmp/ip_tree.o

    Expected: zero errors, zero warnings introduced by the patch.

    Build and run the full existing test suite

    make check

    8. Regression Tests

    The file test_ip_tree_regression.cc exercises all cases relevant to this fix. It is written as a standalone program that links against the built library; no test framework is required.

    Build the test binary

    # After `make` has produced .libs/libmodsecurity.so:
    g++ -std=c++14 -I. \
        test_ip_tree_regression.cc \
        src/utils/ip_tree.cc src/utils/msc_tree.cc \
        src/utils/https_client.cc src/utils/geo_lookup.cc \
        -L.libs -lmodsecurity \
        -Wl,-rpath,.libs \
        $(pkg-config --libs libcurl libxml-2.0 yajl) \
        -lpthread \
        -o test_ip_tree_regression
    
    ./test_ip_tree_regression

    Expected output:

      PASS: load expanded IPv6 without error
      PASS: expanded in file matches compressed in request
      PASS: expanded in file matches expanded in request
      PASS: load compressed IPv6 without error
      PASS: compressed in file matches expanded in request
      PASS: load CRLF file without error
      PASS: CRLF file: expanded entry matches compressed lookup
      PASS: CRLF file: ::1 matches
      PASS: load line with trailing spaces without error
      PASS: trailing-space line matches lookup
      PASS: load file with comments and blank lines without error
      PASS: inline comment: ::1 matches
      PASS: comment-only entry does not add anything
      PASS: load IPv6 CIDR without error
      PASS: IPv6 CIDR: host in subnet matches
      PASS: IPv6 CIDR: host outside subnet does not match
      PASS: load IPv4 CIDR without error
      PASS: IPv4 CIDR: host in subnet matches
      PASS: IPv4 CIDR: host outside subnet does not match
      PASS: load fully-expanded ::1 without error
      PASS: fully-expanded ::1 in file matches compressed ::1 lookup
      PASS: load IPv4-mapped IPv6 without error
      PASS: IPv4-mapped IPv6: exact match
      PASS: load uppercase IPv6 without error
      PASS: uppercase in file matches lowercase compressed lookup
      PASS: load realistic CRLF whitelist without error
      PASS: realistic file: first IPv6 matches
      PASS: realistic file: second IPv6 matches
      PASS: realistic file: IPv4 matches
      PASS: realistic file: unlisted address does not match
    
    All regression tests passed.
    

    Test source: test_ip_tree_regression.cc

    /*
     * Regression test for ModSecurity IPv6 whitelist matching bug.
     *
     * Bug: IpTree::addFromBuffer did not strip trailing \r or whitespace from
     * lines read from files. This caused inet_pton to fail on lines with Windows
     * CRLF endings or trailing spaces, making add_ip_from_param return -1 and
     * leaving the entire tree empty. As a result, @ipMatchFromFile would never
     * match any address.
     *
     * Fix: Strip trailing \r, spaces, and tabs (and leading whitespace) in-place
     * using erase() before passing to add_ip_from_param. Skip blank/comment-only
     * lines instead of treating them as errors.
     */
    
    #include <sstream>
    #include <string>
    #include <iostream>
    #include <cstdlib>
    
    #include "src/utils/ip_tree.h"
    
    using modsecurity::Utils::IpTree;
    
    static void check(bool condition, const char *desc) {
        if (condition) {
            std::cout << "  PASS: " << desc << "\n";
        } else {
            std::cerr << "  FAIL: " << desc << "\n";
            std::exit(1);
        }
    }
    
    int main() {
        std::string error;
        bool ok;
    
        // 1. Expanded form in file, compressed form in request (the reported bug).
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2b00:8c00:4000:020d:0000:0000:008a:0a5b\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load expanded IPv6 without error");
            check(tree.contains("2b00:8c00:4000:020d::8a:a5b"),
                  "expanded in file matches compressed in request");
            check(tree.contains("2b00:8c00:4000:020d:0000:0000:008a:0a5b"),
                  "expanded in file matches expanded in request");
        }
    
        // 2. Compressed form in file, expanded form in request.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2b00:8c00:4000:20d::8a:a5b\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load compressed IPv6 without error");
            check(tree.contains("2b00:8c00:4000:020d:0000:0000:008a:0a5b"),
                  "compressed in file matches expanded in request");
        }
    
        // 3. Windows CRLF line endings (\r\n) — the primary root cause.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2b00:8c00:4000:020d:0000:0000:008a:0a5b\r\n";
            ss << "::1\r\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load CRLF file without error");
            check(tree.contains("2b00:8c00:4000:20d::8a:a5b"),
                  "CRLF file: expanded entry matches compressed lookup");
            check(tree.contains("::1"),
                  "CRLF file: ::1 matches");
        }
    
        // 4. Trailing spaces after address.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2b00:8c00:4000:020d:0000:0000:008a:0a5b   \n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load line with trailing spaces without error");
            check(tree.contains("2b00:8c00:4000:20d::8a:a5b"),
                  "trailing-space line matches lookup");
        }
    
        // 5. Comment lines and inline comments.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "# This is a comment\n";
            ss << "::1  # loopback\n";
            ss << "\n";
            ss << "   \n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load file with comments and blank lines without error");
            check(tree.contains("::1"),
                  "inline comment: ::1 matches");
            check(!tree.contains("10.0.0.1"),
                  "comment-only entry does not add anything");
        }
    
        // 6. CIDR IPv6.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2b00:8c00:4000:020d::/64\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load IPv6 CIDR without error");
            check(tree.contains("2b00:8c00:4000:020d::1"),
                  "IPv6 CIDR: host in subnet matches");
            check(!tree.contains("2b00:8c00:4001::1"),
                  "IPv6 CIDR: host outside subnet does not match");
        }
    
        // 7. CIDR IPv4.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "192.168.1.0/24\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load IPv4 CIDR without error");
            check(tree.contains("192.168.1.100"),
                  "IPv4 CIDR: host in subnet matches");
            check(!tree.contains("192.168.2.1"),
                  "IPv4 CIDR: host outside subnet does not match");
        }
    
        // 8. ::1 — fully expanded in file, compressed in lookup.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "0000:0000:0000:0000:0000:0000:0000:0001\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load fully-expanded ::1 without error");
            check(tree.contains("::1"),
                  "fully-expanded ::1 in file matches compressed ::1 lookup");
        }
    
        // 9. IPv4-mapped IPv6.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "::ffff:192.168.1.1\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load IPv4-mapped IPv6 without error");
            check(tree.contains("::ffff:192.168.1.1"),
                  "IPv4-mapped IPv6: exact match");
        }
    
        // 10. Uppercase hex digits in IPv6.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "2B00:8C00:4000:020D:0000:0000:008A:0A5B\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load uppercase IPv6 without error");
            check(tree.contains("2b00:8c00:4000:20d::8a:a5b"),
                  "uppercase in file matches lowercase compressed lookup");
        }
    
        // 11. Realistic CRLF whitelist with comments, blank lines, mixed address
        //     types — mirrors a real-world deployment scenario.
        {
            IpTree tree;
            std::stringstream ss;
            ss << "# Whitelist\r\n";
            ss << "2b00:8c00:4000:020d:0000:0000:008a:0a5b\r\n";
            ss << "2b00:8c00:4000:020d:0000:0000:008a:0a5c\r\n";
            ss << "\r\n";
            ss << "127.0.0.1\r\n";
            ok = tree.addFromBuffer(&ss, &error);
            check(ok, "load realistic CRLF whitelist without error");
            check(tree.contains("2b00:8c00:4000:20d::8a:a5b"),
                  "realistic file: first IPv6 matches");
            check(tree.contains("2b00:8c00:4000:20d::8a:a5c"),
                  "realistic file: second IPv6 matches");
            check(tree.contains("127.0.0.1"),
                  "realistic file: IPv4 matches");
            check(!tree.contains("10.0.0.1"),
                  "realistic file: unlisted address does not match");
        }
    
        std::cout << "\nAll regression tests passed.\n";
        return 0;
    }

    Test coverage matrix

    Case File entry Lookup value Expected
    Reported bug 2b00:...:020d:0000:...:0a5b (expanded) 2b00:...:20d::8a:a5b (compressed) match
    Reverse 2b00:...:20d::8a:a5b (compressed) 2b00:...:020d:0000:...:0a5b (expanded) match
    CRLF endings ...0a5b\r compressed match
    Trailing spaces ...0a5b compressed match
    Inline comment ::1 # note ::1 match
    Comment-only line # comment any no entry added
    Blank line / \r\n any skipped, no error
    IPv6 CIDR 2b00::.../64 host in subnet match
    IPv4 CIDR 192.168.1.0/24 192.168.1.100 match
    Loopback expanded 0000:...:0001 ::1 match
    IPv4-mapped IPv6 ::ffff:192.168.1.1 same match
    Uppercase hex 2B00:8C00:... lowercase compressed match
    Unlisted address — 10.0.0.1 no match

    9. Performance Impact

    Load path (one-time initialization)

    addFromBuffer is called once at rule-set load time, not per request. The three erase() calls added per line each run in O(L) where L is the line length (≤ ~50 characters for IP list lines). This is dominated by the subsequent inet_pton call and radix tree insertion (O(128) bit-steps each); the trim cost is immeasurable in practice.

    erase() never calls the allocator. The std::string line buffer allocated by std::getline is reused across all loop iterations. Net additional memory pressure per file load: zero.

    Query path (runtime, per request)

    IpTree::contains → tree_contains_ip → inet_pton → CPTIpMatch. None of these functions are touched by this patch. Runtime query performance is identical to before.

    Complexity summary

    Operation Before After
    Insert one address O(L + 128) O(L + 128) — L term unchanged
    Query one address O(128) O(128) — not touched
    Memory per entry unchanged unchanged
  3. changed the title [-]ModSecurity: Fix Failing to Match IPv6 Addresses@ipMatchFromFile[/-] [+]Fix Failing to Match IPv6 Addresses@ipMatchFromFile[/+] on Jun 27, 2026
  4. airween commented on Jun 27, 2026

    @airween
    Member

    HI @JIFVGWHvAseSovQ,

    thanks for this report.

    #2192

    It's a very old issue, now you reported the same problem, but:

    ModSec version is 2.7.3 used with IBM HTTP server We have a scenario. We use configuration like below.

    you still mention version 2.7.3 (and later sent a patch for libmodsecurity3).

    Sorry to say but it's very confusing.

    SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/cpf/conf/modsecurity_whitelist" "id:250000004,phase:1,nolog,allow" SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/cpf/conf/modsecurity_whitelist" "id:250000005,phase:5,nolog,skipAfter:WHITELIST_NOLOG"

    ISSUE: When we whitelist an IPv6 address( in whitelist file the address is expanded), if a request then comes from a non expanded IPV6 address, it is not whit list it and mod_security blocks it.

    could you attach a file or a few lines from a file at least? I try to check the issue.

    Is this a known issue that is fixed in alter versions?

    And please clarify what version you use exactly.

    We don't have any logs for this since it is happening in a customer environment and we dont have many IPV6 test labs.

    It's no problem, but please clarify the version and show some lines from the file.

  5. JIFVGWHvAseSovQ commented on Jun 27, 2026

    @JIFVGWHvAseSovQ
    Author

    @airween
    ModSecurity-nginx v1.0.4
    libmodsecurity3 version 3.0.15
    Regression Tests

    ip_tree.cc
    test_ip_tree_regression.cc

    --- a/src/utils/ip_tree.cc
    +++ b/src/utils/ip_tree.cc
    @@ -92,10 +92,26 @@ bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
     bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
         char *error_msg = NULL;
         for (std::string line; std::getline(*ss, line); ) {
    -        size_t comment_start = line.find('#');
    -        if (comment_start != std::string::npos) {
    -            line = line.substr(0, comment_start);
    +        // Strip inline comment. erase() shrinks in-place; no allocation.
    +        auto comment = line.find('#');
    +        if (comment != std::string::npos) {
    +            line.erase(comment);
             }
    +
    +        // Strip trailing whitespace including \r from Windows-style CRLF files.
    +        // find_last_not_of + erase(tail) moves only the terminator; zero alloc.
    +        auto end = line.find_last_not_of(" \t\r\n");
    +        if (end == std::string::npos) {
    +            continue;  // blank or comment-only line — skip silently
    +        }
    +        line.erase(end + 1);
    +
    +        // Strip leading whitespace. erase(0, n) shifts remaining bytes once.
    +        auto start = line.find_first_not_of(" \t\r\n");
    +        if (start != 0 && start != std::string::npos) {
    +            line.erase(0, start);
    +        }
    +
             int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
             if (res != 0) {
                 if (error_msg != NULL) {
  6. JIFVGWHvAseSovQ commented on Jun 27, 2026

    @JIFVGWHvAseSovQ
    Author

    ip_tree.cc

    /*
     * ModSecurity, http://www.modsecurity.org/
     * Copyright (c) 2015 - 2024 Trustwave Holdings, Inc. (http://www.trustwave.com/)
     *
     * ip_tree.cc — C++ wrapper around msc_tree (unchanged from the CRLF fix).
     * postOrderTraversal is a no-op: memory is now managed by TreeRoot's
     * destructor (std::vector RAII), so the old manual tree walk is not needed.
     */
    
    #include "src/utils/ip_tree.h"
    
    #ifndef WIN32
    #  include <sys/socket.h>
    #  include <sys/types.h>
    #  include <netinet/in.h>
    #  include <arpa/inet.h>
    #else
    #  include <WinSock2.h>
    #endif
    
    #include <string>
    #include <fstream>
    #include <sstream>
    
    #include "src/utils/geo_lookup.h"
    #include "src/utils/https_client.h"
    
    namespace modsecurity {
    namespace Utils {
    
    /* postOrderTraversal: was needed to walk and free the old radix tree nodes.
       The new implementation stores data in std::vector inside TreeRoot, which is
       freed automatically by destroy_radix_tree() in ~IpTree().  This function
       is kept in the public interface for ABI compatibility but has no work to
       do. */
    void IpTree::postOrderTraversal(TreeNode * /*node*/) {}
    
    IpTree::IpTree() {
        create_radix_tree(&m_tree);
    }
    
    IpTree::~IpTree() {
        /* destroy_radix_tree() is used instead of `delete m_tree` so that
           ip_tree.cc does not need to see TreeRoot's full definition.
           msc_tree.h only forward-declares TreeRoot; a raw `delete` on an
           incomplete type is undefined behaviour (and triggers -Wdelete-incomplete
           with GCC).  destroy_radix_tree() is defined in msc_tree.cc where the
           full definition is visible. */
        destroy_radix_tree(m_tree);
        m_tree = nullptr;
    }
    
    bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
        for (std::string line; std::getline(*ss, line); ) {
            /* Strip inline comment. erase() shrinks in-place; no allocation. */
            auto comment = line.find('#');
            if (comment != std::string::npos) {
                line.erase(comment);
            }
    
            /* Strip trailing whitespace including \r from Windows CRLF files. */
            auto end = line.find_last_not_of(" \t\r\n");
            if (end == std::string::npos) {
                continue;   /* blank or comment-only line */
            }
            line.erase(end + 1);
    
            /* Strip leading whitespace. */
            auto start = line.find_first_not_of(" \t\r\n");
            if (start != 0 && start != std::string::npos) {
                line.erase(0, start);
            }
    
            int res = add_ip_from_param(line.c_str(), &m_tree);
            if (res != 0) {
                if (error != nullptr) {
                    error->assign("Failed to parse and add IP: " + line);
                }
                return false;
            }
        }
        return true;
    }
    
    bool IpTree::addFromBuffer(const std::string &buffer, std::string *error) {
        std::stringstream ss;
        ss << buffer;
        return addFromBuffer(&ss, error);
    }
    
    bool IpTree::addFromFile(const std::string &file, std::string *error) {
        std::ifstream myfile(file, std::ios::in);
        if (!myfile.is_open()) {
            error->assign("Failed to open file: " + file);
            return false;
        }
        return addFromBuffer(&myfile, error);
    }
    
    bool IpTree::addFromUrl(const std::string &url, std::string *error) {
        HttpsClient c;
        bool ret = c.download(url);
        if (!ret) {
            error->assign(c.error);
        } else {
            ret = addFromBuffer(c.content, error);
        }
        return ret;
    }
    
    bool IpTree::contains(const std::string &ip) {
        int res = tree_contains_ip(m_tree, ip.c_str());
        return res > 0;
    }
    
    }  // namespace Utils
    }  // namespace modsecurity

    msc_tree.h

    /*
     * ModSecurity, http://www.modsecurity.org/
     * Copyright (c) 2015 - 2024 Trustwave Holdings, Inc. (http://www.trustwave.com/)
     *
     * 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
     *
     * Replacement for msc_tree.{h,cc}.
     *
     * The original radix-tree implementation had three structural bugs:
     *   1. InsertNetmask: malloc without copying the previous netmasks array,
     *      silently losing all prior CIDR entries on every reallocation.
     *   2. CPTAddElement (found!=0 branch): same malloc-without-copy pattern.
     *   3. CPTFindElementIPNetblock: destructively masked ipdata[] in-place,
     *      corrupting the caller's buffer and every subsequent recursive lookup.
     *
     * This replacement uses a sorted interval vector (one per address family).
     * Each CIDR prefix is expanded to a [lo, hi] closed integer interval and
     * stored in a flat std::vector kept sorted by lo.  Lookup is std::lower_bound,
     * O(log n).  Build is O(n log n).  Memory is 32 bytes per entry.  The
     * implementation is ~150 lines of standard C++17 with no new dependencies.
     *
     * Public C API (extern "C") is kept identical to msc_tree.h so ip_tree.cc
     * requires zero changes.
     */
    
    #ifndef SRC_UTILS_MSC_TREE_H_
    #define SRC_UTILS_MSC_TREE_H_
    
    /* ── legacy type aliases kept for ip_tree.cc ─────────────────────────────── */
    /* ip_tree.cc only uses TreeRoot* (opaque) and calls the five C functions
       below.  We keep the name but redefine the struct internally.              */
    
    #define IPV4_TREE 0x1
    #define IPV6_TREE 0x2
    
    #define IPV4_LEN  0x20
    #define IPV6_LEN  0x80
    
    #define NETMASK_256 0x100
    #define NETMASK_128 0x80
    #define NETMASK_64  0x40
    #define NETMASK_32  0x20
    #define NETMASK_16  0x10
    #define NETMASK_8   0x8
    #define NETMASK_4   0x4
    #define NETMASK_2   0x2
    
    #define FALSE 0
    #define TRUE  1
    
    /* Opaque handle used by ip_tree.cc */
    struct TreeRoot;
    
    /* Legacy node type referenced only in IpTree::postOrderTraversal.
       That function now does nothing (memory is owned by TreeRoot internals),
       but the signature must compile.  We provide a minimal stub. */
    struct TreeNode { int _unused; };
    
    extern "C" {
    
    unsigned char is_netmask_v4(char *ip_strv4);
    unsigned char is_netmask_v6(char *ip_strv6);
    
    /* IMPORTANT: all entries must be added (via add_ip_from_param /
       ip_tree_from_param) before the first call to tree_contains_ip() on a
       given tree. The interval vectors are sorted and merged lazily, exactly
       once, on first query (std::call_once in msc_tree.cc). Entries added
       after that first query are appended unsorted and will not be found
       reliably, and can even break lookups for entries added before the
       first query. This matches how IpMatch/IpMatchFromFile use the tree
       (all inserts happen in init(), all queries happen later in
       evaluate()), but any other caller must preserve that ordering. */
    int tree_contains_ip(TreeRoot *rtree, const char *value);
    int add_ip_from_param(const char *param, TreeRoot **rtree);
    int ip_tree_from_param(const char *param, TreeRoot **rtree);
    int create_radix_tree(TreeRoot **rtree);
    
    /* Destroy a tree created by create_radix_tree.  Caller must set the pointer
       to nullptr afterwards.  Using this instead of a raw `delete` keeps
       ip_tree.cc free of TreeRoot's full definition, which is an implementation
       detail of msc_tree.cc. */
    void destroy_radix_tree(TreeRoot *rtree);
    
    }  /* extern "C" */
    
    #endif  /* SRC_UTILS_MSC_TREE_H_ */

    msc_tree.cc

    /*
     * ModSecurity, http://www.modsecurity.org/
     * Copyright (c) 2015 - 2026 Trustwave Holdings, Inc. (http://www.trustwave.com/)
     *
     * 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
     *
     * Replacement implementation for msc_tree.cc.
     * See msc_tree.h for the full rationale.
     *
     * Design
     * ──────
     * Each CIDR entry (or single host = CIDR/32 or /128) is converted to a
     * closed integer interval [lo, hi] and stored in a flat sorted std::vector.
     * IPv4 uses uint32_t; IPv6 uses __uint128_t (supported on all GCC/Clang
     * targets that ModSecurity already builds on; no new dependency).
     *
     * Insert: find position with lower_bound, O(n) shift (build is one-time).
     * Lookup: upper_bound + backward scan, O(log n + k) where k is the number
     *         of intervals sharing the same lo (k=1 for non-overlapping sets).
     *
     * Overlapping CIDR prefixes (e.g. 10/8 and 10.0/16 and 10.0.0/24 on the
     * same tree) are handled correctly by sorting equal-lo intervals by hi DESC
     * (wider first) and scanning backward past all intervals whose lo <= query.
     *
     * Memory: IPv4: 8 bytes/entry   IPv6: 32 bytes/entry
     */
    
    #ifdef MODSECURITY_BUILD
    #  include "src/utils/msc_tree.h"
    #else
    /* standalone / test build — header provided via -include flag */
    #endif
    
    #include <cstdint>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <new>
    #include <mutex>
    
    #ifndef WIN32
    #  include <sys/socket.h>
    #  include <netinet/in.h>
    #  include <arpa/inet.h>
    #else
    #  include "src/compat/msvc.h"
    #  include <WinSock2.h>
    #  include <WS2tcpip.h>
    /* MSVC has no strtok_r (POSIX-only). Its strtok_s takes the same
       (str, delim, &saveptr) signature, so it's a drop-in replacement. */
    #  define strtok_r strtok_s
    /* MSVC provides strdup as the underscored _strdup (same signature). */
    #  define strdup _strdup
    #endif
    
    /* ── Integer Address Types & MSVC Compatibility ────────────────────────── */
    
    using u32  = uint32_t;
    
    #if defined(_MSC_VER) && !defined(__clang__)
    // Custom 128-bit integer structure for MSVC compilation
    struct u128 {
        uint64_t high;
        uint64_t low;
    
        u128() : high(0), low(0) {}
        u128(uint64_t val) : high(0), low(val) {}
        u128(int val) : high(0), low(static_cast<uint64_t>(val)) {}
        u128(uint64_t h, uint64_t l) : high(h), low(l) {}
    
        u128 operator<<(int shift) const {
            if (shift <= 0)   return *this;
            if (shift >= 128) return u128(0, 0);
            if (shift >= 64)  return u128(low << (shift - 64), 0);
            // shift in [1, 63] range: safe shift, (64 - shift) is also in [1, 63] - no UB
            return u128((high << shift) | (low >> (64 - shift)), low << shift);
        }
    
        u128 operator|(uint64_t val) const {
            return u128(high, low | val);
        }
    
        u128 operator|(const u128& o) const {
            return u128(high | o.high, low | o.low);
        }
    
        u128 operator&(const u128& o) const {
            return u128(high & o.high, low & o.low);
        }
    
        u128 operator~() const {
            return u128(~high, ~low);
        }
    
        u128 operator+(uint64_t val) const {
            uint64_t new_low = low + val;
            uint64_t new_high = high + (new_low < low ? 1 : 0);
            return u128(new_high, new_low);
        }
    
        bool operator<(const u128& o) const {
            return high < o.high || (high == o.high && low < o.low);
        }
    
        bool operator<=(const u128& o) const {
            return high < o.high || (high == o.high && low <= o.low);
        }
    
        bool operator>(const u128& o) const {
            return high > o.high || (high == o.high && low > o.low);
        }
    
        bool operator>=(const u128& o) const {
            return high > o.high || (high == o.high && low >= o.low);
        }
    
        bool operator==(const u128& o) const {
            return high == o.high && low == o.low;
        }
    
        bool operator!=(const u128& o) const {
            return !(*this == o);
        }
    };
    #else
    using u128 = __uint128_t;
    #endif
    
    /* ── Interval Definitions ───────────────────────────────────────────────── */
    
    struct Iv4 { u32  lo, hi; };
    struct Iv6 { u128 lo, hi; };
    
    struct TreeRoot {
        std::vector<Iv4> v4;
        std::vector<Iv6> v6;
        std::once_flag once; // Thread-safe trigger for lazy normalization on first query
    };
    
    /* ── IP Address Parsers ────────────────────────────────────────────────── */
    
    static bool parse4(const char *s, u32 *out) {
        struct in_addr a;
        if (inet_pton(AF_INET, s, &a) != 1) return false;
        *out = ntohl(a.s_addr);
        return true;
    }
    
    static bool parse6(const char *s, u128 *out) {
        struct in6_addr a;
        if (inet_pton(AF_INET6, s, &a) != 1) return false;
        u128 v = 0;
        for (int i = 0; i < 16; i++) {
            v = (v << 8) | static_cast<uint64_t>(a.s6_addr[i]);
        }
        *out = v;
        return true;
    }
    
    /* ── Interval Merging Algorithms ────────────────────────────────────────── */
    
    static void compress4(std::vector<Iv4> &vec) {
        if (vec.size() < 2) return;
    
        // Sort by lo ascending, and by hi descending if lo is equal (wider intervals first)
        std::sort(vec.begin(), vec.end(), [](const Iv4 &a, const Iv4 &b) {
            return a.lo < b.lo || (a.lo == b.lo && b.hi < a.hi);
        });
    
        size_t write_idx = 0;
        for (size_t i = 1; i < vec.size(); ++i) {
            Iv4 &curr = vec[write_idx];
            Iv4 &next = vec[i];
    
            if (next.lo <= curr.hi) {
                // Overlapping or nested intervals -> merge directly
                if (curr.hi < next.hi) {
                    curr.hi = next.hi;
                }
            } else if (curr.hi != ~static_cast<u32>(0) && next.lo == curr.hi + 1) {
                // Adjacent intervals without overflow risk (e.g., 10.0.0.1 and 10.0.0.2) -> merge
                curr.hi = next.hi;
            } else {
                write_idx++;
                vec[write_idx] = next;
            }
        }
        vec.resize(write_idx + 1);
    }
    
    static void compress6(std::vector<Iv6> &vec) {
        if (vec.size() < 2) return;
    
        // Sort by lo ascending, and by hi descending if lo is equal
        std::sort(vec.begin(), vec.end(), [](const Iv6 &a, const Iv6 &b) {
            return a.lo < b.lo || (a.lo == b.lo && b.hi < a.hi);
        });
    
        size_t write_idx = 0;
        for (size_t i = 1; i < vec.size(); ++i) {
            Iv6 &curr = vec[write_idx];
            Iv6 &next = vec[i];
    
            if (next.lo <= curr.hi) {
                // Overlapping or nested intervals -> merge directly
                if (curr.hi < next.hi) {
                    curr.hi = next.hi;
                }
            } else if (curr.hi != ~u128(0) && next.lo == curr.hi + 1) {
                // Adjacent intervals without overflow risk -> merge
                curr.hi = next.hi;
            } else {
                write_idx++;
                vec[write_idx] = next;
            }
        }
        vec.resize(write_idx + 1);
    }
    
    /* ── Fast Appends ───────────────────────────────────────────────────────── */
    
    static void insert4(std::vector<Iv4> &vec, u32 lo, u32 hi) {
        vec.push_back(Iv4{lo, hi});
    }
    
    static void insert6(std::vector<Iv6> &vec, u128 lo, u128 hi) {
        vec.push_back(Iv6{lo, hi});
    }
    
    /* ── Parse and Load Single Rule ─────────────────────────────────────────── */
    
    static bool add_entry(TreeRoot *root, const char *entry) {
        char buf[128];
        const char *slash = strchr(entry, '/');
        int prefix_len = -1;
    
        if (slash != nullptr) {
            size_t host_len = static_cast<size_t>(slash - entry);
            if (host_len >= sizeof(buf)) return false;
            memcpy(buf, entry, host_len);
            buf[host_len] = '\0';
    
            /* atoi() returns 0 for non-numeric input (e.g. "10.0.0.1/xx"),
               which would silently become a /0 "match everything" rule.
               Use strtol() and verify the whole suffix was consumed. */
            char *endptr = nullptr;
            long parsed = strtol(slash + 1, &endptr, 10);
            if (*endptr != '\0' || endptr == slash + 1) return false;
            if (parsed < 0 || parsed > 128) return false;
            prefix_len = static_cast<int>(parsed);
        } else {
            if (strlen(entry) >= sizeof(buf)) return false;
            strcpy(buf, entry);
        }
    
        bool is_v6 = (strchr(buf, ':') != nullptr);
    
        if (!is_v6) {
            if (prefix_len < 0) prefix_len = 32;
            if (prefix_len < 0 || prefix_len > 32) return false;
            u32 host;
            if (!parse4(buf, &host)) return false;
            u32 mask = (prefix_len == 0) ? 0u : (~0u << (32 - prefix_len));
            insert4(root->v4, host & mask, (host & mask) | ~mask);
        } else {
            if (prefix_len < 0) prefix_len = 128;
            if (prefix_len < 0 || prefix_len > 128) return false;
            u128 host;
            if (!parse6(buf, &host)) return false;
            u128 mask = (prefix_len == 0)
                ? u128(0)
                : (~u128(0) << (128 - prefix_len));
            insert6(root->v6, host & mask, (host & mask) | ~mask);
        }
        return true;
    }
    
    /* ═══════════════════════════════════════════════════════════════════════════
       Public C API Implementation (Extern "C")
       ═══════════════════════════════════════════════════════════════════════════ */
    extern "C" {
    
    unsigned char is_netmask_v4(char *ip_strv4) {
        unsigned char netmask_v4 = 32;
        if (ip_strv4 == nullptr) return netmask_v4;
        char *mask_str = strchr(ip_strv4, '/');
        if (mask_str) {
            *(mask_str++) = '\0';
            if (strchr(mask_str, '.') != nullptr) return 0;
            char *endptr = nullptr;
            long cidr = strtol(mask_str, &endptr, 10);
            if (*endptr != '\0' || endptr == mask_str) return 0;
            if (cidr == 32) return 32;
            if (cidr < 0 || cidr > 32) return 0;
            netmask_v4 = static_cast<unsigned char>(cidr);
        }
        return netmask_v4;
    }
    
    unsigned char is_netmask_v6(char *ip_strv6) {
        unsigned char netmask_v6 = 128;
        if (ip_strv6 == nullptr) return netmask_v6;
        char *mask_str = strchr(ip_strv6, '/');
        if (mask_str) {
            *(mask_str++) = '\0';
            if (strchr(mask_str, ':') != nullptr) return 0;
            char *endptr = nullptr;
            long cidr = strtol(mask_str, &endptr, 10);
            if (*endptr != '\0' || endptr == mask_str) return 0;
            if (cidr < 0 || cidr > 128) return 0;
            netmask_v6 = static_cast<unsigned char>(cidr);
        }
        return netmask_v6;
    }
    
    int create_radix_tree(TreeRoot **rtree) {
        *rtree = new (std::nothrow) TreeRoot();
        return (*rtree == nullptr) ? -1 : 0;
    }
    
    int add_ip_from_param(const char *param, TreeRoot **rtree) {
        if (!param || !rtree || !*rtree) return -1;
        char *copy = strdup(param);
        if (!copy) return -1;
        char *saved = nullptr;
        char *tok = strtok_r(copy, ",", &saved);
        while (tok != nullptr) {
            while (*tok == ' ' || *tok == '\t') ++tok;
            if (*tok != '\0') {
                if (!add_entry(*rtree, tok)) {
                    free(copy);
                    return -1;
                }
            }
            tok = strtok_r(nullptr, ",", &saved);
        }
        free(copy);
        return 0;
    }
    
    int ip_tree_from_param(const char *param, TreeRoot **rtree) {
        if (create_radix_tree(rtree) != 0) return -1;
        return add_ip_from_param(param, rtree);
    }
    
    int tree_contains_ip(TreeRoot *rtree, const char *value) {
        if (!rtree || !value) return 0;
    
        // Trigger thread-safe lazy interval merging on first query
        std::call_once(rtree->once, [rtree]() {
            compress4(rtree->v4);
            compress6(rtree->v6);
        });
    
        if (strchr(value, ':') == nullptr) {
            u32 addr;
            if (!parse4(value, &addr)) return -1;
            
            const auto &vec = rtree->v4;
            if (vec.empty()) return 0;
            
            auto it = std::upper_bound(vec.begin(), vec.end(), addr,
                [](u32 v, const Iv4 &iv) { return v < iv.lo; });
            
            if (it == vec.begin()) return 0;
            --it;
            return (addr >= it->lo && addr <= it->hi) ? 1 : 0;
        } else {
            u128 addr;
            if (!parse6(value, &addr)) return -1;
            
            const auto &vec = rtree->v6;
            if (vec.empty()) return 0;
            
            auto it = std::upper_bound(vec.begin(), vec.end(), addr,
                [](u128 v, const Iv6 &iv) { return v < iv.lo; });
            
            if (it == vec.begin()) return 0;
            --it;
            return (addr >= it->lo && addr <= it->hi) ? 1 : 0;
        }
    }
    
    void destroy_radix_tree(TreeRoot *rtree) {
        delete rtree;
    }
    
    }  /* extern "C" */
  7. airween commented on Jun 27, 2026

    @airween
    Member

    ModSecurity-nginx v1.0.4
    libmodsecurity3 version 3.0.15

    thanks,

    Regression Tests
    ip_tree.cc test_ip_tree_regression.cc

    Ahm, I meant

    SecRule REMOTE_ADDR "@ipMatchFromFile /etc/opt/cpf/conf/modsecurity_whitelist" "id:250000004,phase:1,nolog,allow"
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    these or this file(s).

    I guess you suspect the issue is there.

  8. JIFVGWHvAseSovQ commented on Jun 27, 2026

    @JIFVGWHvAseSovQ
    Author

    cat /etc/opt/cpf/conf/modsecurity_whitelist
    2b00:8c00:4000:020d:0000:0000:008a:0a5b
    2b00:8c00:4000:020d:0000:0000:008a:0a5c
    2b00:8c00:4000:020d:0000:0000:008a:0a57

  9. airween commented on Jun 27, 2026

    @airween
    Member

    @JIFVGWHvAseSovQ - thanks.

    You wrote:

    ISSUE:
    When we whitelist an IPv6 address( in whitelist file the address is expanded), if a request then comes from a non expanded IPV6 address, it is not whit list it and mod_security blocks it.

    so this is the content, and these are the expanded IPv6 addresses.

    May I ask you what does it mean "a request then comes from a non expanded IPV6 address"?

  10. JIFVGWHvAseSovQ commented on Jun 27, 2026

    @JIFVGWHvAseSovQ
    Author

    @airween - thanks.

    You wrote:

    ISSUE:
    When we whitelist an IPv6 address( in whitelist file the address is expanded), if a request then comes from a non expanded IPV6 address, it is not whit list it and mod_security blocks it.

    so this is the content, and these are the expanded IPv6 addresses.

    May I ask you what does it mean "a request then comes from a non expanded IPV6 address"?

    Request IPv6 (compressed): 2001:8a00:6000:13b0::b566:ffd1
    whitelist IPv6 (expanded): 2001:8a00:6000:13b0:0000:0000:b566:ffd1

    @ipMatch / @ipMatchFromFile — Bug Report & Fix

  11. fzipi commented on Oct 12, 2026

    @fzipi
    Collaborator

    Revisiting this one. This duplicates #2192 verbatim, and the investigation it triggered produced the real finding in #3588 (a bug in the radix tree's netmask handling). Let's close this one and keep #3588.

  12. added
    duplicate
    needs decision
    on Oct 12, 2026
  13. fzipi commented on Dec 1, 2026

    @fzipi
    Collaborator

    Closing as a duplicate of #3588, which has the root cause in the radix tree netmask handling and fixes in #3660 (v3) and #3671 (v2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xduplicateneeds decision

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions