/* * gh_config.c - the string toolkit and the configuration store of * plugin_github. * * Two jobs: * 1. A growable byte buffer with real error returns + a JSON escaper. * Tool output is text taken straight out of GitHub's JSON responses - * attacker-influenced content a model will read - so every conversion * here is bounded and the escaper never lets a quote or a control byte * break a document. * 2. config.json at mode 0600 inside the connector directory, with the * documented resolution order GITHUB_TOKEN / GITHUB_API_BASE env > * config.json > default. The SDK's sx_json_file_save() writes atomically * but does not set a mode, so the mode is applied here explicitly * (fchmod on the temp before rename where possible, chmod after). * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if defined(_WIN32) && !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0601 #endif #if !defined(_WIN32) # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L # undef _POSIX_C_SOURCE # define _POSIX_C_SOURCE 200809L # endif # ifndef _DEFAULT_SOURCE # define _DEFAULT_SOURCE 1 # endif #endif #include "gh.h" #include #include #include #include #include #include #if !defined(_WIN32) # include #endif /* ------------------------------------------------------------------ buffer */ void gh_buf_init(gh_buf *b) { b->p = NULL; b->n = 0; b->cap = 0; } int gh_buf_need(gh_buf *b, size_t extra) { size_t want; char *np; if (b->n + extra + 1 <= b->cap) return 0; want = b->cap ? b->cap : 256; while (want < b->n + extra + 1) { if (want > (size_t)1 << 30) { want = b->n + extra + 1; break; } want *= 2; } np = (char *)realloc(b->p, want); if (!np) return -1; b->p = np; b->cap = want; return 0; } int gh_buf_add(gh_buf *b, const char *s, size_t len) { if (!s || len == 0) { if (gh_buf_need(b, 0) != 0) return -1; b->p[b->n] = '\0'; return 0; } if (gh_buf_need(b, len) != 0) return -1; memcpy(b->p + b->n, s, len); b->n += len; b->p[b->n] = '\0'; return 0; } int gh_buf_puts(gh_buf *b, const char *s) { return s ? gh_buf_add(b, s, strlen(s)) : gh_buf_add(b, NULL, 0); } int gh_buf_putc(gh_buf *b, int c) { char ch = (char)c; if (gh_buf_need(b, 1) != 0) return -1; b->p[b->n++] = ch; b->p[b->n] = '\0'; return 0; } int gh_buf_printf(gh_buf *b, const char *fmt, ...) { va_list ap, ap2; int need; va_start(ap, fmt); va_copy(ap2, ap); need = vsnprintf(NULL, 0, fmt, ap); va_end(ap); if (need < 0) { va_end(ap2); return -1; } if (gh_buf_need(b, (size_t)need) != 0) { va_end(ap2); return -1; } vsnprintf(b->p + b->n, b->cap - b->n, fmt, ap2); va_end(ap2); b->n += (size_t)need; return 0; } const char *gh_buf_str(const gh_buf *b) { return b->p ? b->p : ""; } void gh_buf_free(gh_buf *b) { free(b->p); b->p = NULL; b->n = b->cap = 0; } /* ------------------------------------------------------------------ helpers */ void gh_copy(char *dst, size_t capsz, const char *src) { size_t n; if (!dst || capsz == 0) return; if (!src) { dst[0] = '\0'; return; } n = strlen(src); if (n >= capsz) n = capsz - 1; memcpy(dst, src, n); dst[n] = '\0'; } static int unreserved(int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_' || c == '~'; } void gh_url_encode(const char *in, gh_buf *out) { size_t i; if (!in) return; for (i = 0; in[i]; i++) { unsigned char c = (unsigned char)in[i]; if (unreserved(c)) gh_buf_putc(out, c); else gh_buf_printf(out, "%%%02X", c); } } /* Replace invalid UTF-8 with U+FFFD and escape for JSON. GitHub itself * returns UTF-8, but a repo description is user data and a malformed byte * sequence must not be able to break the tool reply the model parses. */ int gh_json_str(gh_buf *b, const char *s) { size_t i; if (gh_buf_putc(b, '"') != 0) return -1; if (!s) return gh_buf_putc(b, '"'); for (i = 0; s[i]; i++) { unsigned char ch = (unsigned char)s[i]; if (ch == '"' || ch == '\\') { if (gh_buf_putc(b, '\\') != 0 || gh_buf_putc(b, ch) != 0) return -1; } else if (ch == '\n') { if (gh_buf_add(b, "\\n", 2) != 0) return -1; } else if (ch == '\r') { if (gh_buf_add(b, "\\r", 2) != 0) return -1; } else if (ch == '\t') { if (gh_buf_add(b, "\\t", 2) != 0) return -1; } else if (ch < 0x20) { if (gh_buf_printf(b, "\\u%04x", ch) != 0) return -1; } else if (ch < 0x80) { if (gh_buf_putc(b, ch) != 0) return -1; } else { /* validate the UTF-8 sequence length and pass it through */ int need = 0, k; unsigned char cu = ch; if ((cu & 0xE0) == 0xC0) need = 1; else if ((cu & 0xF0) == 0xE0) need = 2; else if ((cu & 0xF8) == 0xF0) need = 3; if (need == 0) { if (gh_buf_add(b, "\357\277\275", 3) != 0) return -1; /* U+FFFD */ continue; } for (k = 1; k <= need; k++) { unsigned char nx = (unsigned char)s[i + k]; if (!s[i + k] || (nx & 0xC0) != 0x80) { need = -1; break; } } if (need < 0) { if (gh_buf_add(b, "\357\277\275", 3) != 0) return -1; continue; } for (k = 0; k <= need; k++) if (gh_buf_putc(b, s[i + k]) != 0) return -1; i += (size_t)need; } } return gh_buf_putc(b, '"'); } /* ------------------------------------------------------------- validation */ int gh_api_base_valid(const char *base, char *out, size_t outsz, char *err, size_t errsz) { sx_url u; if (!base || !*base) { gh_copy(out, outsz, GH_DEFAULT_API_BASE); return 0; } if (sx_url_parse(base, &u, err, errsz) != 0) return -1; if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { if (err && errsz) gh_copy(err, errsz, "api base must be http(s)"); return -1; } if (strchr(u.path, '?')) { if (err && errsz) gh_copy(err, errsz, "api base must not carry a query"); return -1; } /* rebuild scheme://host[:port] without any path, dropping a "/" path */ { char tmp[300]; if (u.port > 0 && u.port != (u.tls ? 443 : 80)) snprintf(tmp, sizeof tmp, "%s://%s:%d", u.scheme, u.host, u.port); else snprintf(tmp, sizeof tmp, "%s://%s", u.scheme, u.host); gh_copy(out, outsz, tmp); } return 0; } /* ------------------------------------------------------------------ config */ /* The token is only ever read from the environment or written to disk here; * no function in this file prints it anywhere. */ static void cfg_default(gh_config *cfg) { gh_copy(cfg->api_base, sizeof cfg->api_base, GH_DEFAULT_API_BASE); cfg->token[0] = '\0'; cfg->has_token = 0; } int gh_config_load(gh_config *cfg) { yyjson_doc *doc = NULL; const char *env; cfg_default(cfg); if (sx_json_file_load(GH_ID, GH_CONFIG_FILE, &doc) == 0 && doc) { yyjson_val *root = yyjson_doc_get_root(doc); yyjson_val *v; if (root && yyjson_is_obj(root)) { v = yyjson_obj_get(root, "api_base"); if (v && yyjson_is_str(v) && yyjson_get_str(v)[0]) { char fixed[256]; char err[128]; if (gh_api_base_valid(yyjson_get_str(v), fixed, sizeof fixed, err, sizeof err) == 0) gh_copy(cfg->api_base, sizeof cfg->api_base, fixed); /* a corrupt stored base falls back to the default silently: * the page shows the resolved value, so the user sees it */ } v = yyjson_obj_get(root, "token"); if (v && yyjson_is_str(v) && yyjson_get_str(v)[0]) gh_copy(cfg->token, sizeof cfg->token, yyjson_get_str(v)); } yyjson_doc_free(doc); } cfg->has_token = cfg->token[0] != '\0'; /* env overrides config for both values; this is what the test suite and * an operator's one-off run use */ env = getenv("GITHUB_API_BASE"); if (env && *env) { char fixed[256], err[128]; if (gh_api_base_valid(env, fixed, sizeof fixed, err, sizeof err) == 0) gh_copy(cfg->api_base, sizeof cfg->api_base, fixed); } env = getenv("GITHUB_TOKEN"); if (env && *env) { gh_copy(cfg->token, sizeof cfg->token, env); cfg->has_token = 1; } return 0; } /* chmod after an atomic write: the SDK helper renames a temp file it created * with the process umask, so the mode is fixed on the final path. On Windows * the file inherits ACLs; chmod is a no-op there and not an error. */ static void tighten_mode(const char *name) { #if !defined(_WIN32) char path[1024]; if (sx_connector_path(GH_ID, name, path, sizeof path) == 0) chmod(path, 0600); #else (void)name; #endif } int gh_config_save(const char *api_base, const char *token, int force_clear_token) { yyjson_doc *old = NULL; yyjson_mut_doc *doc; yyjson_mut_val *root; char base[256], err[160]; char keep_token[256]; base[0] = '\0'; keep_token[0] = '\0'; /* carry the stored values forward unless the caller replaces them */ if (sx_json_file_load(GH_ID, GH_CONFIG_FILE, &old) == 0 && old) { yyjson_val *r = yyjson_doc_get_root(old); if (r && yyjson_is_obj(r)) { yyjson_val *t = yyjson_obj_get(r, "token"); if (t && yyjson_is_str(t)) gh_copy(keep_token, sizeof keep_token, yyjson_get_str(t)); yyjson_val *a = yyjson_obj_get(r, "api_base"); if (a && yyjson_is_str(a)) gh_copy(base, sizeof base, yyjson_get_str(a)); } yyjson_doc_free(old); } if (!base[0]) gh_copy(base, sizeof base, GH_DEFAULT_API_BASE); if (api_base && *api_base) { char fixed[256]; if (gh_api_base_valid(api_base, fixed, sizeof fixed, err, sizeof err) != 0) return -2; /* caller relays err */ gh_copy(base, sizeof base, fixed); } if (token && *token) gh_copy(keep_token, sizeof keep_token, token); if (force_clear_token) keep_token[0] = '\0'; doc = yyjson_mut_doc_new(NULL); root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); yyjson_mut_obj_add_strcpy(doc, root, "api_base", base); yyjson_mut_obj_add_strcpy(doc, root, "token", keep_token); { /* an honest timestamp costs nothing and makes the file diffable */ char stamp[32]; time_t now = time(NULL); struct tm tmv; #if defined(_WIN32) gmtime_s(&tmv, &now); #else gmtime_r(&now, &tmv); #endif strftime(stamp, sizeof stamp, "%Y-%m-%dT%H:%M:%SZ", &tmv); yyjson_mut_obj_add_strcpy(doc, root, "updated_utc", stamp); } if (sx_json_file_save(GH_ID, GH_CONFIG_FILE, doc) != 0) { yyjson_mut_doc_free(doc); return -1; } yyjson_mut_doc_free(doc); tighten_mode(GH_CONFIG_FILE); return 0; }