Skip to content

Commit 1ed1bbc

Browse files
refactor: throw EACCES error for IP, throw EAI_FAIL for domain name.
1 parent ad1742f commit 1ed1bbc

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

installer/sandbox.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
115115
return real_connect(sockfd, addr, addrlen);
116116
}
117117

118-
/** 拦截 getaddrinfo() —— 精确匹配域名 */
118+
/** 拦截 getaddrinfo() —— 只拦截域名,不拦截纯 IP */
119119
int getaddrinfo(const char *node, const char *service,
120120
const struct addrinfo *hints, struct addrinfo **res) {
121121
static int (*real_getaddrinfo)(const char *, const char *,
@@ -126,10 +126,19 @@ int getaddrinfo(const char *node, const char *service,
126126
static char *banned_env = NULL;
127127
if (!banned_env) banned_env = load_banned_hosts();
128128

129-
if (banned_env && *banned_env && node && match_env_patterns(node, banned_env)) {
130-
fprintf(stderr, "[sandbox] 🚫 Access to host %s is banned\n", node);
131-
return EAI_FAIL;
129+
if (banned_env && *banned_env && node) {
130+
// 检测 node 是否是 IP
131+
struct in_addr ipv4;
132+
struct in6_addr ipv6;
133+
int is_ip = (inet_pton(AF_INET, node, &ipv4) == 1) ||
134+
(inet_pton(AF_INET6, node, &ipv6) == 1);
135+
136+
// 只对“非IP的域名”进行屏蔽
137+
if (!is_ip && match_env_patterns(node, banned_env)) {
138+
fprintf(stderr, "[sandbox] 🚫 Access to host %s is banned (DNS blocked)\n", node);
139+
return EAI_FAIL; // 模拟 DNS 层禁止
140+
}
132141
}
133142

134143
return real_getaddrinfo(node, service, hints, res);
135-
}
144+
}

0 commit comments

Comments
 (0)