{"record":{"id":"2789f97e94ddfa54","repo":"stamparm/maltrail","slug":"x-invalid-ip-address-s","errorCode":null,"errorMessage":"[x] invalid IP address '%s'","messagePattern":"\\[x\\] invalid IP address '(.+?)'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"core/common.py","lineNumber":282,"sourceCode":"                    _ipcat_cache[value] = name\n\n    if address in _ipcat_cache:\n        retval = _ipcat_cache[address]\n    elif address in _ipcat_dynamic_cache:\n        retval = _ipcat_dynamic_cache[address]\n    else:\n        retval = \"\"\n\n        if os.path.isfile(IPCAT_SQLITE_FILE):\n            with sqlite3.connect(IPCAT_SQLITE_FILE, isolation_level=None) as conn:\n                cursor = conn.cursor()\n                try:\n                    _ = addr_to_int(address)\n                    cursor.execute(\"SELECT name FROM ranges WHERE start_int <= ? AND end_int >= ?\", (_, _))\n                    _ = cursor.fetchone()\n                    retval = str(_[0]) if _ else retval\n                except Exception:\n                    raise ValueError(\"[x] invalid IP address '%s'\" % address)\n\n                _ipcat_dynamic_cache[address] = retval\n\n    return retval\n\ndef worst_asns(address):\n    if not address:\n        return None\n\n    try:\n        _ = addr_to_int(address)\n        for prefix, mask, name in WORST_ASNS.get(address.split('.')[0], {}):\n            if _ & mask == prefix:\n                return name\n    except (IndexError, ValueError):\n        pass\n\n    return None","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/stamparm/maltrail/blob/77cfb06d7606506d101bbcec0786c77166c4255e/core/common.py#L264-L300","documentation":"ipcat_lookup() parses the given address with addr_to_int() to query the local ranges table; when that fails (or the DB query errors) it deliberately re-raises as ValueError. It means the caller passed a string that is not a parseable IPv4/IPv6 address.","triggerScenarios":"ipcat_lookup('not-an-ip'), empty string, hostname ('example.com') instead of an IP, or IPv6 forms addr_to_int does not support.","commonSituations":"Unvalidated user/CGI input, log lines with hostnames, misparsed fields before calling _check_ip or _meta, stale cache logic after address normalization changes.","solutions":["Validate the address with a proper parser before calling (e.g. socket.inet_pton or ipaddress.ip_address())","Trim whitespace and strip port/scope from the input string","Reject/resolve hostnames to IPs first if hostnames are expected","Catch ValueError around ipcat_lookup and treat as unknown/invalid IP"],"exampleFix":"// before\nretval = ipcat_lookup(user_input)\n// after\nimport ipaddress\ntry:\n    ip = str(ipaddress.ip_address(user_input.strip()))\n    retval = ipcat_lookup(ip)\nexcept ValueError:\n    retval = None  # not a valid IP","handlingStrategy":"validation","validationCode":"import ipaddress, socket\ndef is_valid_ip(s):\n    if not isinstance(s, str):\n        return False\n    try:\n        ipaddress.ip_address(s.strip())\n        return True\n    except ValueError:\n        return False","typeGuard":"def looks_like_ip(s):\n    try:\n        socket.inet_pton(socket.AF_INET, s)\n        return True\n    except (OSError, TypeError):\n        try:\n            socket.inet_pton(socket.AF_INET6, s.split('%')[0])\n            return True\n        except (OSError, TypeError):\n            return False","tryCatchPattern":"try:\n    owner = ipcat_lookup(address)\nexcept ValueError:\n    owner = None  # unparseable address","preventionTips":["Normalize input (strip spaces, ports, brackets, zone ids) before lookup","Resolve hostnames to IPs before calling ipcat_lookup","Cache validation results alongside lookup results","Unit-test the address parser against malformed inputs"],"tags":["python","ip-address","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"77cfb06d7606506d101bbcec0786c77166c4255e","analyzedAt":"2026-09-13T03:50:16.010Z","contentChangedAt":"2026-09-13T03:50:16.010Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}