fix(Bun.serve): return EACCESS when we don't have perms (#7191)

* fix(Bun.serve): return EACCESS when we don't have perms

The error reported to js land when listening fails was always the same,
this adds a second one for EACCESS when we are not the super user.

Fixes: https://github.com/oven-sh/bun/issues/7187

* fix: adjust code to be only ran on linuz

* fix: correct typo

* fix: remove comment since its linux only now
This commit is contained in:
Liz
2023-11-19 19:38:33 +01:00
committed by GitHub
parent ef8b9efaa4
commit 778bad9dfd
2 changed files with 31 additions and 5 deletions

View File

@@ -14,6 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext;
const Fs = @import("../../fs.zig");
const Resolver = @import("../../resolver/resolver.zig");
const ast = @import("../../import_record.zig");
const Sys = @import("../../sys.zig");
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const logger = @import("root").bun.logger;
@@ -5601,11 +5602,25 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
if (error_instance == .zero) {
switch (this.config.address) {
.tcp => |tcp| {
error_instance = (JSC.SystemError{
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server"),
.code = bun.String.static("EADDRINUSE"),
.syscall = bun.String.static("listen"),
}).toErrorInstance(this.globalThis);
error_set: {
if (comptime Environment.isLinux) {
var rc: i32 = -1;
const code = Sys.getErrno(rc);
if (code == bun.C.E.ACCES) {
error_instance = (JSC.SystemError{
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "permission denied {s}:{d}", .{ tcp.hostname orelse "0.0.0.0", tcp.port }) catch "Failed to start server"),
.code = bun.String.static("EACCES"),
.syscall = bun.String.static("listen"),
}).toErrorInstance(this.globalThis);
break :error_set;
}
}
error_instance = (JSC.SystemError{
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server"),
.code = bun.String.static("EADDRINUSE"),
.syscall = bun.String.static("listen"),
}).toErrorInstance(this.globalThis);
}
},
.unix => |unix| {
error_instance = (JSC.SystemError{

View File

@@ -1304,3 +1304,14 @@ it("should response with HTTP 413 when request body is larger than maxRequestBod
server.stop(true);
});
if (process.platform === "linux")
it("should use correct error when using a root range port(#7187)", () => {
expect(() => {
const server = Bun.serve({
port: 1003,
fetch(req) {
return new Response("request answered");
},
});
}).toThrow("permission denied 0.0.0.0:1003");
});