{"record":{"id":"9d865061624e5c19","repo":"Billionmail/BillionMail","slug":"invalid-port","errorCode":null,"errorMessage":"invalid port: ","messagePattern":"invalid port: ","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/internal/service/public/common.go","lineNumber":2274,"sourceCode":"\t// Try to listen on the port\n\tlistener, err := net.Listen(\"tcp\", fmt.Sprintf(\":%s\", strconv.Itoa(port)))\n\n\tif err != nil {\n\t\t// Port is in use\n\t\treturn true\n\n\t}\n\t// Close listener\n\tdefer listener.Close()\n\n\treturn false\n}\n\n// Allow port\nfunc AllowPort(port int) (err error) {\n\t// Check if port number is valid\n\tif !IsPort(strconv.Itoa(port)) {\n\t\treturn errors.New(\"invalid port: \" + strconv.Itoa(port))\n\t}\n\n\tdefer func() {\n\t\tif err == nil {\n\t\t\t// Reload firewall\n\t\t\t_ = ReloadFirewall()\n\t\t}\n\t}()\n\n\t// Command line output\n\tvar s string\n\n\t// Check if it is Ubuntu\n\tif FileExists(\"/usr/sbin/ufw\") || FileExists(\"/usr/bin/ufw\") {\n\t\ts, err = ExecShell(fmt.Sprintf(\"ufw allow %d/tcp\", port))\n\t\tif err != nil {\n\t\t\tg.Log().Error(context.Background(), \"AllowPort error: \", err, \" \", s)\n\t\t}","sourceCodeStart":2256,"sourceCodeEnd":2292,"githubUrl":"https://github.com/Billionmail/BillionMail/blob/fc36c76c050c3775c5e899faf7403cf0262d2744/core/internal/service/public/common.go#L2256-L2292","documentation":"AllowPort in core/internal/service/public/common.go opens a port in the firewall. Before touching the firewall it validates the port number with IsPort; if the integer is outside the valid port range (or otherwise invalid), it returns \"invalid port: <port>\". ReloadFirewall only runs when the call succeeds.","triggerScenarios":"Calling AllowPort(port) with port <= 0, port > 65535, or a value IsPort rejects; typically the port came from an unvalidated request parameter or a misconfigured config value parsed from a string.","commonSituations":"Admin UI submitting an empty/NaN port parsed as 0, config files with port \"http\" instead of \"80\", or overflowed values from atoi conversions of large strings.","solutions":["Validate the port is an integer in 1-65535 before calling AllowPort.","Fix the source config or request value that produced the out-of-range port.","If the caller has a string port, parse with strconv.Atoi and range-check, then surface a friendly validation error to the user."],"exampleFix":"// before\nport, _ := strconv.Atoi(req.Port)\npublic.AllowPort(port) // port==0 -> invalid port: 0\n// after\nport, err := strconv.Atoi(req.Port)\nif err != nil || port < 1 || port > 65535 {\n    return gerror.Newf(\"port must be an integer between 1 and 65535, got %q\", req.Port)\n}\nreturn public.AllowPort(port)","handlingStrategy":"validation","validationCode":"if port < 1 || port > 65535 {\n    return fmt.Errorf(\"port %d out of range 1-65535\", port)\n}\nerr := public.AllowPort(port)","typeGuard":"func isValidPort(p int) bool { return p >= 1 && p <= 65535 }","tryCatchPattern":"if err := public.AllowPort(port); err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid port:\") {\n        return fmt.Errorf(\"could not open firewall port %d: %w (check your input)\", port, err)\n    }\n    return err\n}","preventionTips":["Validate ports at the API boundary with a shared validator","Never ignore errors from strconv.Atoi when parsing port strings","Persist only validated port values in settings"],"tags":["validation","firewall","ports","go"],"backgroundTag":"invalid-port-number","analyzedSha":"fc36c76c050c3775c5e899faf7403cf0262d2744","analyzedAt":"2026-09-05T21:28:54.019Z","contentChangedAt":"2026-09-05T21:28:54.019Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}