{"record":{"id":"d5fdfc92eb94094c","repo":"projectdiscovery/nuclei","slug":"invalid-base-ip","errorCode":null,"errorMessage":"invalid base ip","messagePattern":"invalid base ip","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/protocols/common/randomip/randomip.go","lineNumber":45,"sourceCode":"\n\tif !iputil.IsCIDR(cidr) {\n\t\treturn nil, errors.Errorf(\"%s is not a valid cidr\", cidr)\n\t}\n\n\tbaseIp, ipnet, err := net.ParseCIDR(cidr)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tswitch {\n\tcase ipnet.Mask[len(ipnet.Mask)-1] == 255:\n\t\treturn baseIp, nil\n\tcase iputil.IsIPv4(baseIp.String()):\n\t\treturn getRandomIP(ipnet, 4), nil\n\tcase iputil.IsIPv6(baseIp.String()):\n\t\treturn getRandomIP(ipnet, 16), nil\n\tdefault:\n\t\treturn nil, errors.New(\"invalid base ip\")\n\t}\n}\n\nfunc getRandomIP(ipnet *net.IPNet, size int) net.IP {\n\tip := ipnet.IP\n\tvar iteration int\n\n\tfor iteration < maxIterations {\n\t\titeration++\n\t\tones, _ := ipnet.Mask.Size()\n\t\tquotient := ones / 8\n\t\tremainder := ones % 8\n\t\tvar r []byte\n\t\tswitch size {\n\t\tcase 4, 16:\n\t\t\tr = make([]byte, size)\n\t\tdefault:\n\t\t\treturn ip","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/projectdiscovery/nuclei/blob/265b3a3dec374741614e342f813c10f8b38d2bb7/pkg/protocols/common/randomip/randomip.go#L27-L63","documentation":"Raised by GetRandomIPWithCidr in pkg/protocols/common/randomip (randomip.go:45). After net.ParseCIDR succeeds, the function branches on the base IP: if the last mask byte is 255 the base is returned as-is; otherwise IPv4/IPv6 each get a random address in the net. The 'invalid base ip' branch is a defensive default for a parsed CIDR whose base IP classifies as neither IPv4 nor IPv6 via iputil. Most bad inputs (non-CIDR strings, bad masks) fail earlier inside ParseCIDR and return that error instead, so hitting this branch means an exotic, nearly-unreachable IP form.","triggerScenarios":"Passing an unusual CIDR whose base IP defeats iputil.IsIPv4/IsIPv6 classification (e.g. IPv4-mapped or degenerate forms produced by dynamic string building in a template); in practice most users who see it passed a malformed expression into rand_ip() that happened to parse.","commonSituations":"The rand_ip() DSL helper fed with a variable-built CIDR string; copy-paste of an IPv6 CIDR with zone or embedded dashes; unit tests constructing synthetic IPNet values.","solutions":["Pass a canonical CIDR such as 173.1.0.0/16 or 2001:db8::/32","Pre-validate with net.ParseCIDR and iputil.IsIPv4/IsIPv6 before calling the helper","If the input is dynamic, build it from trusted parts rather than string concatenation of user data"],"exampleFix":"// before\nip := randomip.GetRandomIPWithCidr(userSupplied)\n// after\nif _, _, err := net.ParseCIDR(userSupplied); err != nil { return err }\nip, err := randomip.GetRandomIPWithCidr(userSupplied)","handlingStrategy":"validation","validationCode":"func cidrOK(s string) bool {\n    _, ipnet, err := net.ParseCIDR(s)\n    if err != nil { return false }\n    base := ipnet.IP\n    return iputil.IsIPv4(base.String()) || iputil.IsIPv6(base.String())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Hard-code canonical CIDRs in rand_ip() calls","Pre-validate dynamic CIDRs with net.ParseCIDR","Log the offending value when random IP generation fails"],"tags":["randomip","cidr","dsl","validation"],"backgroundTag":null,"analyzedSha":"265b3a3dec374741614e342f813c10f8b38d2bb7","analyzedAt":"2026-08-15T20:05:51.855Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}