commit 357e978e25ac6113ea8a581bbac1ea8685d6a58c55043f90753f993d37dd6da9 from: Stefan Sperling date: Wed Jul 16 13:48:03 2025 UTC introduce gotsys_ref_name_is_valid() We need to enforce additional restrictions on reference names specified in gotsys.conf in order to prevent escaping to arbitrary gotd.conf syntax. Add test coverage. commit - 3c2cd970527dfff3fc4c31a69e76097bdb52e8e1e9efa82d372c71880aab43c7 commit + 357e978e25ac6113ea8a581bbac1ea8685d6a58c55043f90753f993d37dd6da9 blob - cf42cc4734496f9e53a1473413025b9fd7ad9f8dbd4e71f49b8e605db4fc97a2 blob + fc304129d4caf7ffb37a0fa72010f76bf6c0c61da0fe2e7ad045f930a5579f79 --- gotsys/gotsys.h +++ gotsys/gotsys.h @@ -121,6 +121,7 @@ struct gotsys_conf { void gotsys_conf_init(struct gotsys_conf *); const struct got_error *gotsys_conf_parse(const char *, struct gotsys_conf *, int *); +int gotsys_ref_name_is_valid(char *); void gotsys_authorized_key_free(struct gotsys_authorized_key *); void gotsys_authorized_keys_list_purge(struct gotsys_authorized_keys_list *); void gotsys_user_free(struct gotsys_user *); blob - a406754370f654a0dc80695b393920210af7543e32c278840830264febccb462 blob + f11e9734816079157637d23f9efb4eb38bf2beae346edacfe814c6d8cf1151bf --- gotsys/parse.y +++ gotsys/parse.y @@ -1122,6 +1122,33 @@ conf_user_authorized_key(char *keytype, char *keydata, return NULL; } +/* + * Reference name restrictions specific to gotsys.conf. + * Exclude symbols which could be used to escape from strings and write + * arbitrary gotd.conf snippets. Also exclude whitespace because newlines + * are relevant to the parser. + */ +int +gotsys_ref_name_is_valid(char *refname) +{ + const char *s; + const char forbidden[] = { '\'', '"', '{' , '}', '=' }; + size_t i; + + s = refname; + while (*s) { + for (i = 0; i < nitems(forbidden); i++) { + if (*s == forbidden[i]) + return 0; + } + if (isspace((unsigned char)s[0])) + return 0; + s++; + } + + return 1; +} + static int refname_is_valid(char *refname) { @@ -1131,7 +1158,8 @@ refname_is_valid(char *refname) return 0; } - if (!got_ref_name_is_valid(refname)) { + if (!got_ref_name_is_valid(refname) || + !gotsys_ref_name_is_valid(refname)) { yyerror("invalid reference name: %s", refname); return 0; } blob - 3e85fc33ab1a6edf767f6cadc57f5f9fbf4d30bed37e0646886b02eb6a3a2c37 blob + cf7569a70819c3a2164953a501734dcca7899fc56d774f7db8ef1f5a504b953b --- regress/gotsysd/test_gotsysd.sh +++ regress/gotsysd/test_gotsysd.sh @@ -1184,6 +1184,91 @@ EOF test_done "$testroot" "0" } +test_bad_ref_in_gotsysconf() { + local testroot=`test_init bad_ref_in_gotsysconf 1` + + got checkout -q $testroot/${GOTSYS_REPO} $testroot/wt >/dev/null + ret=$? + if [ $ret -ne 0 ]; then + echo "got checkout failed unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + crypted_vm_pw=`echo ${GOTSYSD_VM_PASSWORD} | encrypt | tr -d '\n'` + crypted_pw=`echo ${GOTSYSD_DEV_PASSWORD} | encrypt | tr -d '\n'` + sshkey=`cat ${GOTSYSD_SSH_PUBKEY}` + + # An attempt to send an invalid gotsys.conf file + cat > ${testroot}/wt/gotsys.conf < $testroot/stdout 2> $testroot/stderr + ret=$? + if [ $ret -eq 0 ]; then + echo "gotsys check succeeded unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + echo "gotsys: ${testroot}/wt/gotsys.conf: line 18: invalid reference name: refs/heads/mai\"n" \ + > $testroot/stderr.expected + cmp -s $testroot/stderr.expected $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stderr.expected $testroot/stderr + test_done "$testroot" "$ret" + return 1 + fi + + (cd ${testroot}/wt && got commit -m "commit a bad gotsys.conf" \ + >/dev/null) + local commit_id=`git_show_head $testroot/${GOTSYS_REPO}` + + got send -q -i ${GOTSYSD_SSH_KEY} -r ${testroot}/${GOTSYS_REPO} \ + > $testroot/stdout 2> $testroot/stderr + ret=$? + if [ $ret -eq 0 ]; then + echo "got send succeeded unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + cat > ${testroot}/stderr.expected <