| 1 | /* |
| 2 | * gh_config.c - the string toolkit and the configuration store of |
| 3 | * plugin_github. |
| 4 | * |
| 5 | * Two jobs: |
| 6 | * 1. A growable byte buffer with real error returns + a JSON escaper. |
| 7 | * Tool output is text taken straight out of GitHub's JSON responses - |
| 8 | * attacker-influenced content a model will read - so every conversion |
| 9 | * here is bounded and the escaper never lets a quote or a control byte |
| 10 | * break a document. |
| 11 | * 2. config.json at mode 0600 inside the connector directory, with the |
| 12 | * documented resolution order GITHUB_TOKEN / GITHUB_API_BASE env > |
| 13 | * config.json > default. The SDK's sx_json_file_save() writes atomically |
| 14 | * but does not set a mode, so the mode is applied here explicitly |
| 15 | * (fchmod on the temp before rename where possible, chmod after). |
| 16 | * |
| 17 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 18 | */ |
| 19 | |
| 20 | #if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 21 | # define _WIN32_WINNT 0x0601 |
| 22 | #endif |
| 23 | #if !defined(_WIN32) |
| 24 | # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L |
| 25 | # undef _POSIX_C_SOURCE |
| 26 | # define _POSIX_C_SOURCE 200809L |
| 27 | # endif |
| 28 | # ifndef _DEFAULT_SOURCE |
| 29 | # define _DEFAULT_SOURCE 1 |
| 30 | # endif |
| 31 | #endif |
| 32 | |
| 33 | #include "gh.h" |
| 34 | |
| 35 | #include <ctype.h> |
| 36 | #include <stdarg.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <time.h> |
| 41 | |
| 42 | #if !defined(_WIN32) |
| 43 | # include <sys/stat.h> |
| 44 | #endif |
| 45 | |
| 46 | /* ------------------------------------------------------------------ buffer */ |
| 47 | |
| 48 | void gh_buf_init(gh_buf *b) |
| 49 | { |
| 50 | b->p = NULL; |
| 51 | b->n = 0; |
| 52 | b->cap = 0; |
| 53 | } |
| 54 | |
| 55 | int gh_buf_need(gh_buf *b, size_t extra) |
| 56 | { |
| 57 | size_t want; |
| 58 | char *np; |
| 59 | |
| 60 | if (b->n + extra + 1 <= b->cap) return 0; |
| 61 | want = b->cap ? b->cap : 256; |
| 62 | while (want < b->n + extra + 1) { |
| 63 | if (want > (size_t)1 << 30) { |
| 64 | want = b->n + extra + 1; |
| 65 | break; |
| 66 | } |
| 67 | want *= 2; |
| 68 | } |
| 69 | np = (char *)realloc(b->p, want); |
| 70 | if (!np) return -1; |
| 71 | b->p = np; |
| 72 | b->cap = want; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int gh_buf_add(gh_buf *b, const char *s, size_t len) |
| 77 | { |
| 78 | if (!s || len == 0) { |
| 79 | if (gh_buf_need(b, 0) != 0) return -1; |
| 80 | b->p[b->n] = '\0'; |
| 81 | return 0; |
| 82 | } |
| 83 | if (gh_buf_need(b, len) != 0) return -1; |
| 84 | memcpy(b->p + b->n, s, len); |
| 85 | b->n += len; |
| 86 | b->p[b->n] = '\0'; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int gh_buf_puts(gh_buf *b, const char *s) |
| 91 | { |
| 92 | return s ? gh_buf_add(b, s, strlen(s)) : gh_buf_add(b, NULL, 0); |
| 93 | } |
| 94 | |
| 95 | int gh_buf_putc(gh_buf *b, int c) |
| 96 | { |
| 97 | char ch = (char)c; |
| 98 | if (gh_buf_need(b, 1) != 0) return -1; |
| 99 | b->p[b->n++] = ch; |
| 100 | b->p[b->n] = '\0'; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int gh_buf_printf(gh_buf *b, const char *fmt, ...) |
| 105 | { |
| 106 | va_list ap, ap2; |
| 107 | int need; |
| 108 | |
| 109 | va_start(ap, fmt); |
| 110 | va_copy(ap2, ap); |
| 111 | need = vsnprintf(NULL, 0, fmt, ap); |
| 112 | va_end(ap); |
| 113 | if (need < 0) { va_end(ap2); return -1; } |
| 114 | if (gh_buf_need(b, (size_t)need) != 0) { va_end(ap2); return -1; } |
| 115 | vsnprintf(b->p + b->n, b->cap - b->n, fmt, ap2); |
| 116 | va_end(ap2); |
| 117 | b->n += (size_t)need; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | const char *gh_buf_str(const gh_buf *b) |
| 122 | { |
| 123 | return b->p ? b->p : ""; |
| 124 | } |
| 125 | |
| 126 | void gh_buf_free(gh_buf *b) |
| 127 | { |
| 128 | free(b->p); |
| 129 | b->p = NULL; |
| 130 | b->n = b->cap = 0; |
| 131 | } |
| 132 | |
| 133 | /* ------------------------------------------------------------------ helpers */ |
| 134 | |
| 135 | void gh_copy(char *dst, size_t capsz, const char *src) |
| 136 | { |
| 137 | size_t n; |
| 138 | if (!dst || capsz == 0) return; |
| 139 | if (!src) { dst[0] = '\0'; return; } |
| 140 | n = strlen(src); |
| 141 | if (n >= capsz) n = capsz - 1; |
| 142 | memcpy(dst, src, n); |
| 143 | dst[n] = '\0'; |
| 144 | } |
| 145 | |
| 146 | static int unreserved(int c) |
| 147 | { |
| 148 | return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
| 149 | (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_' || c == '~'; |
| 150 | } |
| 151 | |
| 152 | void gh_url_encode(const char *in, gh_buf *out) |
| 153 | { |
| 154 | size_t i; |
| 155 | if (!in) return; |
| 156 | for (i = 0; in[i]; i++) { |
| 157 | unsigned char c = (unsigned char)in[i]; |
| 158 | if (unreserved(c)) gh_buf_putc(out, c); |
| 159 | else gh_buf_printf(out, "%%%02X", c); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* Replace invalid UTF-8 with U+FFFD and escape for JSON. GitHub itself |
| 164 | * returns UTF-8, but a repo description is user data and a malformed byte |
| 165 | * sequence must not be able to break the tool reply the model parses. */ |
| 166 | int gh_json_str(gh_buf *b, const char *s) |
| 167 | { |
| 168 | size_t i; |
| 169 | |
| 170 | if (gh_buf_putc(b, '"') != 0) return -1; |
| 171 | if (!s) return gh_buf_putc(b, '"'); |
| 172 | for (i = 0; s[i]; i++) { |
| 173 | unsigned char ch = (unsigned char)s[i]; |
| 174 | if (ch == '"' || ch == '\\') { |
| 175 | if (gh_buf_putc(b, '\\') != 0 || gh_buf_putc(b, ch) != 0) return -1; |
| 176 | } else if (ch == '\n') { |
| 177 | if (gh_buf_add(b, "\\n", 2) != 0) return -1; |
| 178 | } else if (ch == '\r') { |
| 179 | if (gh_buf_add(b, "\\r", 2) != 0) return -1; |
| 180 | } else if (ch == '\t') { |
| 181 | if (gh_buf_add(b, "\\t", 2) != 0) return -1; |
| 182 | } else if (ch < 0x20) { |
| 183 | if (gh_buf_printf(b, "\\u%04x", ch) != 0) return -1; |
| 184 | } else if (ch < 0x80) { |
| 185 | if (gh_buf_putc(b, ch) != 0) return -1; |
| 186 | } else { |
| 187 | /* validate the UTF-8 sequence length and pass it through */ |
| 188 | int need = 0, k; |
| 189 | unsigned char cu = ch; |
| 190 | if ((cu & 0xE0) == 0xC0) need = 1; |
| 191 | else if ((cu & 0xF0) == 0xE0) need = 2; |
| 192 | else if ((cu & 0xF8) == 0xF0) need = 3; |
| 193 | if (need == 0) { |
| 194 | if (gh_buf_add(b, "\357\277\275", 3) != 0) return -1; /* U+FFFD */ |
| 195 | continue; |
| 196 | } |
| 197 | for (k = 1; k <= need; k++) { |
| 198 | unsigned char nx = (unsigned char)s[i + k]; |
| 199 | if (!s[i + k] || (nx & 0xC0) != 0x80) { need = -1; break; } |
| 200 | } |
| 201 | if (need < 0) { |
| 202 | if (gh_buf_add(b, "\357\277\275", 3) != 0) return -1; |
| 203 | continue; |
| 204 | } |
| 205 | for (k = 0; k <= need; k++) |
| 206 | if (gh_buf_putc(b, s[i + k]) != 0) return -1; |
| 207 | i += (size_t)need; |
| 208 | } |
| 209 | } |
| 210 | return gh_buf_putc(b, '"'); |
| 211 | } |
| 212 | |
| 213 | /* ------------------------------------------------------------- validation */ |
| 214 | |
| 215 | int gh_api_base_valid(const char *base, char *out, size_t outsz, |
| 216 | char *err, size_t errsz) |
| 217 | { |
| 218 | sx_url u; |
| 219 | if (!base || !*base) { |
| 220 | gh_copy(out, outsz, GH_DEFAULT_API_BASE); |
| 221 | return 0; |
| 222 | } |
| 223 | if (sx_url_parse(base, &u, err, errsz) != 0) return -1; |
| 224 | if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { |
| 225 | if (err && errsz) gh_copy(err, errsz, "api base must be http(s)"); |
| 226 | return -1; |
| 227 | } |
| 228 | if (strchr(u.path, '?')) { |
| 229 | if (err && errsz) gh_copy(err, errsz, "api base must not carry a query"); |
| 230 | return -1; |
| 231 | } |
| 232 | /* rebuild scheme://host[:port] without any path, dropping a "/" path */ |
| 233 | { |
| 234 | char tmp[300]; |
| 235 | if (u.port > 0 && u.port != (u.tls ? 443 : 80)) |
| 236 | snprintf(tmp, sizeof tmp, "%s://%s:%d", u.scheme, u.host, u.port); |
| 237 | else |
| 238 | snprintf(tmp, sizeof tmp, "%s://%s", u.scheme, u.host); |
| 239 | gh_copy(out, outsz, tmp); |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* ------------------------------------------------------------------ config */ |
| 245 | |
| 246 | /* The token is only ever read from the environment or written to disk here; |
| 247 | * no function in this file prints it anywhere. */ |
| 248 | |
| 249 | static void cfg_default(gh_config *cfg) |
| 250 | { |
| 251 | gh_copy(cfg->api_base, sizeof cfg->api_base, GH_DEFAULT_API_BASE); |
| 252 | cfg->token[0] = '\0'; |
| 253 | cfg->has_token = 0; |
| 254 | } |
| 255 | |
| 256 | int gh_config_load(gh_config *cfg) |
| 257 | { |
| 258 | yyjson_doc *doc = NULL; |
| 259 | const char *env; |
| 260 | |
| 261 | cfg_default(cfg); |
| 262 | |
| 263 | if (sx_json_file_load(GH_ID, GH_CONFIG_FILE, &doc) == 0 && doc) { |
| 264 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 265 | yyjson_val *v; |
| 266 | if (root && yyjson_is_obj(root)) { |
| 267 | v = yyjson_obj_get(root, "api_base"); |
| 268 | if (v && yyjson_is_str(v) && yyjson_get_str(v)[0]) { |
| 269 | char fixed[256]; |
| 270 | char err[128]; |
| 271 | if (gh_api_base_valid(yyjson_get_str(v), fixed, sizeof fixed, |
| 272 | err, sizeof err) == 0) |
| 273 | gh_copy(cfg->api_base, sizeof cfg->api_base, fixed); |
| 274 | /* a corrupt stored base falls back to the default silently: |
| 275 | * the page shows the resolved value, so the user sees it */ |
| 276 | } |
| 277 | v = yyjson_obj_get(root, "token"); |
| 278 | if (v && yyjson_is_str(v) && yyjson_get_str(v)[0]) |
| 279 | gh_copy(cfg->token, sizeof cfg->token, yyjson_get_str(v)); |
| 280 | } |
| 281 | yyjson_doc_free(doc); |
| 282 | } |
| 283 | cfg->has_token = cfg->token[0] != '\0'; |
| 284 | |
| 285 | /* env overrides config for both values; this is what the test suite and |
| 286 | * an operator's one-off run use */ |
| 287 | env = getenv("GITHUB_API_BASE"); |
| 288 | if (env && *env) { |
| 289 | char fixed[256], err[128]; |
| 290 | if (gh_api_base_valid(env, fixed, sizeof fixed, err, sizeof err) == 0) |
| 291 | gh_copy(cfg->api_base, sizeof cfg->api_base, fixed); |
| 292 | } |
| 293 | env = getenv("GITHUB_TOKEN"); |
| 294 | if (env && *env) { |
| 295 | gh_copy(cfg->token, sizeof cfg->token, env); |
| 296 | cfg->has_token = 1; |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* chmod after an atomic write: the SDK helper renames a temp file it created |
| 302 | * with the process umask, so the mode is fixed on the final path. On Windows |
| 303 | * the file inherits ACLs; chmod is a no-op there and not an error. */ |
| 304 | static void tighten_mode(const char *name) |
| 305 | { |
| 306 | #if !defined(_WIN32) |
| 307 | char path[1024]; |
| 308 | if (sx_connector_path(GH_ID, name, path, sizeof path) == 0) |
| 309 | chmod(path, 0600); |
| 310 | #else |
| 311 | (void)name; |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | int gh_config_save(const char *api_base, const char *token, int force_clear_token) |
| 316 | { |
| 317 | yyjson_doc *old = NULL; |
| 318 | yyjson_mut_doc *doc; |
| 319 | yyjson_mut_val *root; |
| 320 | char base[256], err[160]; |
| 321 | char keep_token[256]; |
| 322 | |
| 323 | base[0] = '\0'; |
| 324 | keep_token[0] = '\0'; |
| 325 | |
| 326 | /* carry the stored values forward unless the caller replaces them */ |
| 327 | if (sx_json_file_load(GH_ID, GH_CONFIG_FILE, &old) == 0 && old) { |
| 328 | yyjson_val *r = yyjson_doc_get_root(old); |
| 329 | if (r && yyjson_is_obj(r)) { |
| 330 | yyjson_val *t = yyjson_obj_get(r, "token"); |
| 331 | if (t && yyjson_is_str(t)) gh_copy(keep_token, sizeof keep_token, |
| 332 | yyjson_get_str(t)); |
| 333 | yyjson_val *a = yyjson_obj_get(r, "api_base"); |
| 334 | if (a && yyjson_is_str(a)) gh_copy(base, sizeof base, |
| 335 | yyjson_get_str(a)); |
| 336 | } |
| 337 | yyjson_doc_free(old); |
| 338 | } |
| 339 | if (!base[0]) gh_copy(base, sizeof base, GH_DEFAULT_API_BASE); |
| 340 | |
| 341 | if (api_base && *api_base) { |
| 342 | char fixed[256]; |
| 343 | if (gh_api_base_valid(api_base, fixed, sizeof fixed, err, sizeof err) != 0) |
| 344 | return -2; /* caller relays err */ |
| 345 | gh_copy(base, sizeof base, fixed); |
| 346 | } |
| 347 | if (token && *token) gh_copy(keep_token, sizeof keep_token, token); |
| 348 | if (force_clear_token) keep_token[0] = '\0'; |
| 349 | |
| 350 | doc = yyjson_mut_doc_new(NULL); |
| 351 | root = yyjson_mut_obj(doc); |
| 352 | yyjson_mut_doc_set_root(doc, root); |
| 353 | yyjson_mut_obj_add_strcpy(doc, root, "api_base", base); |
| 354 | yyjson_mut_obj_add_strcpy(doc, root, "token", keep_token); |
| 355 | { /* an honest timestamp costs nothing and makes the file diffable */ |
| 356 | char stamp[32]; |
| 357 | time_t now = time(NULL); |
| 358 | struct tm tmv; |
| 359 | #if defined(_WIN32) |
| 360 | gmtime_s(&tmv, &now); |
| 361 | #else |
| 362 | gmtime_r(&now, &tmv); |
| 363 | #endif |
| 364 | strftime(stamp, sizeof stamp, "%Y-%m-%dT%H:%M:%SZ", &tmv); |
| 365 | yyjson_mut_obj_add_strcpy(doc, root, "updated_utc", stamp); |
| 366 | } |
| 367 | if (sx_json_file_save(GH_ID, GH_CONFIG_FILE, doc) != 0) { |
| 368 | yyjson_mut_doc_free(doc); |
| 369 | return -1; |
| 370 | } |
| 371 | yyjson_mut_doc_free(doc); |
| 372 | tighten_mode(GH_CONFIG_FILE); |
| 373 | return 0; |
| 374 | } |