mirror of
https://github.com/garrytan/gstack.git
synced 2026-10-31 22:14:25 +01:00
* security: isolate every gstack Bun entry point from the audited checkout's .env and bunfig.toml Bun auto-loads .env, .env.local and .env.<NODE_ENV> from its cwd and runs a cwd bunfig.toml preload before the program starts. gstack runs inside the repo it audits, so a committed .env could steer state roots, the claude binary or spend caps, and a bunfig preload ran arbitrary code in every bin and hook. Every bun shebang, shell and skill caller, TypeScript spawn and compiled build now disables both (--no-env-file --config=/dev/null, bunIsolationArgs(), --no-compile-autoload-dotenv/bunfig), with a ratchet and hostile-fixture regressions. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: name the directly executed scripts in the isolation ratchet Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: follow the isolation flags in touchfiles, copy lists, recipe assertions and the harness pin Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: keep browse/src/bun-child-config.ts unchanged so the native cookie qualification still binds Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(windows): the empty bunfig outlives its process so the timeline hook's detached repair child can read it Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * v1.91.159.0 security: gstack's Bun entry points ignore the audited repo's .env and bunfig.toml (S1) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: plan-mode render pattern accepts the isolation flags Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
94 lines
4.2 KiB
Bash
Executable File
94 lines
4.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# gstack-learnings-log — append a learning to the project learnings file
|
|
# Usage: gstack-learnings-log '{"skill":"review","type":"pitfall","key":"n-plus-one","insight":"...","confidence":8,"source":"observed"}'
|
|
# Valid types: pattern, pitfall, preference, architecture, tool, operational, investigation
|
|
#
|
|
# Append-only storage. Duplicates (same key+type) are resolved at read time
|
|
# by gstack-learnings-search ("latest winner" per key+type).
|
|
set -euo pipefail
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
# Windows git-bash (#1950): pwd yields a POSIX path (/c/Users/...), which Bun
|
|
# on Windows cannot resolve as an ES module specifier in the import below.
|
|
# cygpath -m converts to C:/Users/... which Bun accepts.
|
|
case "$(uname -s)" in
|
|
MINGW*|MSYS*|CYGWIN*) command -v cygpath >/dev/null 2>&1 && SCRIPT_DIR="$(cygpath -m "$SCRIPT_DIR")" ;;
|
|
esac
|
|
eval "$("$SCRIPT_DIR/gstack-slug" 2>/dev/null)"
|
|
. "$(dirname "$0")/gstack-state-root.sh" 2>/dev/null || { echo "$0: cannot resolve the gstack state root: $(dirname "$0")/gstack-state-root.sh is missing. fix: reinstall with ./setup or /gstack-upgrade (docs/state-root.md)" >&2; exit 1; }
|
|
gstack_state_root_select
|
|
GSTACK_HOME="$_gstack_sr_root"
|
|
mkdir -p "$GSTACK_HOME/projects/$SLUG"
|
|
|
|
INPUT="$1"
|
|
|
|
# Validate and sanitize input. Errors surface (#1950): stderr is captured and
|
|
# printed on failure instead of swallowed — a silent exit 1 here cost Windows
|
|
# users every AI-logged learning.
|
|
TMPERR=$(mktemp "${TMPDIR:-/tmp}/gstack-learnings-log.XXXXXX") || { echo "gstack-learnings-log: could not create a temp file in ${TMPDIR:-/tmp}, so the learning was not recorded. Fix: point TMPDIR at a writable directory." >&2; exit 1; }
|
|
trap 'rm -f "$TMPERR"' EXIT
|
|
set +e
|
|
VALIDATED=$(printf '%s' "$INPUT" | GSTACK_LIB_DIR="$SCRIPT_DIR/../lib" bun --no-env-file --config=/dev/null -e "
|
|
const { hasInjection } = await import(process.env.GSTACK_LIB_DIR + '/jsonl-store.ts');
|
|
const raw = await Bun.stdin.text();
|
|
let j;
|
|
try { j = JSON.parse(raw); } catch { process.stderr.write('gstack-learnings-log: invalid JSON, skipping\n'); process.exit(1); }
|
|
|
|
// Field validation: type must be from allowed list
|
|
const ALLOWED_TYPES = ['pattern', 'pitfall', 'preference', 'architecture', 'tool', 'operational', 'investigation'];
|
|
if (!j.type || !ALLOWED_TYPES.includes(j.type)) {
|
|
process.stderr.write('gstack-learnings-log: invalid type \"' + (j.type || '') + '\", must be one of: ' + ALLOWED_TYPES.join(', ') + '\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Field validation: key must be alphanumeric, hyphens, underscores (no injection surface)
|
|
if (!j.key || !/^[a-zA-Z0-9_-]+$/.test(j.key)) {
|
|
process.stderr.write('gstack-learnings-log: invalid key, must be alphanumeric with hyphens/underscores only\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Field validation: confidence must be 1-10
|
|
const conf = Number(j.confidence);
|
|
if (!Number.isInteger(conf) || conf < 1 || conf > 10) {
|
|
process.stderr.write('gstack-learnings-log: confidence must be integer 1-10\n');
|
|
process.exit(1);
|
|
}
|
|
j.confidence = conf;
|
|
|
|
// Field validation: source must be from allowed list
|
|
const ALLOWED_SOURCES = ['observed', 'user-stated', 'inferred', 'cross-model'];
|
|
if (j.source && !ALLOWED_SOURCES.includes(j.source)) {
|
|
process.stderr.write('gstack-learnings-log: invalid source, must be one of: ' + ALLOWED_SOURCES.join(', ') + '\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Content sanitization: shared injection patterns (lib/jsonl-store.ts, D2A) —
|
|
// one audited list across learnings + decisions, no drift.
|
|
if (j.insight && hasInjection(j.insight)) {
|
|
process.stderr.write('gstack-learnings-log: insight contains suspicious instruction-like content, rejected\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Inject timestamp if not present
|
|
if (!j.ts) j.ts = new Date().toISOString();
|
|
|
|
// Mark trust level based on source
|
|
// user-stated = user explicitly told the agent this. All others are AI-generated.
|
|
j.trusted = j.source === 'user-stated';
|
|
|
|
console.log(JSON.stringify(j));
|
|
" 2>"$TMPERR")
|
|
VALIDATE_RC=$?
|
|
set -e
|
|
|
|
if [ $VALIDATE_RC -ne 0 ] || [ -z "$VALIDATED" ]; then
|
|
if [ -s "$TMPERR" ]; then
|
|
cat "$TMPERR" >&2
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
echo "$VALIDATED" >> "$GSTACK_HOME/projects/$SLUG/learnings.jsonl"
|
|
|
|
# gbrain-sync: enqueue for cross-machine sync (no-op if sync is off).
|
|
"$SCRIPT_DIR/gstack-brain-enqueue" "projects/$SLUG/learnings.jsonl" 2>/dev/null &
|