{"record":{"id":"c843652216a27d85","repo":"moodle/moodle","slug":"invalidipformat","errorCode":"invalidipformat","errorMessage":"invalidipformat","messagePattern":"invalidipformat","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/iplookup/index.php","lineNumber":74,"sourceCode":"    $ispopup = 1;\n} else if ($ispopup === 1) {  // Param popup was set, then we know that we want a popup.\n    $urlparams['ispopup'] = 1;\n}\n// Set the page layout accordingly.\nif ($ispopup) {\n    $PAGE->set_pagelayout('popup');\n} else {\n    $PAGE->set_pagelayout('standard');\n}\n\n$PAGE->set_url('/iplookup/index.php', $urlparams);\n$PAGE->set_context(context_system::instance());\n\n$info = array($ip);\n$note = array();\n\nif (cleanremoteaddr($ip) === false) {\n    throw new \\moodle_exception('invalidipformat', 'error');\n}\n\nif (!ip_is_public($ip)) {\n    throw new \\moodle_exception('iplookupprivate', 'error');\n}\n\n$info = iplookup_find_location($ip);\n\nif ($info['error']) {\n    // Can not display.\n    notice($info['error']);\n}\n\nif ($user) {\n    if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {\n        // note: better not show full names to everybody\n        if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) {\n            array_unshift($info['title'], fullname($user));","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/iplookup/index.php#L56-L92","documentation":"public/iplookup/index.php throws moodle_exception 'invalidipformat' when the user-supplied IP parameter fails cleanremoteaddr() validation. The IP string is not a syntactically valid IPv4/IPv6 address.","triggerScenarios":"Visiting /iplookup/index.php?ip=<value> where cleanremoteaddr($ip) returns false — e.g. garbage input, 'unknown', hostname instead of IP, or a malformed IPv6 string.","commonSituations":"Manually crafting the iplookup URL with a hostname or obfuscated value, logging systems storing placeholder values like '0.0.0.0 hidden', plugins passing unparsed log tokens into the lookup.","solutions":["Pass a valid, properly formatted IP address in the ip parameter.","Normalize the value with cleanremoteaddr() before redirecting to the iplookup page.","If the input comes from logs, sanitize/validate it and skip lookup when it is not a real IP.","Decode/URL-encode the parameter correctly — encoded or truncated IPv6 fails validation."],"exampleFix":"// before\nredirect('/iplookup/index.php?ip=' . $host); // hostname -> invalidipformat\n// after\n$ip = cleanremoteaddr($host);\nif ($ip !== false) {\n    redirect('/iplookup/index.php?ip=' . $ip);\n}","handlingStrategy":"validation","validationCode":"function isValidIp(ip) {\n  return typeof ip === 'string' &&\n    /^(\\d{1,3}(\\.\\d{1,3}){3}|[0-9a-fA-F:]+)$/.test(ip) &&\n    ip.indexOf(':') !== -1 ? ip.includes('::') || ip.split(':').length > 2 : ip.split('.').every(n => +n <= 255);\n}\n// PHP side: cleanremoteaddr($ip) !== false before linking to iplookup","typeGuard":"function isIpString(v) {\n  return typeof v === 'string' && v.length > 0 && (net.isIP(v) !== 0);\n}","tryCatchPattern":"try {\n  render_iplookup($ip);\n} catch (moodle_exception $e) {\n  if ($e->errorcode === 'invalidipformat') {\n    echo 'Provided value is not a valid IP address.';\n  }\n}","preventionTips":["Always run cleanremoteaddr() on IP values sourced from logs before using them in URLs.","Never pass hostnames or log placeholders where an IP is expected.","URL-encode IPv6 addresses in query strings.","Validate user input on entry pages that forward to iplookup."],"tags":["php","moodle","validation","ip-address"],"backgroundTag":"invalid-argument-format","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}