From 0e57ddd3e102eb9c43bf10d6d50add3ac55b217e Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:26:40 +0100 Subject: [PATCH 1/9] Keep CONNECT proxy credentials out of target requests Skip the connect: wrapper when constructing HTTP requests and extracting path or database arguments. Handle http: and https: addresses without slashes consistently with the transport parser. Replace the client's duplicate request builder with the shared helper. Document the behavior and add regression tests for proxy credentials and alternate URI spellings. Discovered by Anthropic's OSS Scanner as ANT-2026-QRD73BWN. --- client/client.c | 31 ++----------------------------- src/comstack.c | 9 ++++++++- src/http.c | 40 ++++++++++++++++++++++++++++++---------- src/yaz/comstack.h | 1 + src/yaz/zgdu.h | 13 +++++++++---- test/test_comstack.c | 7 +++++++ test/test_zgdu.c | 41 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 44 deletions(-) diff --git a/client/client.c b/client/client.c index 81c93da9..d710e06e 100644 --- a/client/client.c +++ b/client/client.c @@ -203,7 +203,6 @@ static void close_session(void); static void marc_file_write(const char *buf, size_t sz); static void wait_and_handle_response(int one_response_only); -static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url); ODR getODROutputStream(void) { @@ -1411,7 +1410,7 @@ static int send_SRW_redirect(const char *uri) { const char *username = 0; const char *password = 0; - Z_GDU *gdu = get_HTTP_Request_url(out, uri); + Z_GDU *gdu = z_get_HTTP_Request_uri(out, uri, 0, 0); gdu->u.HTTP_Request->method = odr_strdup(out, "GET"); z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers, "Accept", @@ -2715,39 +2714,13 @@ static int cmd_init(const char *arg) return 2; } -static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url) -{ - Z_GDU *p = z_get_HTTP_Request(odr); - const char *host = url; - const char *cp0 = strstr(host, "://"); - const char *cp1 = 0; - if (cp0) - cp0 = cp0+3; - else - cp0 = host; - - cp1 = strchr(cp0, '/'); - if (!cp1) - cp1 = cp0 + strlen(cp0); - - if (cp0 && cp1) - { - char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1); - memcpy (h, cp0, cp1 - cp0); - h[cp1-cp0] = '\0'; - z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h); - } - p->u.HTTP_Request->path = odr_strdup(odr, *cp1 ? cp1 : "/"); - return p; -} - static WRBUF get_url(const char *uri, WRBUF username, WRBUF password, int *code, int show_headers) { WRBUF result = 0; ODR out = odr_createmem(ODR_ENCODE); ODR in = odr_createmem(ODR_DECODE); - Z_GDU *gdu = get_HTTP_Request_url(out, uri); + Z_GDU *gdu = z_get_HTTP_Request_uri(out, uri, 0, 0); gdu->u.HTTP_Request->method = odr_strdup(out, "GET"); if (username && password) diff --git a/src/comstack.c b/src/comstack.c index 6fec901b..888b4e71 100644 --- a/src/comstack.c +++ b/src/comstack.c @@ -58,7 +58,14 @@ int cs_get_error(COMSTACK cs, const char **details) void cs_get_host_args(const char *type_and_host, const char **args) { *args = ""; - if (!strncmp(type_and_host, "unix:", 5)) + if (!strncmp(type_and_host, "connect:", 8)) + { + /* As in cs_parse_host, the proxy part is not part of the target. */ + const char *cp = strchr(type_and_host, ','); + if (cp) + type_and_host = cp + 1; + } + else if (!strncmp(type_and_host, "unix:", 5)) { const char *cp = strchr(type_and_host + 5, ':'); if (!cp) diff --git a/src/http.c b/src/http.c index d6762c28..5aa7a7f5 100644 --- a/src/http.c +++ b/src/http.c @@ -223,6 +223,34 @@ Z_GDU *z_get_HTTP_Request(ODR o) } +/* Locate the host where cs_parse_host does, so that Host names what the + transport connects to: http: and https: need no slashes, and *uri is + advanced past a connect:[user:password@]proxy, wrapper. The wrapper is + for the transport only and must not appear in any request. */ +static const char *http_host_start(const char **uri) +{ + const char *cp; + + if (!strncmp(*uri, "connect:", 8)) + { + cp = strchr(*uri, ','); + if (cp) + *uri = cp + 1; + } + if (!strncmp(*uri, "http:", 5)) + cp = *uri + 5; + else if (!strncmp(*uri, "https:", 6)) + cp = *uri + 6; + else + { + cp = strstr(*uri, "://"); + return cp ? cp + 3 : *uri; + } + while (*cp == '/') + cp++; + return cp; +} + Z_GDU *z_get_HTTP_Request_host_path(ODR odr, const char *host, const char *path) @@ -233,12 +261,8 @@ Z_GDU *z_get_HTTP_Request_host_path(ODR odr, if (host) { - const char *cp0 = strstr(host, "://"); + const char *cp0 = http_host_start(&host); const char *cp1 = 0; - if (cp0) - cp0 = cp0+3; - else - cp0 = host; cp1 = strchr(cp0, '/'); if (!cp1) @@ -257,12 +281,8 @@ Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args, int use_full_uri) { Z_GDU *p = z_get_HTTP_Request(odr); - const char *cp0 = strstr(uri, "://"); + const char *cp0 = http_host_start(&uri); const char *cp1 = 0; - if (cp0) - cp0 = cp0+3; - else - cp0 = uri; cp1 = strchr(cp0, '/'); if (!cp1) diff --git a/src/yaz/comstack.h b/src/yaz/comstack.h index 4ccf8332..5b4bfed4 100644 --- a/src/yaz/comstack.h +++ b/src/yaz/comstack.h @@ -427,6 +427,7 @@ YAZ_EXPORT COMSTACK cs_create_host2(const char *vhost, int flags, void **vp, \param args required output pointer to borrowed arguments, or an empty string Returns the portion after the first path slash, skipping a :// separator. + A connect:proxy, wrapper is skipped first, as in cs_parse_host. For unix:path:args, returns args directly if that part contains no colon; otherwise extracts the target path. For example tcp:localhost:210/books yields books, and http://localhost/sru?version=1.2 yields sru?version=1.2. diff --git a/src/yaz/zgdu.h b/src/yaz/zgdu.h index fb05e085..0ee2f0cb 100644 --- a/src/yaz/zgdu.h +++ b/src/yaz/zgdu.h @@ -213,8 +213,10 @@ YAZ_EXPORT Z_GDU *z_get_HTTP_Request(ODR o); \returns a Z_GDU containing the request. Uses z_get_HTTP_Request defaults. The Host value excludes any prefix - through :// and any following slash and path. This constructor does not - validate or percent-encode its inputs; encoding performs validation. + through :// and any following slash and path. It is the host that + cs_parse_host selects: a leading connect:[user:password@]proxy, wrapper + is skipped, and http: and https: need no slashes. This constructor does + not validate or percent-encode its inputs; encoding performs validation. */ YAZ_EXPORT Z_GDU *z_get_HTTP_Request_host_path(ODR odr, const char *host, @@ -229,8 +231,11 @@ YAZ_EXPORT Z_GDU *z_get_HTTP_Request_host_path(ODR odr, Uses z_get_HTTP_Request defaults and adds Host. If args is NULL, uses the portion after the first slash following the URI authority, or an empty string if absent. The target is / followed by args, optionally - preceded by the URI prefix for proxy requests. This constructor does - not validate or percent-encode inputs; encoding performs validation. + preceded by the URI prefix for proxy requests. Host is the host that + cs_parse_host selects: http: and https: need no slashes, and a leading + connect:[user:password@]proxy, wrapper is for the transport only and + appears in neither Host nor the target. This constructor does not + validate or percent-encode inputs; encoding performs validation. */ YAZ_EXPORT Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args, diff --git a/test/test_comstack.c b/test/test_comstack.c index a85c7cd5..5211a615 100644 --- a/test/test_comstack.c +++ b/test/test_comstack.c @@ -546,6 +546,13 @@ static void tst_cs_get_host_args(void) YAZ_CHECK(arg && !strcmp(arg, "x")); cs_get_host_args("http:///x", &arg); YAZ_CHECK(arg && !strcmp(arg, "x")); + /* The proxy part of connect:proxy,target supplies no arguments. */ + cs_get_host_args("connect:proxy:8080,localhost:9999/x", &arg); + YAZ_CHECK(arg && !strcmp(arg, "x")); + cs_get_host_args("connect:user:pass/word@proxy:8080,http://y/x", &arg); + YAZ_CHECK(arg && !strcmp(arg, "x")); + cs_get_host_args("connect:user:pass/word@proxy:8080,localhost:9999", &arg); + YAZ_CHECK(arg && !strcmp(arg, "")); } static void tst_cs_get_error_tcp(void) diff --git a/test/test_zgdu.c b/test/test_zgdu.c index 984dc0c9..5c06695a 100644 --- a/test/test_zgdu.c +++ b/test/test_zgdu.c @@ -117,6 +117,46 @@ static void tst_request_validation(void) } } +static void tst_request_host(void) +{ + /* Host and path must match what the transport (cs_parse_host) uses. + The connect: wrapper, with its credentials, is for the proxy only. */ + static const char *uris[] = { + "example.org:8080/db", + "http://example.org:8080/db", + "https://example.org:8080/db", + "http:example.org:8080/db", + "https:example.org:8080/db", + "http:example.org:8080/db?url=http://other.example/x", + "connect:proxy.example:3128,http://example.org:8080/db", + "connect:user:secret@proxy.example:3128,example.org:8080/db", + "connect:user:secret@proxy.example:3128,http:example.org:8080/db", + "connect:user:secret@proxy.example:3128,https:example.org:8080/db", + "connect:user:se://cr/et@proxy.example:3128,http://example.org:8080/db" + }; + size_t i; + for (i = 0; i < sizeof(uris) / sizeof(*uris); i++) + { + int full; + for (full = 0; full <= 1; full++) + { + ODR odr = odr_createmem(ODR_ENCODE); + Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uris[i], 0, full); + Z_HTTP_Request *req = gdu->u.HTTP_Request; + const char *host = z_HTTP_header_lookup(req->headers, "Host"); + YAZ_CHECK(host && !strcmp(host, "example.org:8080")); + if (!full) + YAZ_CHECK(!strncmp(req->path, "/db", 3)); + YAZ_CHECK(!strstr(req->path, "proxy.example")); + odr_reset(odr); + gdu = z_get_HTTP_Request_host_path(odr, uris[i], "/other"); + host = z_HTTP_header_lookup(gdu->u.HTTP_Request->headers, "Host"); + YAZ_CHECK(host && !strcmp(host, "example.org:8080")); + odr_destroy(odr); + } + } +} + static void tst_cookies(void) { ODR odr = odr_createmem(ODR_ENCODE); @@ -714,6 +754,7 @@ int main (int argc, char **argv) YAZ_CHECK_LOG(); tst_http_framing(); tst_request_validation(); + tst_request_host(); tst_cookies(); tst_http_same_origin(); tst_http_redirect(); From ec3e96c6626ebccf234bdcdc47ce97bd9e987b7d Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:31:03 +0100 Subject: [PATCH 2/9] Match GFS docpath at path component boundaries Prevent a docpath such as docs from exposing sibling files and directories named docs-internal or docs.backup.txt. Requests for unrelated names now continue through normal SRU routing. Document the matching rules and add regression coverage while preserving the existing virtual-host and authentication tests. Discovered by Anthropic's OSS Scanner as ANT-2026-HHJON15T. --- doc/gfs-virtual.xml | 8 +++-- src/seshigh.c | 21 ++++++++---- test/test_gfs_vhosts.c | 74 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/doc/gfs-virtual.xml b/doc/gfs-virtual.xml index 34839385..03027b16 100644 --- a/doc/gfs-virtual.xml +++ b/doc/gfs-virtual.xml @@ -170,11 +170,15 @@ Specifies a path for local file access using HTTP. All URLs with - a leading prefix (/ excluded) that matches the value of docpath + a path (leading / excluded) that is the value of docpath + or begins with docpath followed by / are used for file access. For example, if the server is to offer access in directory xsl, the docpath would be xsl and all URLs of the form - http://host/xsl will result in a local file access. + http://host/xsl/... will result in a local file + access. URLs for other names that merely begin with + xsl, such as http://host/xslt/..., + are not. diff --git a/src/seshigh.c b/src/seshigh.c index 35b1b518..2151bffc 100644 --- a/src/seshigh.c +++ b/src/seshigh.c @@ -2045,6 +2045,20 @@ static int check_path(const char *path) return 1; } +/* check if path is docpath itself or below it (1); otherwise (0) */ +static int check_docpath(const char *path, const char *docpath) +{ + size_t len = strlen(docpath); + + if (*path != '/' || strncmp(path + 1, docpath, len)) + return 0; + if (len == 0 || docpath[len - 1] == '/') + return 1; + /* docpath "doc" is a prefix of /doc/a and /docs/a; only /doc/a is ours */ + path += 1 + len; + return *path == '/' || *path == '?' || *path == '\0'; +} + static char *read_file(const char *fname, ODR o, long *sz) { struct stat stat_buf; @@ -2104,12 +2118,7 @@ static void process_http_request(association *assoc, request *req) r = 1; } if (r == 2 && assoc->server && assoc->server->docpath - && hreq->path[0] == '/' - && - /* check if path is a proper prefix of documentroot */ - strncmp(hreq->path+1, assoc->server->docpath, - strlen(assoc->server->docpath)) - == 0) + && check_docpath(hreq->path, assoc->server->docpath)) { if (!check_path(hreq->path)) { diff --git a/test/test_gfs_vhosts.c b/test/test_gfs_vhosts.c index 1ea892b6..ea57c93e 100644 --- a/test/test_gfs_vhosts.c +++ b/test/test_gfs_vhosts.c @@ -187,14 +187,13 @@ static const char *addrstr(COMSTACK cs) return peer_address; } -static void send_search_args(association *assoc, const char *host, - const char *args, const char *headers, int status) +static void send_get_headers(association *assoc, const char *path, + const char *host, const char *headers, int status) { WRBUF wire = wrbuf_alloc(); ODR dec = odr_createmem(ODR_DECODE); - wrbuf_printf(wire, "GET /db?version=1.2&operation=searchRetrieve&" - "query=cql.allRecords%%3D1%s HTTP/1.1\r\nHost: %s\r\n%s\r\n", - args, host, headers); + wrbuf_printf(wire, "GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n", + path, host, headers); assoc->client_link->user = (void *) wrbuf_cstr(wire); assoc->cs_get_mask = EVENT_INPUT; ir_session(assoc->client_chan, EVENT_INPUT); @@ -222,9 +221,27 @@ static void send_search_args(association *assoc, const char *host, wrbuf_destroy(wire); } +static void send_search_args(association *assoc, const char *host, + const char *args, const char *headers, int status) +{ + WRBUF path = wrbuf_alloc(); + wrbuf_puts(path, "/db?version=1.2&operation=searchRetrieve&" + "query=cql.allRecords%3D1"); + wrbuf_puts(path, args); + send_get_headers(assoc, wrbuf_cstr(path), host, headers, status); + wrbuf_destroy(path); +} + +static void send_get(association *assoc, const char *path, const char *host, + int status) +{ + send_get_headers(assoc, path, host, "", status); +} + static void send_search(association *assoc, const char *host, int status) { - send_search_args(assoc, host, "", "", status); + send_get(assoc, "/db?version=1.2&operation=searchRetrieve&" + "query=cql.allRecords%3D1", host, status); } static void tst_switch(int nonnull) @@ -435,6 +452,50 @@ static void tst_auth(const char *intruder) control_block = saved; statserv_setcontrol(&control_block); } + +/* docpath names a file or directory; it is not a prefix of other names. */ +static void tst_docpath(void) +{ + struct comstack cs; + IOCHAN chan = iochan_create(-1, ir_session, EVENT_INPUT, 0); + association *assoc; + memset(servers, 0, sizeof(servers)); + memset(searches, 0, sizeof(searches)); + use_handle = 0; + servers[0].cb = control_block; + servers[0].cb.bend_init = init; + servers[0].host = "a.example"; + servers[0].docpath = "d"; + gfs_server_list = servers; + strcpy(control_block.xml_config, "in-memory"); + memset(&cs, 0, sizeof(cs)); + cs.protocol = PROTO_HTTP; + cs.f_get = get; + cs.f_more = more; + cs.f_addrstr = addrstr; + assoc = create_association(chan, &cs, 0); + iochan_setdata(chan, assoc); + + /* No such files, but these are file requests: not found. */ + send_get(assoc, "/d", "a.example", 404); + send_get(assoc, "/d/none.txt", "a.example", 404); + send_get(assoc, "/d?version=1.2&operation=searchRetrieve&" + "query=cql.allRecords%3D1", "a.example", 404); + YAZ_CHECK_EQ(searches[0], 0); + /* /db is not below d and goes to SRU. */ + send_search(assoc, "a.example", 200); + YAZ_CHECK_EQ(searches[0], 1); + + servers[0].docpath = "d/"; + send_get(assoc, "/d/none.txt", "a.example", 404); + send_search(assoc, "a.example", 200); + YAZ_CHECK_EQ(searches[0], 2); + destroy_association(assoc); + xfree(chan); + gfs_server_list = 0; + control_block.xml_config[0] = 0; + statserv_setcontrol(&control_block); +} #endif int main(int argc, char **argv) @@ -449,6 +510,7 @@ int main(int argc, char **argv) tst_alive(); tst_auth(""); tst_auth("&x-username=alice&x-password=wrong"); + tst_docpath(); xml_config_close(); #endif YAZ_CHECK_TERM; From 9a97b84d919438e13cce2e6ab27a614ca9cf1061 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:33:39 +0100 Subject: [PATCH 3/9] Make malformed BER dumps opt-in in GFS and yaz-ztest Require requestdetail logging for GFS malformed-PDU dumps and ztest logging for failed ILL APDU dumps. Keep the existing decode-error diagnostics while preventing unsolicited BER dumps at default or disabled logging levels. Discovered by Anthropic's OSS Scanner as ANT-2026-J9FET01M. --- src/seshigh.c | 9 +++++++-- ztest/ztest.c | 12 ++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/seshigh.c b/src/seshigh.c index 2151bffc..7ef1b92b 100644 --- a/src/seshigh.c +++ b/src/seshigh.c @@ -363,8 +363,13 @@ int ir_read(IOCHAN h, int event) (long) odr_offset(assoc->decode)); if (assoc->decode->error != OHTTP) { - yaz_log(YLOG_WARN, "PDU dump:"); - odr_dumpBER(yaz_log_file(), assoc->input_buffer, res); + /* odr_dumpBER is not filtered by yaz_log and writes a + line per BER element: requestdetail only */ + if (log_requestdetail) + { + yaz_log(log_requestdetail, "PDU dump:"); + odr_dumpBER(yaz_log_file(), assoc->input_buffer, res); + } request_release(req); do_close(assoc, Z_Close_protocolError, "Malformed package"); } diff --git a/ztest/ztest.c b/ztest/ztest.c index f58d710d..73a57610 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -547,10 +547,14 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) "Couldn't decode ILL APDU %s near %ld", odr_errmsg(odr_geterror(rr->decode)), (long) odr_offset(rr->decode)); - yaz_log(log_level, "PDU dump:"); - odr_dumpBER(yaz_log_file(), - (char *) r->u.single_ASN1_type->buf, - r->u.single_ASN1_type->len); + if (log_level) + { + yaz_log(log_level, "PDU dump:"); + odr_dumpBER( + yaz_log_file(), + (char *) r->u.single_ASN1_type->buf, + r->u.single_ASN1_type->len); + } ill_apdu = 0; } else From 01e499904cc02f8d5c15192b4e311ef4fa86b6e8 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:34:50 +0100 Subject: [PATCH 4/9] Reject incomplete file reads before sending their contents Return no buffer when a GFS static-file read is incomplete, allowing the existing HTTP 404 path to handle the failure. Also skip incomplete files during admin imports. This prevents uninitialized bytes beyond the data actually read from being transmitted. Discovered by Anthropic's OSS Scanner as ANT-2026-B4ZTDA18. --- client/admin.c | 1 + src/seshigh.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/client/admin.c b/client/admin.c index f16aa2cb..7beb3270 100644 --- a/client/admin.c +++ b/client/admin.c @@ -231,6 +231,7 @@ int cmd_adm_import(const char *arg) if (fread(oct->buf, 1, oct->len, inf) != (size_t) oct->len) { printf("Incomplete read of file %s\n", fname); + oct->buf = 0; } } if (fclose(inf)) diff --git a/src/seshigh.c b/src/seshigh.c index 7ef1b92b..2f744e24 100644 --- a/src/seshigh.c +++ b/src/seshigh.c @@ -2094,7 +2094,11 @@ static char *read_file(const char *fname, ODR o, long *sz) rewind(inf); buf = (char *) odr_malloc(o, *sz); if (fread(buf, 1, *sz, inf) != *sz) + { + /* file changed while reading; buf is only partly filled */ yaz_log(YLOG_WARN|YLOG_ERRNO, "short read %s", fname); + buf = 0; + } } fclose(inf); return buf; From 7279332ac4b24949d00f822b37d0975993c25b75 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:40:54 +0100 Subject: [PATCH 5/9] Escape control characters in GFS request logs Format request entries through a shared helper that escapes ASCII control characters and DEL while preserving UTF-8 text. This prevents peer-supplied fields from inserting extra log lines or terminal escapes. Add regression coverage for SRU query and HTTP path logging, and log missing SRU queries as a dash. Discovered by Anthropic's OSS Scanner as ANT-2026-OP0ZJV63. --- src/seshigh.c | 69 +++++++++++++++++++++++------- test/.gitignore | 1 + test/Makefile.am | 5 ++- test/test_gfs_log.c | 101 ++++++++++++++++++++++++++++++++++++++++++++ win/tests.mak | 1 + 5 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 test/test_gfs_log.c diff --git a/src/seshigh.c b/src/seshigh.c index 2f744e24..b2e2721f 100644 --- a/src/seshigh.c +++ b/src/seshigh.c @@ -118,6 +118,24 @@ static void get_logbits(void) } } +/** logs request entry as one line. Strings from the peer may hold control + characters; they are escaped, so that they do not start new log lines */ +static void log_request_line(WRBUF wr) +{ + const char *cp = wrbuf_cstr(wr); + WRBUF line = wrbuf_alloc(); + + for (; *cp; cp++) + { + if ((*cp > 0 && *cp < ' ') || *cp == 127) + wrbuf_printf(line, "\\x%02X", *cp & 0xff); + else + wrbuf_putc(line, *cp); + } + yaz_log(log_request, "%s", wrbuf_cstr(line)); + wrbuf_destroy(line); +} + static void wr_diag(WRBUF w, int error, const char *addinfo) { wrbuf_printf(w, "ERROR %d+", error); @@ -1440,8 +1458,9 @@ static void srw_bend_search(association *assoc, srw_res->resultSetId : "-"), (srw_req->startRecord ? *srw_req->startRecord : 1), srw_res->num_records); - yaz_log(log_request, "%s %s: %s", wrbuf_cstr(wr), srw_req->queryType, - srw_req->query); + wrbuf_printf(wr, " %s: ", srw_req->queryType); + wrbuf_puts(wr, srw_req->query ? srw_req->query : "-"); + log_request_line(wr); wrbuf_destroy(wr); } } @@ -1769,7 +1788,7 @@ static void srw_bend_scan(association *assoc, *srw_req->maximumTerms : 1)); /* there is no step size in SRU/W ??? */ wrbuf_printf(wr, "%s: %s ", srw_req->queryType, srw_req->scanClause); - yaz_log(log_request, "%s ", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } @@ -2120,7 +2139,17 @@ static void process_http_request(association *assoc, request *req) int num_diagnostic = 0; const char *host = z_HTTP_header_lookup(hreq->headers, "Host"); - yaz_log(log_request, "%s %s HTTP/%s", hreq->method, hreq->path, hreq->version); + if (log_request) + { + WRBUF wr = wrbuf_alloc(); + wrbuf_puts(wr, hreq->method); + wrbuf_puts(wr, " "); + wrbuf_puts(wr, hreq->path); + wrbuf_puts(wr, " HTTP/"); + wrbuf_puts(wr, hreq->version); + log_request_line(wr); + wrbuf_destroy(wr); + } if (!control_association(assoc, host, 0)) { p = z_get_HTTP_Response(o, 404); @@ -2789,20 +2818,27 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb) const char *open = req->idAuthentication->u.open; const char *slash = strchr(open, '/'); size_t len; + WRBUF wr = wrbuf_alloc(); if (slash) len = slash - open; else len = strlen(open); if (len > 100) len = 100; - yaz_log(log_request, "Auth open %.*s", (int) len, open); + wrbuf_puts(wr, "Auth open "); + wrbuf_write(wr, open, len); + log_request_line(wr); + wrbuf_destroy(wr); } else if (req->idAuthentication->which == Z_IdAuthentication_idPass) { const char *user = req->idAuthentication->u.idPass->userId; const char *group = req->idAuthentication->u.idPass->groupId; - yaz_log(log_request, "Auth idPass %s %s", - user ? user : "-", group ? group : "-"); + WRBUF wr = wrbuf_alloc(); + wrbuf_printf(wr, "Auth idPass %s %s", + user ? user : "-", group ? group : "-"); + log_request_line(wr); + wrbuf_destroy(wr); } else if (req->idAuthentication->which == Z_IdAuthentication_anonymous) @@ -2829,7 +2865,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb) (req->implementationVersion ? req->implementationVersion : "-") ); - yaz_log(log_request, "%s", wrbuf_cstr(wr)); + log_request_line(wr); wrbuf_destroy(wr); } return apdu; @@ -3311,6 +3347,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb, { WRBUF wr = wrbuf_alloc(); + wrbuf_printf(wr, "Search "); for (int i = 0 ; i < req->num_databaseNames; i++) { if (i) @@ -3327,7 +3364,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb, req->resultSetName, returnedrecs); yaz_query_to_wrbuf(wr, req->query); - yaz_log(log_request, "Search %s", wrbuf_cstr(wr)); + log_request_line(wr); wrbuf_destroy(wr); } return apdu; @@ -3419,7 +3456,7 @@ static Z_APDU *process_presentRequest(association *assoc, request *reqb) wrbuf_printf(wr, " %s " ODR_INT_PRINTF "+" ODR_INT_PRINTF " ", req->resultSetId, *req->resultSetStartPoint, *req->numberOfRecordsRequested); - yaz_log(log_request, "%s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } if (!resp->records) @@ -3652,7 +3689,7 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb) wrbuf_printf(wr, " "); yaz_scan_to_wrbuf(wr, req->termListAndStartPoint, bsrr->attributeset); - yaz_log(log_request, "%s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } return apdu; @@ -3728,7 +3765,7 @@ static Z_APDU *process_sortRequest(association *assoc, request *reqb) } wrbuf_printf(wr, ")->%s ",req->sortedResultSetName); - yaz_log(log_request, "%s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } return apdu; @@ -3811,7 +3848,7 @@ static Z_APDU *process_deleteRequest(association *assoc, request *reqb) wrbuf_printf(wr, "OK -"); for (i = 0; inum_resultSetList; i++) wrbuf_printf(wr, " %s ", req->resultSetList[i]); - yaz_log(log_request, "%s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } return apdu; @@ -3922,8 +3959,9 @@ static Z_APDU *process_ESRequest(association *assoc, request *reqb) if (log_request) { WRBUF wr = wrbuf_alloc(); + wrbuf_printf(wr, "EsRequest "); wrbuf_diags(wr, resp->num_diagnostics, resp->diagnostics); - yaz_log(log_request, "EsRequest %s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } } @@ -3940,8 +3978,9 @@ static Z_APDU *process_ESRequest(association *assoc, request *reqb) if (log_request) { WRBUF wr = wrbuf_alloc(); + wrbuf_printf(wr, "EsRequest "); wrbuf_diags(wr, resp->num_diagnostics, resp->diagnostics); - yaz_log(log_request, "EsRequest %s", wrbuf_cstr(wr) ); + log_request_line(wr); wrbuf_destroy(wr); } diff --git a/test/.gitignore b/test/.gitignore index ff184974..503f8bbe 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -61,6 +61,7 @@ test_zoom_scan test_zoom_send test_search_present test_sru_update +test_gfs_log test_gfs_vhosts test_charneg test_client_record diff --git a/test/Makefile.am b/test/Makefile.am index 5ef3e0ac..b153d81e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -14,7 +14,7 @@ check_PROGRAMS = test_client_record test_ccl test_charneg test_comstack test_cql test_shared_ptr test_soap1 test_soap2 test_solr test_sortspec test_sru_limits \ test_timing test_tpath test_url test_wrbuf \ test_xmalloc test_xml_include test_xmlquery test_zgdu \ - test_gfs_requests test_gfs_vhosts test_marc_read_iso2709 test_marc_read_sax \ + test_gfs_log test_gfs_requests test_gfs_vhosts test_marc_read_iso2709 test_marc_read_sax \ test_present_limits test_requestq test_sru_update test_zoom_facet test_zoom_record \ test_zoom_record_cache test_zoom_scan test_zoom_send @@ -134,6 +134,9 @@ test_sru_update_LDADD = ../src/libyaz_server.la ../src/libyaz.la test_marc_read_iso2709_SOURCES = test_marc_read_iso2709.c +test_gfs_log_SOURCES = test_gfs_log.c +test_gfs_log_LDADD = ../src/libyaz_server.la ../src/libyaz.la + # Keep HTTP handling with the statserv.c embedded by the test. Shared # libraries on macOS bind their internal calls to their own server state. test_gfs_vhosts_SOURCES = test_gfs_vhosts.c ../src/seshigh.c diff --git a/test/test_gfs_log.c b/test/test_gfs_log.c new file mode 100644 index 00000000..87638e11 --- /dev/null +++ b/test/test_gfs_log.c @@ -0,0 +1,101 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data + * See the file LICENSE for details. + */ +#if HAVE_CONFIG_H +#include +#endif +#include +#include +#include +#include +#include +#include "../src/session.h" + +#if YAZ_HAVE_XML2 +static int search(void *handle, bend_search_rr *rr) +{ + rr->hits = 0; + return 0; +} + +static bend_initresult *init(bend_initrequest *rr) +{ + static bend_initresult result; + rr->bend_search = search; + return &result; +} + +static int get(COMSTACK cs, char **buf, int *size) +{ + *buf = xstrdup((const char *) cs->user); + *size = (int) strlen(*buf); + return *size; +} + +static int more(COMSTACK cs) +{ + return 0; +} + +static const char *addrstr(COMSTACK cs) +{ + return "test"; +} + +/* Collect log entries, one per line. */ +static void log_handler(int level, const char *message, void *info) +{ + WRBUF log = (WRBUF) info; + wrbuf_puts(log, message); + wrbuf_puts(log, "\n"); +} + +/* Send a wire-format request through the actual HTTP/SRU frontend and + check that control characters from the peer are escaped in the log. */ +static void check_request_log(void) +{ + struct comstack cs; + IOCHAN chan = iochan_create(-1, ir_session, EVENT_INPUT, 0); + WRBUF log = wrbuf_alloc(); + association *assoc; + + memset(&cs, 0, sizeof(cs)); + cs.protocol = PROTO_HTTP; + cs.f_get = get; + cs.f_more = more; + cs.f_addrstr = addrstr; + cs.user = "GET /Default\x1b?version=1.2&operation=searchRetrieve" + "&query=a%0Ab%1Bc%7F%C3%A6&maximumRecords=0 HTTP/1.1\r\n" + "Host: localhost\r\n\r\n"; + yaz_log_set_handler(log_handler, log); + assoc = create_association(chan, &cs, 0); + iochan_setdata(chan, assoc); + assoc->cs_get_mask = EVENT_INPUT; + ir_session(chan, EVENT_INPUT); + yaz_log_set_handler(0, 0); + YAZ_CHECK(request_head(&assoc->outgoing) != 0); + + YAZ_CHECK(strstr(wrbuf_cstr(log), "GET /Default\\x1B?version=1.2") != 0); + YAZ_CHECK(strstr(wrbuf_cstr(log), + " cql: a\\x0Ab\\x1Bc\\x7F\xc3\xa6\n") != 0); + YAZ_CHECK(strchr(wrbuf_cstr(log), 27) == 0); + + destroy_association(assoc); + xfree(chan); + wrbuf_destroy(log); +} +#endif + +int main(int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); +#if YAZ_HAVE_XML2 + /* No log file. Entries are passed to log_handler only. */ + yaz_log_init_file(0); + yaz_log_init_level(yaz_log_mask_str("request")); + statserv_getcontrol()->bend_init = init; + check_request_log(); +#endif + YAZ_CHECK_TERM; +} diff --git a/win/tests.mak b/win/tests.mak index 74dd7d13..6ecc36c6 100644 --- a/win/tests.mak +++ b/win/tests.mak @@ -56,6 +56,7 @@ TEST_PROGRAMS = \ $(BINDIR)\test_xml_include.exe \ $(BINDIR)\test_xmlquery.exe \ $(BINDIR)\test_zgdu.exe \ + $(BINDIR)\test_gfs_log.exe \ $(BINDIR)\test_gfs_vhosts.exe \ $(BINDIR)\test_marc_read_iso2709.exe \ $(BINDIR)\test_marc_read_sax.exe \ From 7b09a7e7bf131108124c6657ca240b7b1d97ea18 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:45:28 +0100 Subject: [PATCH 6/9] Apply GFS receive limits before reading the first request Set the COMSTACK receive limit when creating an association. For XML configurations, use the largest limit among virtual servers available on that listener until server selection applies the specific limit. Add regression coverage for default and listener-specific limits and their narrowing after server selection. Discovered by Anthropic's OSS Scanner as ANT-2026-FQE92TA2. --- src/eventl.h | 1 + src/seshigh.c | 1 + src/statserv.c | 30 ++++++++++++++++++++++++++++++ test/test_gfs_vhosts.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/src/eventl.h b/src/eventl.h index 84e03808..c029ea31 100644 --- a/src/eventl.h +++ b/src/eventl.h @@ -85,6 +85,7 @@ void statserv_remove (IOCHAN pIOChannel); void statserv_reap_children(void); int statserv_request_timeout(void); int statserv_max_scan_terms(void); +int statserv_max_recv_bytes(int chan_id); #endif /* * Local variables: diff --git a/src/seshigh.c b/src/seshigh.c index b2e2721f..52103e9e 100644 --- a/src/seshigh.c +++ b/src/seshigh.c @@ -202,6 +202,7 @@ association *create_association(IOCHAN channel, COMSTACK link, anew->client_chan = channel; channel->deadline = time(0) + statserv_request_timeout(); anew->client_link = link; + cs_set_max_recv_bytes(link, statserv_max_recv_bytes(channel->chan_id)); anew->cs_get_mask = 0; anew->cs_put_mask = 0; anew->cs_accept_mask = 0; diff --git a/src/statserv.c b/src/statserv.c index 19380d58..e63e0a07 100644 --- a/src/statserv.c +++ b/src/statserv.c @@ -395,6 +395,36 @@ int control_association(association *assoc, const char *host, int force_open) return 1; } +/* control_association selects the server, and with it the limit, from the + first message of a session. Until then, the limit is the largest one of + the servers that the listener can select. */ +int statserv_max_recv_bytes(int chan_id) +{ + int max_recv_bytes = 0; + if (control_block.xml_config[0]) + { + struct gfs_server *gfs; + for (gfs = gfs_server_list; gfs; gfs = gfs->next) + { + int listen_match = 0; + if (!gfs->listen_ref) + listen_match = 1; + else + { + int i; + for (i = 0; gfs->listen_ref[i] != -1; i++) + if (gfs->listen_ref[i] == chan_id) + listen_match = 1; + } + if (listen_match && gfs->cb.maxrecordsize > max_recv_bytes) + max_recv_bytes = gfs->cb.maxrecordsize; + } + } + if (max_recv_bytes == 0) + max_recv_bytes = control_block.maxrecordsize; + return max_recv_bytes; +} + #if YAZ_HAVE_XML2 static void xml_config_read(const char *base_path) { diff --git a/test/test_gfs_vhosts.c b/test/test_gfs_vhosts.c index ea57c93e..6d5389c4 100644 --- a/test/test_gfs_vhosts.c +++ b/test/test_gfs_vhosts.c @@ -496,6 +496,43 @@ static void tst_docpath(void) control_block.xml_config[0] = 0; statserv_setcontrol(&control_block); } + +/* The first message of a session is received before a server is selected. */ +static void tst_recv_limit(void) +{ + static int listen_b[] = {2, -1}; + struct comstack cs; + IOCHAN chan = iochan_create(-1, ir_session, EVENT_INPUT, 2); + association *assoc; + int i; + + memset(servers, 0, sizeof(servers)); + memset(&cs, 0, sizeof(cs)); + cs.protocol = PROTO_HTTP; + assoc = create_association(chan, &cs, 0); + YAZ_CHECK_EQ(cs.max_recv_bytes, control_block.maxrecordsize); + destroy_association(assoc); + + for (i = 0; i < 2; i++) + servers[i].cb = control_block; + servers[0].cb.maxrecordsize = 1000; + servers[1].cb.maxrecordsize = 5000; + servers[1].listen_ref = listen_b; + servers[0].next = &servers[1]; + gfs_server_list = servers; + strcpy(control_block.xml_config, "in-memory"); + YAZ_CHECK_EQ(statserv_max_recv_bytes(1), 1000); + YAZ_CHECK_EQ(statserv_max_recv_bytes(2), 5000); + assoc = create_association(chan, &cs, 0); + YAZ_CHECK_EQ(cs.max_recv_bytes, 5000); + YAZ_CHECK(control_association(assoc, 0, 0)); + YAZ_CHECK_EQ(cs.max_recv_bytes, 1000); + destroy_association(assoc); + xfree(chan); + gfs_server_list = 0; + control_block.xml_config[0] = 0; + statserv_setcontrol(&control_block); +} #endif int main(int argc, char **argv) @@ -511,6 +548,7 @@ int main(int argc, char **argv) tst_auth(""); tst_auth("&x-username=alice&x-password=wrong"); tst_docpath(); + tst_recv_limit(); xml_config_close(); #endif YAZ_CHECK_TERM; From d4949fc00a99cbeb56042726f441aaec0513746e Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:48:57 +0100 Subject: [PATCH 7/9] Prevent embedded terminators in ISO2709 output Replace field and record terminators in directory tags, indicators, and converted field data with spaces. This prevents input data or character conversion from injecting extra record boundaries while preserving directory lengths and offsets. Add regression tests for MARC-8 conversion and JSON input. Discovered by Anthropic's OSS Scanner as ANT-2026-6KDCAXNJ. --- src/marcdisp.c | 16 ++++++++++++ test/test_marc_read_iso2709.c | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/marcdisp.c b/src/marcdisp.c index b6bbb00a..a8f56ccc 100644 --- a/src/marcdisp.c +++ b/src/marcdisp.c @@ -1170,6 +1170,18 @@ static size_t pow10i(int n) return res; } +/* Blank field and record terminators in wr from offset pos on. Content + read from ISO2709 has none, but character set conversion may produce + them (MARC-8 0x9D/0x9E with ASCII as G1, danMARC @001D) and so may + other readers (JSON). Same length, so the directory is unaffected. */ +static void iso2709_blank_terminators(WRBUF wr, size_t pos) +{ + char *buf = wrbuf_buf(wr); + for (; pos < wrbuf_len(wr); pos++) + if (buf[pos] == ISO2709_FS || buf[pos] == ISO2709_RS) + buf[pos] = ' '; +} + int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr) { struct yaz_marc_node *n, *cap_node = 0; @@ -1254,6 +1266,7 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr) return -1; } wrbuf_printf(wr_dir, "%3.3s", tag); + iso2709_blank_terminators(wr_dir, wrbuf_len(wr_dir) - 3); wrbuf_printf(wr_dir, "%0*ld", length_data_entry, (long) data_length); wrbuf_printf(wr_dir, "%0*ld", length_starting, (long) data_offset); data_offset += data_length; @@ -1291,6 +1304,7 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr) for (n = mt->nodes; n != cap_node; n = n->next) { struct yaz_marc_subfield *s; + size_t pos = wrbuf_len(wr); switch(n->which) { @@ -1302,11 +1316,13 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr) wrbuf_iconv_puts(wr, mt->iconv_cd, s->code_data); marc_iconv_reset(mt, wr); } + iso2709_blank_terminators(wr, pos); wrbuf_putc(wr, ISO2709_FS); break; case YAZ_MARC_CONTROLFIELD: wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data); marc_iconv_reset(mt, wr); + iso2709_blank_terminators(wr, pos); wrbuf_putc(wr, ISO2709_FS); break; case YAZ_MARC_COMMENT: diff --git a/test/test_marc_read_iso2709.c b/test/test_marc_read_iso2709.c index d4067c3b..7c5a9c3d 100644 --- a/test/test_marc_read_iso2709.c +++ b/test/test_marc_read_iso2709.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -270,6 +271,53 @@ static void tst_xml_comment(void) yaz_marc_destroy(mt); } +/* The ISO2709 writer writes no field or record terminators but its own. */ +static void tst_write_terminators(void) +{ + yaz_marc_t mt = yaz_marc_create(); + yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8"); + /* MARC-8 0x9D and 0x9E are 0x1D and 0x1E when ASCII is the G1 set */ + const char marc8[] = " \037ak\033)B\235\236b\235\036"; + const char expect1[] = "00048nam a2200037 4500" "245001000000\036" + " \037ak b \036\035"; + const char *json = "{\"leader\":\"00000nam a2200000 a 4500\",\"fields\":" + "[{\"2\\u001e\\u001d\":{\"ind1\":\"\\u001d\",\"ind2\":\"0\"," + "\"subfields\":[{\"a\":\"x\\u001dy\"}]}}]}"; + const char expect2[] = "00046nam a2200037 a 4500" "2 000800000\036" + " 0\037ax y\036\035"; + struct json_node *n = json_parse(json, 0); + WRBUF w = wrbuf_alloc(); + const char *result; + size_t result_len; + char *buf; + int len; + + yaz_marc_xml(mt, YAZ_MARC_ISO2709); + + YAZ_CHECK(cd != 0); + yaz_marc_iconv(mt, cd); + buf = record("245", marc8, sizeof(marc8) - 1, &len); + YAZ_CHECK_EQ(yaz_marc_decode_buf(mt, buf, len, &result, &result_len), len); + YAZ_CHECK_EQ(result_len, sizeof(expect1) - 1); + YAZ_CHECK(result_len == sizeof(expect1) - 1 && + !memcmp(result, expect1, result_len)); + xfree(buf); + yaz_marc_iconv(mt, 0); + if (cd) + yaz_iconv_close(cd); + + YAZ_CHECK(n != 0); + yaz_marc_reset(mt); + YAZ_CHECK_EQ(yaz_marc_read_json_node(mt, n), 0); + YAZ_CHECK_EQ(yaz_marc_write_mode(mt, w), 0); + YAZ_CHECK_EQ(wrbuf_len(w), sizeof(expect2) - 1); + YAZ_CHECK(wrbuf_len(w) == sizeof(expect2) - 1 && + !memcmp(wrbuf_buf(w), expect2, wrbuf_len(w))); + json_remove_node(n); + wrbuf_destroy(w); + yaz_marc_destroy(mt); +} + int main(int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); @@ -278,5 +326,6 @@ int main(int argc, char **argv) tst_write_bounds(); tst_overlap(); tst_xml_comment(); + tst_write_terminators(); YAZ_CHECK_TERM; } From dcd844575ba372075672ee4e17af058bf216c37f Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:52:41 +0100 Subject: [PATCH 8/9] Restore complete MARC-8 encoder state on E2BIG rollback Save and restore the entire encoder state when rolling back a decomposed character, including character-set selections and pending ligature bytes. This prevents retries from dropping bytes and causing ISO2709 record lengths and directory offsets to disagree with the data. Add regression tests for page escapes and ligature state across retries. Discovered by Anthropic's OSS Scanner as ANT-2026-QIF619NX. --- src/iconv_encode_marc8.c | 13 +++++-------- test/test_iconv.c | 13 +++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/iconv_encode_marc8.c b/src/iconv_encode_marc8.c index fe3c00ae..b241dbfa 100644 --- a/src/iconv_encode_marc8.c +++ b/src/iconv_encode_marc8.c @@ -355,12 +355,10 @@ static size_t yaz_write_marc8_generic(yaz_iconv_t cd, struct encoder_data *w, unsigned long x1, x2; if (yaz_iso_8859_1_lookup_y(x, &x1, &x2)) { - /* save the output pointers .. */ + /* save the output pointers and encoder state .. */ char *outbuf0 = *outbuf; size_t outbytesleft0 = *outbytesleft; - unsigned long last_ch = w->write_marc8_last; - int ncr = w->write_marc8_ncr; - const char *lpage = w->write_marc8_lpage; + struct encoder_data w0 = *w; size_t r = yaz_write_marc8_2(cd, w, x1, outbuf, outbytesleft, loss_mode); @@ -370,12 +368,11 @@ static size_t yaz_write_marc8_generic(yaz_iconv_t cd, struct encoder_data *w, outbuf, outbytesleft, loss_mode); if (r && yaz_iconv_error(cd) == YAZ_ICONV_E2BIG) { - /* not enough room. reset output to original values */ + /* not enough room. reset output and encoder state to original + values (G0/G1 too, as their escapes are taken back) */ *outbuf = outbuf0; *outbytesleft = outbytesleft0; - w->write_marc8_last = last_ch; - w->write_marc8_ncr = ncr; - w->write_marc8_lpage = lpage; + *w = w0; } return r; } diff --git a/test/test_iconv.c b/test/test_iconv.c index f91d0a26..29e3ac66 100644 --- a/test/test_iconv.c +++ b/test/test_iconv.c @@ -617,6 +617,19 @@ static void tst_utf8_to_marc8(const char *marc8_type) "\x1B\x28\x42\x2C" )); + /** CJK char pending, then A GRAVE (NFC) that must be retried because + only its first half fits in outbuf: ESC $1 must not be lost */ + YAZ_CHECK(tst_convert(cd, + "TTT" "\xE4\xB8\x80" "\xC3\x80", /* UTF-8 */ + "TTT" "\033$1" "\x21\x30\x21" "\033(B" + "\xE1" "A")); + + /** Same for the second half of a ligature */ + YAZ_CHECK(tst_convert(cd, + "TT" "\xE4\xB8\x80" "\xCD\xA1" "\xC3\x80", /* UTF-8 */ + "TT" "\xEB" "\033$1" "\x21\x30\x21" "\xEC" "\033(B" + "\xE1" "A")); + { char *inbuf0 = "\xe2\x81\xb0"; char *inbuf = inbuf0; From 88c51cf40903c8063d60f3283d34cd06c6cda7d2 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 27 Nov 2026 20:54:31 +0100 Subject: [PATCH 9/9] Clear MARCXML SAX controlfield tags after use Reset the saved tag after adding a controlfield so enclosing elements cannot repeatedly copy the innermost tag. This prevents quadratic memory growth from nested controlfields with long tags. Add a regression test for nested controlfields and subsequent fields. Discovered by Anthropic's OSS Scanner as ANT-2026-UVY80AMT. --- src/marc_read_sax.c | 3 +++ test/test_marc_read_sax.c | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/marc_read_sax.c b/src/marc_read_sax.c index ba4504a1..55803faa 100644 --- a/src/marc_read_sax.c +++ b/src/marc_read_sax.c @@ -149,6 +149,9 @@ static void yaz_end_element_ns(void *vp, const xmlChar *localname, { yaz_marc_add_controlfield(ctx->mt, wrbuf_cstr(ctx->tag), wrbuf_buf(ctx->cdata), wrbuf_len(ctx->cdata)); + /* tag belongs to this element only; an enclosing controlfield + must not add it again */ + wrbuf_rewind(ctx->tag); } else if (strcmp((const char *)localname, "subfield") == 0) { diff --git a/test/test_marc_read_sax.c b/test/test_marc_read_sax.c index 9bdc8e63..37bfd3d0 100644 --- a/test/test_marc_read_sax.c +++ b/test/test_marc_read_sax.c @@ -148,6 +148,46 @@ static void tst_leader_lengths(void) ""); wrbuf_destroy(xml); } + +static void tst_nested_controlfield(void) +{ + /* The tag of a controlfield is used for that element only. Enclosing + * controlfield elements must not add further copies of it. */ + const char *xml = "00062cgm a2200037Ia 4500" + "" + "x" + "y"; + const char *tag = ""; + const char *cp; + struct leader_test_data data; + yaz_marc_t mt = yaz_marc_create(); + yaz_marc_sax_t sax; + xmlParserCtxtPtr parser; + data.records = 0; + data.output = wrbuf_alloc(); + sax = yaz_marc_sax_new(mt, leader_handler, &data); + parser = xmlCreatePushParserCtxt(yaz_marc_sax_get_handler(sax), sax, + xml, (int) strlen(xml), 0); + YAZ_CHECK(parser != 0); + if (parser) + { + YAZ_CHECK_EQ(xmlParseChunk(parser, 0, 0, 1), 0); + xmlFreeParserCtxt(parser); + } + YAZ_CHECK_EQ(data.records, 1); + cp = strstr(wrbuf_cstr(data.output), tag); + YAZ_CHECK(cp != 0); + if (cp) + { + YAZ_CHECK(strncmp(cp + strlen(tag), "x", 16) == 0); + YAZ_CHECK(strstr(cp + 1, tag) == 0); + } + YAZ_CHECK(strstr(wrbuf_cstr(data.output), + "y") != 0); + yaz_marc_sax_destroy(sax); + yaz_marc_destroy(mt); + wrbuf_destroy(data.output); +} #endif @@ -157,6 +197,7 @@ int main(int argc, char **argv) tst1(); #if YAZ_HAVE_XML2 tst_leader_lengths(); + tst_nested_controlfield(); #endif YAZ_CHECK_TERM; }