{"record":{"id":"b316bbfe5f645765","repo":"grpc/grpc-java","slug":"invalid-host-or-port-b316bb","errorCode":null,"errorMessage":"Invalid host or port: ","messagePattern":"Invalid host or port: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/io/grpc/internal/GrpcUtil.java","lineNumber":550,"sourceCode":"   * @return the {@code authority} provided\n   */\n  public static String checkAuthority(String authority) {\n    URI uri = authorityToUri(authority);\n    // Verify that the user Info is not provided.\n    checkArgument(uri.getAuthority().indexOf('@') == -1,\n        \"Userinfo must not be present on authority: '%s'\", authority);\n    return authority;\n  }\n\n  /**\n   * Combine a host and port into an authority string.\n   */\n  // There is a copy of this method in io.grpc.Grpc\n  public static String authorityFromHostAndPort(String host, int port) {\n    try {\n      return new URI(null, null, host, port, null, null, null).getAuthority();\n    } catch (URISyntaxException ex) {\n      throw new IllegalArgumentException(\"Invalid host or port: \" + host + \" \" + port, ex);\n    }\n  }\n\n  /**\n   * Shared executor for channels.\n   */\n  public static final Resource<Executor> SHARED_CHANNEL_EXECUTOR =\n      new Resource<Executor>() {\n        private static final String NAME = \"grpc-default-executor\";\n        @Override\n        public Executor create() {\n          return Executors.newCachedThreadPool(getThreadFactory(NAME + \"-%d\", true));\n        }\n\n        @Override\n        public void close(Executor instance) {\n          ((ExecutorService) instance).shutdown();\n        }","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/core/src/main/java/io/grpc/internal/GrpcUtil.java#L532-L568","documentation":"GrpcUtil.authorityFromHostAndPort formats a host and port into a valid authority via java.net.URI. If either the host (e.g. empty, illegal characters, unbracketed IPv6) or port (e.g. negative or > 65535) makes URI construction fail, the URISyntaxException is wrapped and rethrown as IllegalArgumentException(\"Invalid host or port: ...\").","triggerScenarios":"Calling authorityFromHostAndPort with an invalid host string (spaces, bad IPv6 without brackets) or an out-of-range port (< 0 or > 65535), typically from a target string parsed into host/port for a channel builder.","commonSituations":"Parsing target strings like \"host:99999\" or \"[::1]:50051\" manually and losing the brackets; user-supplied endpoints; ports read from config as arbitrary integers.","solutions":["Correct the host and port values: validate the port is 0-65535 and format IPv6 hosts with brackets before calling.","Parse endpoints with existing helpers (e.g. InetSocketAddress / GrpcUtil authorities) rather than splitting strings manually.","Pre-validate inputs: `if (port < 0 || port > 65535) throw ...` and check host legality with new URI(null, null, host, port, ...) in a guard."],"exampleFix":"// before\nString authority = GrpcUtil.authorityFromHostAndPort(\"::1\", 50051); // URI fails\n// after\nString authority = GrpcUtil.authorityFromHostAndPort(\"[::1]\", 50051);","handlingStrategy":"validation","validationCode":"if (port < 0 || port > 65535) throw new IllegalArgumentException(\"port out of range: \" + port);\nif (host.contains(\":\") && !host.startsWith(\"[\")) host = \"[\" + host + \"]\";","typeGuard":null,"tryCatchPattern":"try {\n  authority = GrpcUtil.authorityFromHostAndPort(host, port);\n} catch (IllegalArgumentException e) {\n  // report invalid endpoint to caller\n}","preventionTips":["Validate port range before formatting endpoints.","Bracket IPv6 hosts.","Parse targets with library helpers rather than manual splitting."],"tags":["grpc","uri","host","port"],"backgroundTag":"invalid-url-format","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}