Skip to content

Commit 2803d6a

Browse files
authored
Merge pull request #2255 from DeusData/distill/1245-route-literal-exts
fix(routes): a document or log path is not an HTTP route literal
2 parents 8ab555e + 38d422f commit 2803d6a

3 files changed

Lines changed: 118 additions & 3 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,9 @@ static bool has_filesystem_extension(const char *path) {
757757
ext[ext_len] = '\0';
758758

759759
static const char *const hard_file_exts[] = {
760-
".cfg", ".conf", ".credentials", ".crt", ".db", ".env",
761-
".ini", ".key", ".pem", ".pid", ".properties", ".service",
762-
".sock", ".socket", ".sqlite", ".toml", NULL};
760+
".cfg", ".conf", ".credentials", ".crt", ".db", ".env", ".ini", ".key",
761+
".log", ".md", ".pdf", ".pem", ".pid", ".properties", ".rst", ".service",
762+
".sock", ".socket", ".sqlite", ".toml", ".txt", NULL};
763763
for (int i = 0; hard_file_exts[i]; i++) {
764764
if (path_ext_matches(ext, hard_file_exts[i])) {
765765
return true;

‎tests/test_infrascan.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ TEST(infrascan_service_pattern_match_keeps_real_library_qns) {
232232
PASS();
233233
}
234234

235+
/* Document/log extensions are never HTTP routes (distilled from PR #1245):
236+
* the arg-URL detector runs for every resolved call, so without the
237+
* extension guard `open("/new/file.txt")` minted a Route + HTTP_CALLS.
238+
* First segments deliberately avoid the filesystem-root list so ONLY the
239+
* extension guard can reject them; `/api/...` marker paths stay routes. */
240+
TEST(infrascan_http_route_literal_guard_rejects_document_extensions) {
241+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/new/file.txt", "open"));
242+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/docs/guide.md", "read"));
243+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/data/app.log", "write"));
244+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/fake/path.pdf", "open"));
245+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/docs/index.rst", "open"));
246+
/* Same extensions under a query string are still files. */
247+
ASSERT_FALSE(cbm_service_pattern_is_http_route_literal("/docs/guide.md?raw=1", "open"));
248+
/* Positive controls: extension-less routes and the same callees. */
249+
ASSERT_TRUE(cbm_service_pattern_is_http_route_literal("/api/items", "requests.get"));
250+
ASSERT_TRUE(cbm_service_pattern_is_http_route_literal("/docs/guide", "open"));
251+
PASS();
252+
}
253+
235254
TEST(infrascan_route_nodes_skip_bad_http_url_paths) {
236255
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/cbm_infrascan_route_guard");
237256
ASSERT_NOT_NULL(gb);
@@ -312,6 +331,7 @@ TEST(infrascan_http_calls_join_matching_handler_route) {
312331

313332
SUITE(infrascan) {
314333
RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths);
334+
RUN_TEST(infrascan_http_route_literal_guard_rejects_document_extensions);
315335
RUN_TEST(infrascan_service_pattern_match_rejects_ids_inside_words);
316336
RUN_TEST(infrascan_service_pattern_match_keeps_real_library_qns);
317337
RUN_TEST(infrascan_route_nodes_skip_bad_http_url_paths);

‎tests/test_pipeline.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8020,6 +8020,100 @@ TEST(pipeline_ts_config_object_url_http_calls_issue2235) {
80208020
PASS();
80218021
}
80228022

8023+
/* Count HTTP_CALLS-style edges whose url_path property contains `keyword`. */
8024+
static int count_edges_with_url_path(cbm_store_t *s, const char *project, const char *keyword) {
8025+
cbm_edge_t *es = NULL;
8026+
int n = 0;
8027+
cbm_store_find_edges_by_url_path(s, project, keyword, &es, &n);
8028+
if (es) {
8029+
cbm_store_free_edges(es, n);
8030+
}
8031+
return n;
8032+
}
8033+
8034+
/* Distilled from PR #1245: detect_url_in_args (parallel resolver, >= 50 files)
8035+
* runs for EVERY resolved call with no callee gating, so a filesystem path
8036+
* with a document/log extension passed to a project-defined open/read/write
8037+
* helper passed normalize_url_arg (second slash, no space) and the route
8038+
* literal guard, minting a Route node + an HTTP_CALLS edge for a file. The
8039+
* callees are project functions on purpose: a bare builtin `open(...)` never
8040+
* resolves to a target and so never reaches emit_service_edge at all. The
8041+
* first path segments avoid the filesystem-root list (etc/var/tmp/...) so
8042+
* only the extension guard can reject them. requests.get('/api/items') is
8043+
* the positive control that must keep its Route + HTTP_CALLS. */
8044+
TEST(pipeline_arg_url_rejects_document_file_paths) {
8045+
char tmp[256];
8046+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_arg_url_docs_XXXXXX");
8047+
if (!cbm_mkdtemp(tmp)) {
8048+
FAIL("tmpdir");
8049+
}
8050+
8051+
write_temp_file(tmp, "src/files.py",
8052+
"import requests\n"
8053+
"def open_file(path):\n"
8054+
" return path\n"
8055+
"def read_file(path):\n"
8056+
" return path\n"
8057+
"def write_file(path):\n"
8058+
" return path\n"
8059+
"def load_docs():\n"
8060+
" open_file('/new/file.txt')\n"
8061+
" read_file('/docs/guide.md')\n"
8062+
" write_file('/data/app.log')\n"
8063+
" read_file('/reports/summary.pdf')\n"
8064+
" open_file('/docs/index.rst')\n"
8065+
"def fetch_items():\n"
8066+
" return requests.get('/api/items')\n");
8067+
for (int i = 0; i < 52; i++) {
8068+
char name[64];
8069+
char body[128];
8070+
snprintf(name, sizeof(name), "src/filler%d.ts", i);
8071+
snprintf(body, sizeof(body), "export function filler%d(): number { return %d; }\n", i, i);
8072+
write_temp_file(tmp, name, body);
8073+
}
8074+
8075+
char *old_workers = getenv("CBM_WORKERS");
8076+
char *saved = old_workers ? strdup(old_workers) : NULL;
8077+
cbm_setenv("CBM_WORKERS", "4", 1);
8078+
8079+
char db_path[512];
8080+
snprintf(db_path, sizeof(db_path), "%s/arg_url_docs.db", tmp);
8081+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8082+
ASSERT_NOT_NULL(p);
8083+
ASSERT_EQ(cbm_pipeline_run(p), 0);
8084+
const char *project = cbm_pipeline_project_name(p);
8085+
8086+
cbm_store_t *s = cbm_store_open_path(db_path);
8087+
ASSERT_NOT_NULL(s);
8088+
8089+
/* Positive control first: the genuine HTTP call still mints its route. */
8090+
ASSERT_GTE(count_nodes_named(s, project, "/api/items"), 1);
8091+
ASSERT_GTE(count_edges_with_url_path(s, project, "/api/items"), 1);
8092+
/* No Route node for any filesystem document path... */
8093+
ASSERT_EQ(count_nodes_named(s, project, "/new/file.txt"), 0);
8094+
ASSERT_EQ(count_nodes_named(s, project, "/docs/guide.md"), 0);
8095+
ASSERT_EQ(count_nodes_named(s, project, "/data/app.log"), 0);
8096+
ASSERT_EQ(count_nodes_named(s, project, "/reports/summary.pdf"), 0);
8097+
ASSERT_EQ(count_nodes_named(s, project, "/docs/index.rst"), 0);
8098+
/* ...and no HTTP_CALLS edge carrying one as its url_path either. */
8099+
ASSERT_EQ(count_edges_with_url_path(s, project, "/new/file.txt"), 0);
8100+
ASSERT_EQ(count_edges_with_url_path(s, project, "/docs/guide.md"), 0);
8101+
ASSERT_EQ(count_edges_with_url_path(s, project, "/data/app.log"), 0);
8102+
ASSERT_EQ(count_edges_with_url_path(s, project, "/reports/summary.pdf"), 0);
8103+
ASSERT_EQ(count_edges_with_url_path(s, project, "/docs/index.rst"), 0);
8104+
8105+
cbm_store_close(s);
8106+
cbm_pipeline_free(p);
8107+
if (saved) {
8108+
cbm_setenv("CBM_WORKERS", saved, 1);
8109+
free(saved);
8110+
} else {
8111+
cbm_unsetenv("CBM_WORKERS");
8112+
}
8113+
th_rmtree(tmp);
8114+
PASS();
8115+
}
8116+
80238117
/* Native `fetch()` (#856), sequential path (< 50 files → pass_calls.c). A bare
80248118
* unqualified call to the global fetch API has no import and no local
80258119
* definition anywhere in this project, so registry resolution comes back
@@ -17575,6 +17669,7 @@ SUITE(pipeline) {
1757517669
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1757617670
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);
1757717671
RUN_TEST(pipeline_ts_config_object_url_http_calls_issue2235);
17672+
RUN_TEST(pipeline_arg_url_rejects_document_file_paths);
1757817673
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
1757917674
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
1758017675
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);

0 commit comments

Comments
 (0)