Skip to content

Commit

Permalink
fix default listen/listenany port (JuliaLang#23699)
Browse files Browse the repository at this point in the history
secure default is to listen on localhost only
(as documented)

fix JuliaLang#23607
  • Loading branch information
vtjnash authored Sep 15, 2017
1 parent b9aed45 commit 775f1fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
14 changes: 8 additions & 6 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ function getipaddr()
end
end
ccall(:uv_free_interface_addresses, Void, (Ptr{UInt8}, Int32), addr, count)
return lo_present ? ip"127.0.0.1" : error("No networking interface available")
return lo_present ? localhost : error("No networking interface available")
end

##
Expand Down Expand Up @@ -741,15 +741,15 @@ connect!(sock::TCPSocket, addr::InetAddr) = connect!(sock, addr.host, addr.port)
Connect to the host `host` on port `port`.
"""
connect(sock::TCPSocket, port::Integer) = connect(sock,IPv4(127,0,0,1), port)
connect(port::Integer) = connect(IPv4(127,0,0,1), port)
connect(sock::TCPSocket, port::Integer) = connect(sock, localhost, port)
connect(port::Integer) = connect(localhost, port)

# Valid connect signatures for TCP
connect(host::AbstractString, port::Integer) = connect(TCPSocket(), host, port)
connect(addr::IPAddr, port::Integer) = connect(TCPSocket(), addr, port)
connect(addr::InetAddr) = connect(TCPSocket(), addr)

default_connectcb(sock,status) = nothing
default_connectcb(sock, status) = nothing

function connect!(sock::TCPSocket, host::AbstractString, port::Integer)
if sock.status != StatusInit
Expand Down Expand Up @@ -780,7 +780,7 @@ function listen(addr; backlog::Integer=BACKLOG_DEFAULT)
listen(sock; backlog=backlog)
return sock
end
listen(port::Integer; backlog::Integer=BACKLOG_DEFAULT) = listen(IPv4(UInt32(0)), port; backlog=backlog)
listen(port::Integer; backlog::Integer=BACKLOG_DEFAULT) = listen(localhost, port; backlog=backlog)
listen(host::IPAddr, port::Integer; backlog::Integer=BACKLOG_DEFAULT) = listen(InetAddr(host, port); backlog=backlog)

function listen(callback, server::Union{TCPSocket, UDPSocket})
Expand Down Expand Up @@ -822,6 +822,8 @@ end

## Utility functions

const localhost = ip"127.0.0.1"

"""
listenany([host::IPAddr,] port_hint) -> (UInt16, TCPServer)
Expand All @@ -847,7 +849,7 @@ function listenany(host::IPAddr, default_port)
end
end

listenany(default_port) = listenany(IPv4(UInt32(0)), default_port)
listenany(default_port) = listenany(localhost, default_port)

"""
getsockname(sock::Union{TCPServer, TCPSocket}) -> (IPAddr, UInt16)
Expand Down
45 changes: 34 additions & 11 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,46 @@ defaultport = rand(2000:4000)
for testport in [0, defaultport]
port = Channel(1)
tsk = @async begin
p, s = listenany(testport)
local (p, s) = listenany(testport)
@test p != 0
@test getsockname(s) == (Base.localhost, p)
put!(port, p)
sock = accept(s)
# test write call
write(sock,"Hello World\n")

# test "locked" println to a socket
@sync begin
for i in 1:100
@async println(sock, "a", 1)
for i in 1:3
sock = accept(s)
@test getsockname(sock) == (Base.localhost, p)
let peer = getpeername(sock)::Tuple{IPAddr, UInt16}
@test peer[1] == Base.localhost
@test 0 != peer[2] != p
end
# test write call
write(sock, "Hello World\n")

# test "locked" println to a socket
@sync begin
for i in 1:100
@async println(sock, "a", 1)
end
end
close(sock)
end
close(s)
close(sock)
end
wait(port)
@test read(connect(fetch(port)), String) == "Hello World\n" * ("a1\n"^100)
let p = fetch(port)
otherip = getipaddr()
if otherip != Base.localhost
@test_throws Base.UVError("connect", Base.UV_ECONNREFUSED) connect(otherip, p)
end
for i in 1:3
client = connect(p)
let name = getsockname(client)::Tuple{IPAddr, UInt16}
@test name[1] == Base.localhost
@test 0 != name[2] != p
end
@test getpeername(client) == (Base.localhost, p)
@test read(client, String) == "Hello World\n" * ("a1\n"^100)
end
end
wait(tsk)
end

Expand Down

0 comments on commit 775f1fc

Please sign in to comment.