From 84f6ee6ce439ebe7ffa52149d48de5217310cd92 Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Wed, 18 Nov 2026 20:11:37 +0300 Subject: [PATCH] auth: the pre-authorization of a UDP ASSOCIATE is not cached --- src/auth.c | 5 ++++- tests/cases/socks.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/auth.c b/src/auth.c index e37c783..5877320 100644 --- a/src/auth.c +++ b/src/auth.c @@ -125,7 +125,10 @@ int doauth(struct clientparam * param){ if(authfuncs->authorize && (res = (*authfuncs->authorize)(param))) return res; - if(param->srv->authcachetype && authfuncs->authenticate && authfuncs->authenticate != cacheauth && param->username && (!(param->srv->authcachetype&4) || (!param->pwtype && param->password))){ + /* A UDP ASSOCIATE is pre-authorized without looking at the + destination its rules depend on, so its verdict holds for + that association alone and is not kept for later requests. */ + if(param->srv->authcachetype && param->preauth != 1 && authfuncs->authenticate && authfuncs->authenticate != cacheauth && param->username && (!(param->srv->authcachetype&4) || (!param->pwtype && param->password))){ struct authcache ac={.username=""}; if(param->username) { diff --git a/tests/cases/socks.py b/tests/cases/socks.py index 69c3d7e..f444e39 100644 --- a/tests/cases/socks.py +++ b/tests/cases/socks.py @@ -179,3 +179,52 @@ def run(t): "a well-formed name is connected by name over SOCKS4a") stop() + # --- a pre-authorized association decides nothing for later ------------ + # A UDP ASSOCIATE is allowed without looking at the destination its rules + # name, so that verdict may not be kept in the auth cache and returned + # for a CONNECT later. + scache = t.free_port() + allowed = t.free_port() + denied = t.free_port() + t.start("socks_cacheacl", f""" + log + authcache user,pass,dstport 60 + auth cacheacl strong + users u:CL:p + allow u * * {allowed} + deny * + socks -p{scache} + """, ports=[scache]) + # both destinations are alive: what decides between them is the rule + stop = t.raw_server(allowed, b"from the allowed stand-in\r\n") + refuse = t.raw_server(denied, b"from the refused stand-in\r\n") + + def associate_declared(): + """The reply code of a UDP ASSOCIATE naming the refused destination.""" + with socket.create_connection(("127.0.0.1", scache), t.timeout) as s: + s.settimeout(t.timeout) + s.sendall(b"\x05\x01\x02") + reply = recvall(s, 2) + if len(reply) < 2 or reply[1] != 0x02: + return None + s.sendall(b"\x01\x01u\x01p") + reply = recvall(s, 2) + if len(reply) < 2 or reply[1] != 0: + return None + s.sendall(b"\x05\x03\x00\x01" + socket.inet_aton("127.0.0.1") + + struct.pack("!H", denied)) + reply = recvall(s, 4) + return reply[1] if len(reply) > 1 else None + + guarded = f"127.0.0.1:{scache}" + t.eq(None, t.socks_connect(guarded, "127.0.0.1", allowed, auth=("u", "p")), + "a CONNECT the rules allow is connected") + t.ne(None, t.socks_connect(guarded, "127.0.0.1", denied, auth=("u", "p")), + "a CONNECT the rules refuse is refused") + + t.eq(0, associate_declared(), + "the association itself is allowed without its destination") + t.ne(None, t.socks_connect(guarded, "127.0.0.1", denied, auth=("u", "p")), + "a CONNECT after the association is refused all the same") + stop() + refuse()