{"record":{"id":"2e82f31a4316f0c7","repo":"apache/shenyu","slug":"invalid-port-in-upstream-url","errorCode":null,"errorMessage":"Invalid port in upstream URL: ","messagePattern":"Invalid port in upstream URL: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java","lineNumber":363,"sourceCode":"     * mapToDiscoveryUpstreamData.\n     *\n     * @param commonUpstream commonUpstream\n     * @return DiscoveryUpstreamData\n     */\n    public DiscoveryUpstreamData mapToDiscoveryUpstreamData(CommonUpstream commonUpstream) {\n        String upstreamUrl = commonUpstream.getUpstreamUrl();\n        String[] parts = Optional.ofNullable(upstreamUrl)\n                .map(url -> url.split(\":\", 2))\n                .orElseThrow(() -> new IllegalArgumentException(\"Upstream URL must not be null\"));\n        if (parts.length < 2) {\n            throw new IllegalArgumentException(\"Invalid upstream URL, expected 'host:port' format but was: \" + upstreamUrl);\n        }\n        String host = parts[0];\n        int port;\n        try {\n            port = Integer.parseInt(parts[1]);\n        } catch (NumberFormatException ex) {\n            throw new IllegalArgumentException(\"Invalid port in upstream URL: \" + upstreamUrl, ex);\n        }\n        DiscoveryUpstreamDTO discoveryUpstreamDTO = CommonUpstreamUtils.buildDefaultDiscoveryUpstreamDTO(\n                host,\n                port,\n                commonUpstream.getProtocol(),\n                commonUpstream.getNamespaceId());\n        Properties properties = Optional.ofNullable(discoveryUpstreamDTO.getProps())\n                .map(props -> GsonUtils.getInstance().fromJson(props, Properties.class))\n                .orElse(new Properties());\n        properties.setProperty(\"healthCheckEnabled\", String.valueOf(commonUpstream.isHealthCheckEnabled()));\n        discoveryUpstreamDTO.setProps(GsonUtils.getInstance().toJson(properties));\n        return mapToData(discoveryUpstreamDTO);\n    }\n}\n","sourceCodeStart":345,"sourceCodeEnd":378,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/transfer/DiscoveryTransfer.java#L345-L378","documentation":"In mapToDiscoveryUpstreamData, the part after the first ':' in upstreamUrl is parsed as an integer port; NumberFormatException is wrapped into IllegalArgumentException('Invalid port in upstream URL: ...').","triggerScenarios":"URL like 'host:abc', 'host:' (empty port), or 'host:8080x' reaches the transfer; also URLs where the remainder after ':' is not purely numeric, e.g. 'http://host:8080/path' (split limit 2 leaves '//host:8080/path' as the second part... actually 'http' + '//host:8080/path') making port parsing fail.","commonSituations":"Users entering full URLs (http://host:8080) instead of host:port in discovery config; trailing slashes/paths on upstream entries; typo'd ports; copy-paste of service URIs from other tools.","solutions":["Store the upstream as plain 'host:port' with a numeric port, e.g. '10.0.0.5:8080' — not a full URL","Fix the offending entry in the dashboard discovery upstream list or in the discovery center data","Strip scheme/path before registering upstreams obtained from service registries","Add input validation in the dashboard form/API when creating upstreams"],"exampleFix":"// before\nupstreamUrl = \"http://10.0.0.5:8080/api\";\n// after\nupstreamUrl = \"10.0.0.5:8080\";","handlingStrategy":"validation","validationCode":"int port = -1;\nString p = url.substring(url.indexOf(':') + 1);\nboolean portOk = p.chars().allMatch(Character::isDigit) && !p.isEmpty();\nif (portOk) port = Integer.parseInt(p);","typeGuard":null,"tryCatchPattern":"try { map(u); } catch (IllegalArgumentException e) { log.warn(\"invalid port for {}\", u.getUpstreamUrl()); sanitizeAndRetry(); }","preventionTips":["Store plain host:port, never full http:// URLs, in upstream fields","Trim whitespace and reject non-numeric port input at registration time","Add regex validation (^[^:]+:[0-9]+$) on upstream creation endpoints"],"tags":["discovery","port","url-format","admin"],"backgroundTag":"invalid-url-format","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}