Skip to content

Commit 4cea54f

Browse files
committed
fix(deploy): bound Helm rendering on hostile input, no partial or wrong release images
Review round 1 of DeusData#2677. Every case ends in an explicit unknown, never in a hang, an OOM, a partial or a wrong image. Bounds (helm_template.c): - A per-render step and output budget (10,000,000 steps, 64 MiB): a range over a huge int, a huge indent, defines that include each other in a fan stop the render; every probe of the template is UNKNOWN, also one captured before, and the trip is logged (helm.render.budget). Counted, never timed. On the 20 infra corpora no render reaches it. - go-yaml v2's alias limit: values whose decodes are mostly alias expansion (a billion laughs) do not parse, as in Helm. - --set: an index past strvals' MaxIndex (65536) or a key longer than the parser holds is no value (the release is unknown), never a padded list or a split key. Reads (pass_k8s.c, pass_terraform.c): a values file, Chart.yaml, kustomize valuesFile or Terraform file() named by path is read only when it is a discovered file of the repository -- never through a symlink out of it -- with the size cap and tree-sitter padding of every other read. A chart file that is there but not read makes the chart's values unknown, not empty. Wrong images: - Terraform number sets reach the chart as Terraform's exact decimal (1.10 -> "1.1"), not %.17g. - A release with a value source not read -- Flux valuesFrom, valuesFiles, postRenderers; kustomize additionalValuesFiles, a valuesMerge other than override, a valuesFile not read; Terraform dynamic "set" -- has no image of its own. - An aliased subchart renders with .Chart.Name its alias. - An UNKNOWN if / with / range makes what its bodies assign UNKNOWN and leaves the loop a break in them would; set / unset / merge poison the map they change (merge: the maps within too). A change that may reach values another part holds taints every later capture; one of a new value (fromYaml, deepCopy, an empty dict, a part of one) does not. Marks and secrets: - The chart edge reads default_of as JSON (list paths kept), builds its properties to size, and writes no edge whose mark it could not carry whole. - Build arguments with a secret (by name or value) are never kept; build edge properties and values-path marks are never cut. Tests: one per bound and per wrong-image case, each RED on d21949e by its assertion; a Helm chart joins the Kubernetes complexity fixture. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent d21949e commit 4cea54f

12 files changed

Lines changed: 1780 additions & 151 deletions

File tree

‎src/pipeline/helm_template.c‎

Lines changed: 811 additions & 28 deletions
Large diffs are not rendered by default.

‎src/pipeline/helm_template.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ typedef struct {
7575
/* Render template `rel` (text src, len) with `values` (its .Values; NULL:
7676
* an empty map), `chart` and the family's defines, filling `probes` (strings
7777
* in arena a). Values paths are dot-joined keys, a key's own `.` escaped
78-
* (`\.`), as --set writes them. */
79-
void cbm_helm_render(CBMArena *a, const char *rel, const char *src, size_t len,
78+
* (`\.`), as --set writes them. False when the render ran past its step or
79+
* output budget -- a template Helm would not finish either (logged): then
80+
* every capture is UNKNOWN. */
81+
bool cbm_helm_render(CBMArena *a, const char *rel, const char *src, size_t len,
8082
yyjson_mut_val *values, const cbm_helm_chart_meta_t *chart,
8183
const cbm_helm_defines_t *defs, cbm_helm_probe_t *probes, int nprobes);
8284

‎src/pipeline/pass_ci.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,8 @@ static int ci_compose_env(const ci_t *c, TSNode svc, const ci_env_t *dotenv, con
10861086

10871087
/* A compose build's `args` (a mapping, or a list of "K=V" items), their
10881088
* values interpolated, as a JSON object; NULL: none. An argument without a
1089-
* value (the environment's) or with an unknown one is left out. */
1089+
* value (the environment's), with an unknown one or with a secret (by name or
1090+
* value -- never kept in the graph, never an image) is left out. */
10901091
static const char *ci_compose_build_args(const ci_t *c, TSNode build, const ci_env_t *env) {
10911092
TSNode args = ts_node_is_null(build) ? build : ci_get(c, build, "args");
10921093
if (ts_node_is_null(args)) {
@@ -1109,13 +1110,19 @@ static const char *ci_compose_build_args(const ci_t *c, TSNode build, const ci_e
11091110
raw = ts_node_is_null(pairs[i].value) ? NULL : ci_scalar(c, pairs[i].value, NULL);
11101111
}
11111112
const char *value = raw ? ci_interp(c->a, raw, env) : NULL;
1112-
if (!key || !key[0] || !value) {
1113+
if (!key || !key[0] || !value || cbm_is_secret_binding(key, value)) {
11131114
continue;
11141115
}
1115-
char ek[CBM_SZ_256];
1116-
char ev[CBM_SZ_1K];
1117-
cbm_json_escape(ek, (int)sizeof(ek), key);
1118-
cbm_json_escape(ev, (int)sizeof(ev), value);
1116+
/* JSON escaping writes at most 6 bytes a byte: nothing is cut */
1117+
int kn = (int)(strlen(key) * 6 + SKIP_ONE);
1118+
int vn = (int)(strlen(value) * 6 + SKIP_ONE);
1119+
char *ek = cbm_arena_alloc(c->a, (size_t)kn);
1120+
char *ev = cbm_arena_alloc(c->a, (size_t)vn);
1121+
if (!ek || !ev) {
1122+
return NULL;
1123+
}
1124+
cbm_json_escape(ek, kn, key);
1125+
cbm_json_escape(ev, vn, value);
11191126
json = cbm_arena_sprintf(c->a, "%s%s\"%s\":\"%s\"", json ? json : "{", json ? "," : "", ek,
11201127
ev);
11211128
}

‎src/pipeline/pass_k8s.c‎

Lines changed: 166 additions & 64 deletions
Large diffs are not rendered by default.

‎src/pipeline/pass_shell.c‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,8 @@ static const char *sh_image_repo(sh_t *s, TSNode cmd, const sh_argv_t *a, int i)
24892489
}
24902490

24912491
/* JSON object `json` (NULL: none) with build argument `kv` ("K=V") added; a
2492-
* `K` alone (the environment's value) or an unknown word adds nothing. */
2492+
* `K` alone (the environment's value), an unknown word or a secret (by name
2493+
* or value -- never kept in the graph, never an image) adds nothing. */
24932494
static const char *sh_build_arg(sh_t *s, const char *json, const char *kv) {
24942495
const char *eq = kv ? strchr(kv, '=') : NULL;
24952496
if (!eq || eq == kv) {
@@ -2500,8 +2501,17 @@ static const char *sh_build_arg(sh_t *s, const char *json, const char *kv) {
25002501
return json;
25012502
}
25022503
}
2503-
char ev[CBM_SZ_1K];
2504-
cbm_json_escape(ev, (int)sizeof(ev), eq + SKIP_ONE);
2504+
const char *key = cbm_arena_strndup(s->a, kv, (size_t)(eq - kv));
2505+
if (!key || cbm_is_secret_binding(key, eq + SKIP_ONE)) {
2506+
return json;
2507+
}
2508+
/* JSON escaping writes at most 6 bytes a byte: nothing is cut */
2509+
int vn = (int)(strlen(eq + SKIP_ONE) * 6 + SKIP_ONE);
2510+
char *ev = cbm_arena_alloc(s->a, (size_t)vn);
2511+
if (!ev) {
2512+
return json;
2513+
}
2514+
cbm_json_escape(ev, vn, eq + SKIP_ONE);
25052515
const char *pair = cbm_arena_sprintf(s->a, "\"%.*s\":\"%s\"", (int)(eq - kv), kv, ev);
25062516
if (!pair) {
25072517
return json;
@@ -3763,17 +3773,25 @@ static int sh_link_build(cbm_pipeline_ctx_t *ctx, const sh_hit_t *x, CBMHashTabl
37633773
char esc_file[CBM_SZ_1K];
37643774
char esc_img[CBM_SZ_1K];
37653775
char ci[CBM_SZ_1K];
3766-
char props[CBM_SZ_4K];
37673776
cbm_json_escape(esc_file, (int)sizeof(esc_file), x->file);
37683777
cbm_json_escape(esc_img, (int)sizeof(esc_img), x->ext);
37693778
sh_ci_props(x, ci, sizeof(ci));
3770-
snprintf(props, sizeof(props),
3779+
/* sized to what it holds: default_of and build_args are never cut */
3780+
size_t pn = strlen(esc_file) + strlen(esc_img) + strlen(ci) +
3781+
(x->default_of ? strlen(x->default_of) : 0) +
3782+
(x->build_args ? strlen(x->build_args) : 0) + CBM_SZ_256;
3783+
char *props = cbm_alloc(CBM_MEM_CLASS_OTHER, pn);
3784+
if (!props) {
3785+
return 0;
3786+
}
3787+
snprintf(props, pn,
37713788
"{\"via\":\"build\",\"tool\":\"docker-build\",\"file\":\"%s\",\"image\":\"%s\"%s,"
37723789
"\"line\":%d%s%s%s%s}",
37733790
esc_file, esc_img, ci, x->line, x->default_of ? ",\"default_of\":" : "",
37743791
x->default_of ? x->default_of : "", x->build_args ? ",\"build_args\":" : "",
37753792
x->build_args ? x->build_args : "");
37763793
cbm_gbuf_insert_edge(ctx->gbuf, img, mod, "DEPENDS_ON", props);
3794+
cbm_free(CBM_MEM_CLASS_OTHER, props);
37773795
return SKIP_ONE;
37783796
}
37793797

‎src/pipeline/pass_terraform.c‎

Lines changed: 140 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "helpers.h"
3232
#include "lang_specs.h"
3333

34+
#include <ctype.h>
3435
#include <stdatomic.h>
3536
#include <stdint.h>
3637
#include <stdio.h>
@@ -330,9 +331,96 @@ static char *tf_heredoc(const tf_t *t, TSNode n) {
330331
return out;
331332
}
332333

334+
/* HCL number literal s (digits, a fraction, an exponent) as Terraform turns a
335+
* number into a string: exact decimal, no exponent, no trailing zeros (cty's
336+
* big.Float Text('f', -1); 1.10 -> "1.1", 2e3 -> "2000"). False when it is
337+
* no such literal or does not fit out. */
338+
static bool tf_number_text(const char *s, size_t n, char *out, size_t sz) {
339+
enum { TF_MAX_EXP = 4096 };
340+
char dig[CBM_SZ_256];
341+
size_t nd = 0;
342+
long exp = 0;
343+
bool frac = false;
344+
size_t i = 0;
345+
for (; i < n && (isdigit((unsigned char)s[i]) || s[i] == '.'); i++) {
346+
if (s[i] == '.') {
347+
if (frac) {
348+
return false;
349+
}
350+
frac = true;
351+
continue;
352+
}
353+
if (nd + SKIP_ONE >= sizeof(dig)) {
354+
return false;
355+
}
356+
dig[nd++] = s[i];
357+
exp -= frac ? SKIP_ONE : 0;
358+
}
359+
if (i < n) {
360+
if (s[i] != 'e' && s[i] != 'E') {
361+
return false;
362+
}
363+
i++;
364+
bool neg = i < n && s[i] == '-';
365+
i += i < n && (s[i] == '-' || s[i] == '+');
366+
long e = 0;
367+
if (i == n) {
368+
return false;
369+
}
370+
for (; i < n; i++) {
371+
if (!isdigit((unsigned char)s[i]) || e > TF_MAX_EXP) {
372+
return false;
373+
}
374+
e = e * 10 + (s[i] - '0');
375+
}
376+
exp += neg ? -e : e;
377+
}
378+
size_t b = 0;
379+
while (b < nd && dig[b] == '0') {
380+
b++;
381+
}
382+
if (b == nd) {
383+
return snprintf(out, sz, "0") == SKIP_ONE; /* every digit 0 (or none) */
384+
}
385+
size_t e = nd;
386+
while (dig[e - SKIP_ONE] == '0') { /* stops at dig[b], which is no 0 */
387+
e--;
388+
exp++;
389+
}
390+
size_t m = e - b; /* significant digits: dig[b, e) x 10^exp */
391+
size_t o = 0;
392+
long point = (long)m + exp; /* digits before the decimal point */
393+
size_t need = exp >= 0 ? m + (size_t)exp
394+
: point > 0 ? m + SKIP_ONE
395+
: m + PAIR_LEN + (size_t)-point;
396+
if (need + SKIP_ONE > sz) {
397+
return false;
398+
}
399+
if (point <= 0) {
400+
out[o++] = '0';
401+
out[o++] = '.';
402+
for (long z = 0; z < -point; z++) {
403+
out[o++] = '0';
404+
}
405+
}
406+
for (size_t k = 0; k < m; k++) {
407+
if (point > 0 && (long)k == point) {
408+
out[o++] = '.';
409+
}
410+
out[o++] = dig[b + k];
411+
}
412+
for (long z = 0; z < exp; z++) {
413+
out[o++] = '0';
414+
}
415+
out[o] = '\0';
416+
return true;
417+
}
418+
333419
/* An HCL literal as JSON: a string without interpolation, a number, a bool,
334-
* null, a tuple or object of those; NULL: anything else. */
335-
static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
420+
* null, a tuple or object of those; NULL: anything else. `num_text`: a number
421+
* as the string Terraform makes of it (a helm_release set value is a string),
422+
* else a float64. */
423+
static yyjson_mut_val *tf_json_v(const tf_t *t, yyjson_mut_doc *doc, TSNode n, bool num_text) {
336424
n = tf_unwrap(n);
337425
if (tf_type(n, "string_lit") || tf_type(n, "quoted_template")) {
338426
char buf[CBM_SZ_4K];
@@ -371,7 +459,11 @@ static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
371459
uint32_t b = ts_node_start_byte(n);
372460
uint32_t e = ts_node_end_byte(n);
373461
if (tf_type(n, "numeric_lit")) {
374-
char buf[CBM_SZ_64];
462+
char buf[CBM_SZ_256];
463+
if (num_text) {
464+
return tf_number_text(t->src + b, e - b, buf, sizeof(buf)) ? yyjson_mut_strcpy(doc, buf)
465+
: NULL;
466+
}
375467
snprintf(buf, sizeof(buf), "%.*s", (int)(e - b), t->src + b);
376468
return yyjson_mut_real(doc, strtod(buf, NULL));
377469
}
@@ -388,7 +480,7 @@ static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
388480
if (tf_type(c, "tuple_start") || tf_type(c, "tuple_end")) {
389481
continue;
390482
}
391-
yyjson_mut_val *v = tf_json(t, doc, c);
483+
yyjson_mut_val *v = tf_json_v(t, doc, c, num_text);
392484
if (!v) {
393485
return NULL;
394486
}
@@ -411,9 +503,9 @@ static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
411503
uint32_t kb = ts_node_start_byte(id);
412504
key = yyjson_mut_strncpy(doc, t->src + kb, ts_node_end_byte(id) - kb);
413505
} else {
414-
key = tf_json(t, doc, kn);
506+
key = tf_json_v(t, doc, kn, false);
415507
}
416-
yyjson_mut_val *val = tf_json(t, doc, vn);
508+
yyjson_mut_val *val = tf_json_v(t, doc, vn, num_text);
417509
if (!key || !yyjson_mut_is_str(key) || !val) {
418510
return NULL;
419511
}
@@ -424,38 +516,22 @@ static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
424516
return NULL;
425517
}
426518

427-
/* A repository file's text (cbm_alloc), NULL when unreadable. */
519+
static yyjson_mut_val *tf_json(const tf_t *t, yyjson_mut_doc *doc, TSNode n) {
520+
return tf_json_v(t, doc, n, false);
521+
}
522+
523+
static char *tf_read(const char *path, size_t *len);
524+
525+
/* A repository file's text (cbm_alloc; capped and padded like every file
526+
* read), NULL when unreadable -- or when it is no file discovery took in: a
527+
* symlink, or a path through one, is never followed out of the repository. */
428528
static char *tf_read_repo(const tf_t *t, const char *path, size_t *len) {
429-
char full[CBM_SZ_4K];
430-
snprintf(full, sizeof(full), "%s/%s", t->ctx->repo_path, path);
431-
FILE *f = cbm_fopen(full, "rb");
432-
if (!f) {
529+
if (!cbm_pipeline_file_node(t->ctx->gbuf, t->ctx->project_name, path)) {
433530
return NULL;
434531
}
435-
char *buf = NULL;
436-
size_t cap = 0;
437-
char chunk[CBM_SZ_4K];
438-
size_t r;
439-
*len = 0;
440-
while ((r = fread(chunk, SKIP_ONE, sizeof(chunk), f)) > 0) {
441-
if (*len + r + SKIP_ONE > cap) {
442-
cap = (*len + r + SKIP_ONE) * PAIR_LEN;
443-
char *g = cbm_realloc(CBM_MEM_CLASS_OTHER, buf, cap);
444-
if (!g) {
445-
cbm_free(CBM_MEM_CLASS_OTHER, buf);
446-
(void)fclose(f);
447-
return NULL;
448-
}
449-
buf = g;
450-
}
451-
memcpy(buf + *len, chunk, r);
452-
*len += r;
453-
}
454-
(void)fclose(f);
455-
if (buf) {
456-
buf[*len] = '\0';
457-
}
458-
return buf;
532+
char full[CBM_SZ_4K];
533+
int n = snprintf(full, sizeof(full), "%s/%s", t->ctx->repo_path, path);
534+
return n > 0 && (size_t)n < sizeof(full) ? tf_read(full, len) : NULL;
459535
}
460536

461537
/* A `values = [...]` item as values: a YAML string / heredoc, file(<path>)
@@ -512,22 +588,18 @@ static yyjson_mut_val *tf_values_item(const tf_t *t, yyjson_mut_doc *doc, TSNode
512588
: NULL;
513589
}
514590

515-
/* A `set` entry (name / value / type) applied to root; false when it is no
516-
* literal. */
591+
/* A `set` entry (name / value / type; read with numbers as text) applied to
592+
* root; false when it is no literal. */
517593
static bool tf_set_entry(yyjson_mut_doc *doc, yyjson_mut_val *root, yyjson_mut_val *entry) {
518594
yyjson_mut_val *nm = yyjson_mut_obj_get(entry, "name");
519595
yyjson_mut_val *val = yyjson_mut_obj_get(entry, "value");
520596
yyjson_mut_val *ty = yyjson_mut_obj_get(entry, "type");
521597
if (!yyjson_mut_is_str(nm) || !val) {
522598
return false;
523599
}
524-
char vbuf[CBM_SZ_64];
525600
const char *v = NULL;
526601
if (yyjson_mut_is_str(val)) {
527602
v = yyjson_mut_get_str(val);
528-
} else if (yyjson_mut_is_real(val)) {
529-
snprintf(vbuf, sizeof(vbuf), "%.17g", yyjson_mut_get_real(val));
530-
v = vbuf;
531603
} else if (yyjson_mut_is_bool(val)) {
532604
v = yyjson_mut_get_bool(val) ? "true" : "false";
533605
}
@@ -550,7 +622,7 @@ static yyjson_mut_val *tf_set_block(const tf_t *t, yyjson_mut_doc *doc, TSNode b
550622
}
551623
TSNode key = ts_node_named_child(at, 0);
552624
uint32_t b = ts_node_start_byte(key);
553-
yyjson_mut_val *v = tf_json(t, doc, ts_node_named_child(at, SKIP_ONE));
625+
yyjson_mut_val *v = tf_json_v(t, doc, ts_node_named_child(at, SKIP_ONE), true);
554626
if (!v) {
555627
return NULL;
556628
}
@@ -561,9 +633,29 @@ static yyjson_mut_val *tf_set_block(const tf_t *t, yyjson_mut_doc *doc, TSNode b
561633
return entry;
562634
}
563635

636+
/* Is block `a` a `dynamic "set"` / `dynamic "set_list"` -- entries made by a
637+
* for_each, never literal? */
638+
static bool tf_dynamic_set(const tf_t *t, TSNode a) {
639+
TSNode kw = ts_node_named_child(a, 0);
640+
TSNode label = ts_node_named_child(a, SKIP_ONE);
641+
if (!tf_type(a, "block") || ts_node_is_null(kw) || ts_node_is_null(label)) {
642+
return false;
643+
}
644+
uint32_t kb = ts_node_start_byte(kw);
645+
if (ts_node_end_byte(kw) - kb != strlen("dynamic") ||
646+
strncmp(t->src + kb, "dynamic", strlen("dynamic")) != 0) {
647+
return false;
648+
}
649+
uint32_t lb = ts_node_start_byte(label);
650+
size_t ll = ts_node_end_byte(label) - lb;
651+
return (ll == strlen("\"set\"") && strncmp(t->src + lb, "\"set\"", ll) == 0) ||
652+
(ll == strlen("\"set_list\"") && strncmp(t->src + lb, "\"set_list\"", ll) == 0);
653+
}
654+
564655
/* The values a helm_release body gives its chart -- `values` items
565656
* coalesced in order, then each `set` -- as JSON (cbm_alloc); NULL when a
566-
* part is no literal. set_sensitive is never read. */
657+
* part is no literal, or is made by a `dynamic` block. set_sensitive is
658+
* never read. */
567659
static char *tf_release_values(const tf_t *t, TSNode body, const char ***files, int *nfiles) {
568660
yyjson_mut_doc *doc = yyjson_mut_doc_new(cbm_json_writer_allocator());
569661
yyjson_mut_val *root = doc ? yyjson_mut_obj(doc) : NULL;
@@ -576,8 +668,10 @@ static char *tf_release_values(const tf_t *t, TSNode body, const char ***files,
576668
size_t kl = ts_node_is_null(key) ? 0 : ts_node_end_byte(key) - kb;
577669
bool is_set = kl == strlen("set") && strncmp(t->src + kb, "set", kl) == 0;
578670
bool is_values = kl == strlen("values") && strncmp(t->src + kb, "values", kl) == 0;
579-
/* set_list values are not read: the release's values are partial */
580-
if (kl == strlen("set_list") && strncmp(t->src + kb, "set_list", kl) == 0) {
671+
/* set_list and dynamic set values are not read: the release's
672+
* values would be partial */
673+
if ((kl == strlen("set_list") && strncmp(t->src + kb, "set_list", kl) == 0) ||
674+
tf_dynamic_set(t, a)) {
581675
ok = false;
582676
break;
583677
}
@@ -598,7 +692,7 @@ static char *tf_release_values(const tf_t *t, TSNode body, const char ***files,
598692
ok = entry && tf_set_entry(doc, root, entry);
599693
} else if (pass == SKIP_ONE && is_set && tf_type(a, "attribute")) {
600694
/* provider v3: set = [{ name, value, type }, ...] */
601-
yyjson_mut_val *list = tf_json(t, doc, ts_node_named_child(a, SKIP_ONE));
695+
yyjson_mut_val *list = tf_json_v(t, doc, ts_node_named_child(a, SKIP_ONE), true);
602696
ok = yyjson_mut_is_arr(list);
603697
size_t idx, max;
604698
yyjson_mut_val *entry;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v2
2+
name: cx
3+
version: 0.1.0
4+
appVersion: "1.0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{- define "cx.image" -}}
2+
{{- $reg := .registry -}}
3+
{{- printf "%s/%s:%s" $reg .repository .tag -}}
4+
{{- end -}}

0 commit comments

Comments
 (0)