Files

214 lines
11 KiB
C

#ifndef GW_BLIZZARD_EGRESS_H
#define GW_BLIZZARD_EGRESS_H
/* Gitea #13 + owner 2026-11-27 ("any and all blizzard links that open browser or try to call blizzard, disabled"):
* the 70334 client contacts Blizzard on its own (login-screen breaking news, GDP ingest, telemetry, version, rpc)
* and its UI opens Blizzard web pages (support, account, checkout, nydus redirects). Every Blizzard host literal of
* WowB.exe / WowB_loader.dll (ASCII and UTF-16LE, 46 hosts on build 70334) is overwritten in the process image and
* in already-formatted heap copies with a same-length host under the reserved .invalid TLD (RFC 6761: never
* resolves), so a request fails as on a machine without network. A browser launch for such a host is refused by
* gw_blizzard_url_blocked (ShellExecute guard in blizzard_egress.c). Pure scanner here; the Windows writer is
* blizzard_egress.c. */
#include <stddef.h>
#include <stdint.h>
#include <string.h>
static const char *const gw_egress_hosts[] = {
"breaking-news.support.blizzard.com",
"breaking-news.support.battlenet.com.cn",
"wow.ingest.gdp.blizzard.com",
"telemetry-in.battle.net",
"telemetry-in.battlenet.com.cn",
/* full 70334 catalogue (strings -n 6 / -el of WowB.exe + WowB_loader.dll) */
"eu.battle.net", "us.battle.net", "kr.battle.net", "tw.battle.net", "www.battle.net", "bgs.battle.net",
"account.battle.net", "nydus.battle.net", "enGB.nydus.battle.net", "s.version.battle.net", "v1.rpc.battle.net",
"dev.bgs.cn.battle.net", "us.checkout.battle.net", "eu.checkout.battle.net", "kr.checkout.battle.net",
"us.checkout.integration.battle.net", "eu.checkout.integration.battle.net", "kr.checkout.integration.battle.net",
"us.account.integration.battle.net", "eu.account.integration.battle.net", "kr.account.integration.battle.net",
"account.integration.battle.net", "actual.integration.battle.net", "actual.staging.battle.net",
"support.blizzard.com", "worldofwarcraft.blizzard.com", "confluence.blizzard.com",
"us.apidev.blizzard.net", "t2jira.corp.blizzard.net", "oauth.web.blizzard.net",
"support.battlenet.com.cn", "account.battlenet.com.cn", "checkout.battlenet.com.cn", "excp.battlenet.com.cn",
"cn.version.battlenet.com.cn", "actual.battlenet.com.cn", "actual.staging.battlenet.com.cn",
"wow.blizzard.cn", "login.identity.blzdev.cn", "checkout-qa-cn.commerce.blzdev.cn", "profile.blzstatic.cn",
};
#define GW_EGRESS_HOST_COUNT (sizeof(gw_egress_hosts) / sizeof(gw_egress_hosts[0]))
/* Registrable Blizzard domains: a browser launch for any host under one of these is refused, catalogue or not. */
static const char *const gw_blizzard_domains[] = {
"blizzard.com", "battle.net", "battlenet.com", "battlenet.com.cn", "blizzard.net", "blizzard.cn", "blzdev.cn",
"blzstatic.cn", "worldofwarcraft.com", "blizzardgames.cn", "blizzard.co.kr", "invalid",
};
#define GW_BLIZZARD_DOMAIN_COUNT (sizeof(gw_blizzard_domains) / sizeof(gw_blizzard_domains[0]))
static inline int gw_egress_host_byte(uint8_t c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '.';
}
/* Character i of a string stored with `stride` bytes per character (1 = ASCII, 2 = UTF-16LE); -1 when the stored
* unit is not a plain byte character. */
static inline int gw_egress_char(const uint8_t *data, size_t i, size_t stride)
{
if (stride == 2 && data[i * 2 + 1] != 0) return -1;
return data[i * stride];
}
/* Next blocked host at or after byte offset `from`, characters `stride` bytes wide: returns its byte offset (or
* `size` when none) and sets *length to its length in characters. The match must be a whole host (preceded by '/',
* '.', or a non-host character, followed by a non-host character other than '.') so a longer name that merely
* contains one is left alone. */
/* Whole-host match of host h starting at byte offset `at` (same rules as the reference scan below). */
static inline int gw_egress_match_at(const uint8_t *data, size_t size, size_t at, size_t stride, size_t h, size_t *length)
{
const char *host = gw_egress_hosts[h];
size_t n = strlen(host), i, chars;
int before, after;
if (at + stride > size) return 0;
chars = (size - at) / stride;
if (gw_egress_char(data + at, 0, stride) != (uint8_t)host[0] || n > chars) return 0;
for (i = 0; i < n && gw_egress_char(data + at, i, stride) == (uint8_t)host[i]; ++i) {}
if (i != n) return 0;
before = at >= stride ? gw_egress_char(data + at - stride, 0, stride) : -1;
after = n < chars ? gw_egress_char(data + at, n, stride) : -1;
if (before >= 0 && gw_egress_host_byte((uint8_t)before) && before != '.') return 0;
if (after >= 0 && gw_egress_host_byte((uint8_t)after)) return 0;
*length = n;
return 1;
}
/* #119: the startup heap scan covers gigabytes, so the search must not try every host at every lowercase byte
* (that took ~30 s on the main thread before D3D init). Every catalogued host contains a 'b' that is followed by
* another character (tests check it): memchr finds 'b' bytes, a table of (host, offset of that 'b', next char)
* picks the few starts worth testing. Same results as gw_egress_find_stride_reference: lowest matching start,
* lowest host index on ties. */
typedef struct { uint16_t host, offset; uint8_t next; } gw_egress_anchor;
static gw_egress_anchor gw_egress_anchors[256];
static size_t gw_egress_anchor_count, gw_egress_max_length;
static inline void gw_egress_anchors_init(void)
{
size_t h, k, count = 0, longest = 0;
if (*(volatile size_t *)&gw_egress_anchor_count) return;
for (h = 0; h < GW_EGRESS_HOST_COUNT; ++h) {
const char *host = gw_egress_hosts[h];
size_t n = strlen(host);
if (n > longest) longest = n;
for (k = 0; k + 1 < n && count < 256; ++k)
if (host[k] == 'b') {
gw_egress_anchors[count].host = (uint16_t)h;
gw_egress_anchors[count].offset = (uint16_t)k;
gw_egress_anchors[count].next = (uint8_t)host[k + 1];
++count;
}
}
gw_egress_max_length = longest;
__atomic_store_n(&gw_egress_anchor_count, count, __ATOMIC_RELEASE); /* published last: readers see a full table */
}
static inline size_t gw_egress_find_stride(const uint8_t *data, size_t size, size_t from, size_t *length, size_t stride)
{
size_t best = size, best_len = 0, best_host = 0, p = from, i;
gw_egress_anchors_init();
while (p < size) {
const uint8_t *hit = (const uint8_t *)memchr(data + p, 'b', size - p);
int next;
if (!hit) break;
p = (size_t)(hit - data);
if (best < size && p > best + gw_egress_max_length * stride) break;
if (p + 2 * stride <= size && (next = gw_egress_char(data + p, 1, stride)) >= 0 && gw_egress_char(data + p, 0, stride) == 'b')
for (i = 0; i < gw_egress_anchor_count; ++i) {
const gw_egress_anchor *a = &gw_egress_anchors[i];
size_t start, len = 0;
if (a->next != (uint8_t)next || (size_t)a->offset * stride > p) continue;
start = p - (size_t)a->offset * stride;
if (start < from || start > best || (start == best && best_len && a->host >= best_host)) continue;
if (gw_egress_match_at(data, size, start, stride, a->host, &len)) {
best = start; best_len = len; best_host = a->host;
}
}
++p;
}
*length = best < size ? best_len : 0;
return best < size ? best : size;
}
static inline size_t gw_egress_find_stride_reference(const uint8_t *data, size_t size, size_t from, size_t *length, size_t stride)
{
size_t at;
for (at = from; at + stride <= size; ++at) {
size_t h;
if (data[at] < 'a' || data[at] > 'z') continue; /* every host starts with a lowercase letter */
for (h = 0; h < GW_EGRESS_HOST_COUNT; ++h) {
const char *host = gw_egress_hosts[h];
size_t n = strlen(host), i, chars = (size - at) / stride;
int before, after;
if (data[at] != (uint8_t)host[0] || n > chars) continue;
for (i = 0; i < n && gw_egress_char(data + at, i, stride) == (uint8_t)host[i]; ++i) {}
if (i != n) continue;
before = at >= stride ? gw_egress_char(data + at - stride, 0, stride) : -1;
after = n < chars ? gw_egress_char(data + at, n, stride) : -1;
if (before >= 0 && gw_egress_host_byte((uint8_t)before) && before != '.') continue;
if (after >= 0 && gw_egress_host_byte((uint8_t)after)) continue;
*length = n;
return at;
}
}
*length = 0;
return size;
}
static inline size_t gw_egress_find(const uint8_t *data, size_t size, size_t from, size_t *length)
{
return gw_egress_find_stride(data, size, from, length, 1);
}
/* Same-length replacement: "xxxx...x.invalid" (labels stay well under 63 bytes for these hosts). */
static inline void gw_egress_replacement(char *out, size_t length)
{
size_t i;
for (i = 0; i < length; ++i) out[i] = 'x';
if (length > 8) memcpy(out + length - 8, ".invalid", 8);
}
/* Writes the replacement over `length` characters at `at`, `stride` bytes per character. */
static inline void gw_egress_write(uint8_t *at, size_t length, size_t stride)
{
char replacement[64];
size_t i;
gw_egress_replacement(replacement, length);
for (i = 0; i < length; ++i) at[i * stride] = (uint8_t)replacement[i];
}
/* 1 when `url` (wide, may be NULL) is a web address whose host is or ends in a Blizzard domain (or a neutralised
* .invalid host): such a launch is refused instead of opening the browser. Local paths and Octo links pass. */
static inline int gw_blizzard_url_blocked(const uint16_t *url)
{
char host[256];
size_t i = 0, n = 0, d;
if (!url) return 0;
while (url[i] == ' ') ++i;
{ /* scheme: http, https, or a bare "www." / host-looking target */
static const char *const schemes[] = {"http://", "https://"};
size_t s, matched = 0;
for (s = 0; s < 2 && !matched; ++s) {
size_t k = 0;
while (schemes[s][k] && url[i + k] && (url[i + k] | 0x20) == (uint16_t)schemes[s][k]) ++k;
if (!schemes[s][k]) matched = k;
}
if (!matched) return 0;
i += matched;
}
while (url[i] && url[i] != '/' && url[i] != ':' && url[i] != '?' && url[i] != '#' && n + 1 < sizeof(host)) {
uint16_t c = url[i++];
if (c >= 'A' && c <= 'Z') c |= 0x20;
if (c > 0x7f) return 1; /* non-ASCII host: never one of ours */
if (c == '@') { n = 0; continue; } /* user info */
host[n++] = (char)c;
}
host[n] = 0;
while (n && host[n - 1] == '.') host[--n] = 0;
for (d = 0; d < GW_BLIZZARD_DOMAIN_COUNT; ++d) {
size_t m = strlen(gw_blizzard_domains[d]);
if (n == m && memcmp(host, gw_blizzard_domains[d], m) == 0) return 1;
if (n > m && host[n - m - 1] == '.' && memcmp(host + n - m, gw_blizzard_domains[d], m) == 0) return 1;
}
return 0;
}
#endif