Skip to content
suit Documentation
Login

Documentation

// Fuzz target for what `suit curate apply` reads out of a file it did not write
// and what it edits: the lines of a choices file, the outfit named for a project,
// and the line editor on a suit.conf that parses. Whatever the bytes: a line is
// blank, a choice, malformed with a message, or a folder with a message; a choice
// holds only names and a project a config accepts, and read again from its own
// words it is the same; the outfit name is a name of at most 128 bytes; and an
// edit of a list entry of a config that parses gives a config that parses, with
// that one entry changed and every other entry as it was. As a SKILL.md: a call
// edit leaves every harness line in its front matter and an auto edit none, and
// neither changes a file with no front matter.

#include "conf.h"
#include "curate.h"
#include "platform.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

static const char *const act_word[] = {"keep", "personal", "retire", "call", "auto"};
static const char *const kind_word[] = {"skill", "agent", "command"};

static void check_line(const char *line, size_t len)
{
    CurateChoice c;
    char msg[256] = "";
    ChoiceLine r = curate_choice_parse(line, len, &c, msg, sizeof msg);
    if (r == CHOICE_NONE) return;
    if (r == CHOICE_BAD || r == CHOICE_FOLDER) {
        ensure(msg[0] != '\0' && strlen(msg) < sizeof msg);
        return;
    }
    ensure(r == CHOICE_OK);
    ensure(c.kind >= 0 && c.kind <= 2);
    ensure(c.act < CHOICE_CALL || c.kind == 0);  // call and auto are for skills
    ensure(conf_name_ok(c.ward, strlen(c.ward), false) && conf_name_ok(c.name, strlen(c.name), false));
    char again[1024];
    [[maybe_unused]] int n;
    if (c.act == CHOICE_PERSONAL) {
        size_t pl = strlen(c.project);
        ensure(pl > 0 && pl <= CHOICE_PROJECT_MAX);
        ensure(c.project[0] != '~' && c.project[0] != '/' && strcmp(c.project, "-") != 0);
        for (size_t i = 0; i < pl; i++) ensure(strchr("._-/:@~", c.project[i]) != nullptr || (c.project[i] >= '0' && c.project[i] <= '9') || (c.project[i] >= 'a' && c.project[i] <= 'z') || (c.project[i] >= 'A' && c.project[i] <= 'Z'));
        n = snprintf(again, sizeof again, "personal %s %s %s %s", kind_word[c.kind], c.ward, c.name, c.project);
    } else {
        ensure(c.project[0] == '\0');
        n = snprintf(again, sizeof again, "%s %s %s %s", act_word[c.act], kind_word[c.kind], c.ward, c.name);
    }
    ensure(n > 0 && (size_t)n < sizeof again);
    [[maybe_unused]] CurateChoice d;
    [[maybe_unused]] ChoiceLine again_kind = curate_choice_parse(again, (size_t)n, &d, msg, sizeof msg);
    ensure(again_kind == CHOICE_OK);
    ensure(d.act == c.act && d.kind == c.kind && strcmp(d.ward, c.ward) == 0 && strcmp(d.name, c.name) == 0 && strcmp(d.project, c.project) == 0);

    char outfit[CONF_NAME_MAX + 1];
    unsigned k = (unsigned)(len % 7) + 1;
    size_t ol = curate_personal_outfit(c.ward, c.project, k, outfit);
    [[maybe_unused]] size_t wl = strlen(c.ward);
    if (ol == 0) {
        // only a wardrobe name that leaves no room for `personal.`, the dot and a suffix
        ensure(9 + wl + 1 + (k > 1 ? 2 : 0) > CONF_NAME_MAX);
    } else {
        ensure(ol == strlen(outfit) && ol <= CONF_NAME_MAX && ol >= 9);
        ensure(conf_name_ok(outfit, ol, false) && memcmp(outfit, "personal.", 9) == 0);
    }
    char home[CONF_NAME_MAX + 1];
    [[maybe_unused]] size_t hl = curate_home_outfit("attic", c.ward, home);
    ensure((hl == 0) == (6 + wl > CONF_NAME_MAX));
}

static Conf before;
static Conf after;
static char text[CONF_FILE_MAX + 8192];

static bool is_list_key(const char *key)
{
    return strcmp(key, "skills") == 0 || strcmp(key, "agents") == 0 || strcmp(key, "commands") == 0 || strcmp(key, "include") == 0 ||
        strcmp(key, "outfits") == 0;
}

static void check_edit(const uint8_t *data, size_t size)
{
    if (size > CONF_FILE_MAX) return;
    ConfError e;
    if (!conf_parse_part(&before, (const char *)data, size, &e)) return;
    uint32_t at = UINT32_MAX;
    for (uint32_t i = 0; i < before.nentries && at == UINT32_MAX; i++)
        if (is_list_key(before.entries[(i + size) % before.nentries].key)) at = (uint32_t)((i + size) % before.nentries);
    if (at == UINT32_MAX) return;
    const Entry *en = &before.entries[at];
    memcpy(text, data, size);
    size_t len = size;
    const char *words[2] = {"xx", "y-z"};
    size_t n = size % 3;  // 0 words removes the entry
    EditResult r = curate_edit_value(text, &len, sizeof text, en->line, words, n);
    if (r != EDIT_OK) return;
    if (len > CONF_FILE_MAX) return;  // the parser's limit, not the editor's
    [[maybe_unused]] bool reparsed = conf_parse_part(&after, text, len, &e);
    ensure(reparsed);
    ensure(after.nentries == before.nentries - (n == 0 ? 1u : 0u));
    [[maybe_unused]] uint32_t j = 0;
    for (uint32_t i = 0; i < before.nentries; i++) {
        if (i == at) {
            if (n == 0) continue;
            ensure(strcmp(after.entries[j].key, en->key) == 0);
            ensure(strcmp(after.entries[j].val, n == 1 ? "xx" : "xx y-z") == 0);
        } else {
            ensure(strcmp(after.entries[j].key, before.entries[i].key) == 0 && strcmp(after.entries[j].val, before.entries[i].val) == 0);
        }
        j++;
    }
    ensure(after.nsections == before.nsections);
    // The edits that add a line and a section, on the same text.
    size_t len2 = size;
    memcpy(text, data, size);
    if (curate_edit_insert(text, &len2, sizeof text, en->line, "include = zz-edit") == EDIT_OK) ensure(len2 > size);
    len2 = size;
    memcpy(text, data, size);
    // A section the file already has cannot be made again, and the limits of a file are the parser's.
    bool absent = true;
    for (uint32_t i = 0; i < before.nsections; i++) absent &= !(before.sections[i].kind == KIND_OUTFIT && strcmp(before.sections[i].arg[0], "zz-new") == 0);
    if (absent && curate_edit_append(text, &len2, sizeof text, "[outfit zz-new]", "skills = a") == EDIT_OK && len2 <= CONF_FILE_MAX &&
        before.kind_count[KIND_OUTFIT] < CONF_OUTFIT_MAX && before.nentries < CONF_ENTRY_MAX) {
        reparsed = conf_parse_part(&after, text, len2, &e);
        ensure(reparsed);
        ensure(after.nsections == before.nsections + 1);
        ensure(strcmp(after.sections[after.nsections - 1].arg[0], "zz-new") == 0);
    }
}

static char front[(1u << 16) + 1024];

static void check_front(const uint8_t *data, size_t size)
{
    if (size > 1u << 16) return;
    const char *const lines[] = {"disable-model-invocation: true", "x-hidden: yes"};
    uint64_t have = 0;
    [[maybe_unused]] FrontResult r = curate_front_has((const char *)data, size, lines, 2, &have);
    ensure(r != FRONT_FULL && have <= 3);
    size_t len = 0;
    FrontResult a = curate_front_edit((const char *)data, size, lines, 2, true, front, sizeof front, &len);
    ensure(a == r);
    if (a != FRONT_OK) return;
    [[maybe_unused]] uint64_t now = 0;
    ensure(curate_front_has(front, len, lines, 2, &now) == FRONT_OK && now == 3);
    ensure(len >= size);
    [[maybe_unused]] FrontResult t = curate_front_edit((const char *)data, size, lines, 2, false, front, sizeof front, &len);
    ensure(t == FRONT_OK && len <= size);
    ensure(curate_front_has(front, len, lines, 2, &now) == FRONT_OK && now == 0);
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    // the data as a choices file, line by line
    static char file[CHOICES_FILE_MAX > 65536 ? 65536 : CHOICES_FILE_MAX];
    size_t use = size < sizeof file ? size : sizeof file;
    memcpy(file, data, use);
    for (size_t pos = 0; pos < use;) {
        const char *nl = memchr(file + pos, '\n', use - pos);
        size_t end = nl != nullptr ? (size_t)(nl - file) : use;
        check_line(file + pos, end - pos);
        pos = end + 1;
    }
    (void)curate_choices_header(file, use);
    check_edit(data, size);
    check_front(data, size);
    return 0;
}