Skip to content

Commit a654acb

Browse files
DeusDataKiborgik
andcommitted
fix(test-infra): grant the build-dir ACL by SID, not by name (DeusData#1532 follow-up)
The local Windows leg caught this within hours of merging DeusData#1532: on a machine that is not domain-joined, USERDOMAIN is the literal string WORKGROUP, and `WORKGROUP\test` is not a resolvable principal — WARN: build-dir DACL stamp (pre-wave) failed (user=WORKGROUP\test ...): WORKGROUP\test: No mapping between account names and security IDs was done. so the grant fails outright and the tree keeps its inherited `Authenticated Users:(M)`. The guard then correctly reports that cross-account mutation is still possible, and the whole leg stops. Since most Windows machines are not domain-joined, that is the common case, not the exotic one. DeusData#1532 was fixing a real bug in the other direction (a bare name resolves against the machine first, so a host named like its user grants to an empty principal). Both failures are name RESOLUTION failures, so this stops resolving names: the account is identified by SID, which is unambiguous everywhere. The SYSTEM and Administrators grants in these same commands have always used that form — the current user was the odd one out. Name lookup survives only as a fallback where PowerShell is unavailable. Applied to all three scripts carrying the stamp (run-tests-parallel, soak-test, memlab), since they share the failure and would otherwise drift apart. Co-Authored-By: Kiborgik <Kiborgik@users.noreply.github.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 8d487b7 commit a654acb

5 files changed

Lines changed: 128 additions & 13 deletions

File tree

‎scripts/memlab.sh‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,27 @@ if [[ "$BINARY" == *.exe ]] && command -v cygpath >/dev/null 2>&1 &&
3838
# Qualify with the domain: a bare name from coreutils `whoami` resolves
3939
# against the machine first, so on a host whose name equals the user's the
4040
# grant lands on an empty principal.
41-
ME="$(whoami | tr -d '\r')"
42-
if [ -n "${USERDOMAIN:-}" ]; then
43-
ME="${USERDOMAIN}\\${ME}"
44-
fi
41+
# Identify the account by SID, never by name. icacls resolves a bare name
42+
# against the machine first, so a host whose name equals the user's grants
43+
# to an empty principal (#1532); and a USERDOMAIN-qualified name is
44+
# UNRESOLVABLE on a workgroup machine — "WORKGROUP\test: No mapping between
45+
# account names and security IDs was done" — which fails the grant outright
46+
# and silently leaves the tree writable by Authenticated Users. A SID has
47+
# neither ambiguity, and the SYSTEM/Administrators grants below already use
48+
# this form. Name lookup remains only as a fallback where PowerShell is
49+
# unavailable.
50+
ME="$(powershell.exe -NoProfile -NonInteractive -Command \
51+
'[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value' 2>/dev/null |
52+
tr -d '\r\n')"
53+
case "${ME}" in
54+
S-1-*) ME="*${ME}" ;;
55+
*)
56+
ME="$(whoami | tr -d '\r')"
57+
if [ -n "${USERDOMAIN:-}" ]; then
58+
ME="${USERDOMAIN}\\${ME}"
59+
fi
60+
;;
61+
esac
4562
MSYS2_ARG_CONV_EXCL='*' icacls "$WIN_ROOT_W" /reset /Q >/dev/null 2>&1 || true
4663
if ! MSYS2_ARG_CONV_EXCL='*' icacls "$WIN_ROOT_W" /inheritance:r \
4764
/grant:r "${ME}:(OI)(CI)F" '*S-1-5-18:(OI)(CI)F' '*S-1-5-32-544:(OI)(CI)F' \

‎scripts/run-tests-parallel.sh‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,27 @@ stamp_windows_build_dir() {
6464
# (COMPUTERNAME=BUILD, user build) the grant lands on an empty principal
6565
# (BUILD\) and, combined with /inheritance:r above, locks this script out
6666
# of its own log directory.
67-
me="$(whoami | tr -d '\r')"
68-
if [ -n "${USERDOMAIN:-}" ]; then
69-
me="${USERDOMAIN}\\${me}"
70-
fi
67+
# Identify the account by SID, never by name. icacls resolves a bare name
68+
# against the machine first, so a host whose name equals the user's grants
69+
# to an empty principal (#1532); and a USERDOMAIN-qualified name is
70+
# UNRESOLVABLE on a workgroup machine — "WORKGROUP\test: No mapping between
71+
# account names and security IDs was done" — which fails the grant outright
72+
# and silently leaves the tree writable by Authenticated Users. A SID has
73+
# neither ambiguity, and the SYSTEM/Administrators grants below already use
74+
# this form. Name lookup remains only as a fallback where PowerShell is
75+
# unavailable.
76+
me="$(powershell.exe -NoProfile -NonInteractive -Command \
77+
'[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value' 2>/dev/null |
78+
tr -d '\r\n')"
79+
case "${me}" in
80+
S-1-*) me="*${me}" ;;
81+
*)
82+
me="$(whoami | tr -d '\r')"
83+
if [ -n "${USERDOMAIN:-}" ]; then
84+
me="${USERDOMAIN}\\${me}"
85+
fi
86+
;;
87+
esac
7188
# Normalize FIRST: some runner images stamp EXPLICIT (non-inherited)
7289
# Authenticated-Users ACEs onto the workspace tree, which /inheritance:r
7390
# cannot strip and /grant:r does not touch (it replaces only the granted

‎scripts/soak-test.sh‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,27 @@ soak_stamp_windows_dir() {
8080
# Qualify with the domain: a bare name from coreutils `whoami` resolves
8181
# against the machine first, so on a host whose name equals the user's the
8282
# grant lands on an empty principal.
83-
me="$(whoami | tr -d '\r')"
84-
if [ -n "${USERDOMAIN:-}" ]; then
85-
me="${USERDOMAIN}\\${me}"
86-
fi
83+
# Identify the account by SID, never by name. icacls resolves a bare name
84+
# against the machine first, so a host whose name equals the user's grants
85+
# to an empty principal (#1532); and a USERDOMAIN-qualified name is
86+
# UNRESOLVABLE on a workgroup machine — "WORKGROUP\test: No mapping between
87+
# account names and security IDs was done" — which fails the grant outright
88+
# and silently leaves the tree writable by Authenticated Users. A SID has
89+
# neither ambiguity, and the SYSTEM/Administrators grants below already use
90+
# this form. Name lookup remains only as a fallback where PowerShell is
91+
# unavailable.
92+
me="$(powershell.exe -NoProfile -NonInteractive -Command \
93+
'[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value' 2>/dev/null |
94+
tr -d '\r\n')"
95+
case "${me}" in
96+
S-1-*) me="*${me}" ;;
97+
*)
98+
me="$(whoami | tr -d '\r')"
99+
if [ -n "${USERDOMAIN:-}" ]; then
100+
me="${USERDOMAIN}\\${me}"
101+
fi
102+
;;
103+
esac
87104
if ! output=$(MSYS2_ARG_CONV_EXCL='*' icacls "$dir_w" /reset /Q 2>&1); then
88105
echo "FAIL: soak DACL normalize failed (dir=$dir_w user=$me)" >&2
89106
printf '%s\n' "$output" >&2

‎src/mcp/mcp.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ static const tool_def_t TOOLS[] = {
601601
"\"description\":\"Max enriched results per call. Default 10. Response includes "
602602
"'total_grep_matches' and 'total_results' so callers can detect truncation. No "
603603
"offset parameter — raise limit or narrow with file_pattern / path_filter to see more."
604-
"\",\"default\":10}},\"required\":[\"pattern\",\"project\"]}"},
604+
"\",\"default\":10,\"minimum\":1}},\"required\":[\"pattern\",\"project\"]}"},
605605

606606
{"list_projects", "List projects", "List all indexed projects",
607607
"{\"type\":\"object\",\"properties\":{}}"},
@@ -9634,6 +9634,14 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
96349634
char *path_filter = cbm_mcp_get_string_arg(args, "path_filter");
96359635
char *mode_str = cbm_mcp_get_string_arg(args, "mode");
96369636
int limit = cbm_mcp_get_int_arg(args, "limit", MCP_DEFAULT_LIMIT);
9637+
/* #1511: a negative limit flowed straight into the result cap and came back
9638+
* as the reported count ("results: -5"), which reads to an agent as a real
9639+
* answer rather than a rejected argument. The schema now declares
9640+
* minimum:1, but a schema is a request to the client, never a guarantee to
9641+
* the server — clamp here too. */
9642+
if (limit < 1) {
9643+
limit = MCP_DEFAULT_LIMIT;
9644+
}
96379645
int context_lines = cbm_mcp_get_int_arg(args, "context", 0);
96389646
bool use_regex = cbm_mcp_get_bool_arg(args, "regex");
96399647
uint64_t search_t0 = cbm_now_ms();

‎tests/test_mcp.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,6 +4012,60 @@ TEST(tool_search_code_missing_pattern) {
40124012
PASS();
40134013
}
40144014

4015+
/* #1511 (distilled from @lukiod's #1512): search_code echoed a negative limit
4016+
* back as the result count — "results: -5" — which an agent reads as an answer,
4017+
* not as a rejected argument. Both halves matter: the schema declares the bound
4018+
* so well-behaved clients never send it, and the handler clamps because a
4019+
* schema is a request to the client, never a guarantee to the server. */
4020+
TEST(tool_search_code_negative_limit_is_not_echoed_issue1511) {
4021+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4022+
char *resp =
4023+
cbm_mcp_server_handle(srv, "{\"jsonrpc\":\"2.0\",\"id\":35,\"method\":\"tools/call\","
4024+
"\"params\":{\"name\":\"search_code\","
4025+
"\"arguments\":{\"pattern\":\"func main\","
4026+
"\"project\":\"nonexistent\",\"limit\":-5}}}");
4027+
ASSERT_NOT_NULL(resp);
4028+
ASSERT_NULL(strstr(resp, "results: -5"));
4029+
free(resp);
4030+
cbm_mcp_server_free(srv);
4031+
PASS();
4032+
}
4033+
4034+
TEST(tool_search_code_limit_declares_a_minimum_issue1511) {
4035+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4036+
char *resp = cbm_mcp_server_handle(
4037+
srv, "{\"jsonrpc\":\"2.0\",\"id\":36,\"method\":\"tools/list\",\"params\":{}}");
4038+
ASSERT_NOT_NULL(resp);
4039+
4040+
yyjson_doc *doc = yyjson_read(resp, strlen(resp), 0);
4041+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
4042+
yyjson_val *result = root ? yyjson_obj_get(root, "result") : NULL;
4043+
yyjson_val *tools = result ? yyjson_obj_get(result, "tools") : NULL;
4044+
yyjson_val *minimum = NULL;
4045+
if (tools && yyjson_is_arr(tools)) {
4046+
size_t index, max;
4047+
yyjson_val *tool;
4048+
yyjson_arr_foreach(tools, index, max, tool) {
4049+
yyjson_val *name = yyjson_obj_get(tool, "name");
4050+
if (!name || !yyjson_is_str(name) || strcmp(yyjson_get_str(name), "search_code") != 0) {
4051+
continue;
4052+
}
4053+
yyjson_val *schema = yyjson_obj_get(tool, "inputSchema");
4054+
yyjson_val *props = schema ? yyjson_obj_get(schema, "properties") : NULL;
4055+
yyjson_val *limit = props ? yyjson_obj_get(props, "limit") : NULL;
4056+
minimum = limit ? yyjson_obj_get(limit, "minimum") : NULL;
4057+
break;
4058+
}
4059+
}
4060+
bool declared = minimum && yyjson_is_int(minimum) && yyjson_get_int(minimum) >= 1;
4061+
yyjson_doc_free(doc);
4062+
free(resp);
4063+
cbm_mcp_server_free(srv);
4064+
4065+
ASSERT_TRUE(declared);
4066+
PASS();
4067+
}
4068+
40154069
TEST(tool_search_code_no_project) {
40164070
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
40174071

@@ -10591,6 +10645,8 @@ SUITE(mcp) {
1059110645
RUN_TEST(tool_get_code_snippet_missing_qn);
1059210646
RUN_TEST(tool_get_code_snippet_not_found);
1059310647
RUN_TEST(tool_search_code_missing_pattern);
10648+
RUN_TEST(tool_search_code_negative_limit_is_not_echoed_issue1511);
10649+
RUN_TEST(tool_search_code_limit_declares_a_minimum_issue1511);
1059410650
RUN_TEST(tool_search_code_no_project);
1059510651
RUN_TEST(search_code_multi_word);
1059610652
RUN_TEST(search_code_scoped_path_with_spaces_issue687);

0 commit comments

Comments
 (0)