Skip to content

Keep the query string on index redirects - #729

Merged
AlbertShown merged 1 commit into
webui-dev:mainfrom
ViktorJannicke:fix/redirect-keep-query
Nov 26, 2026
Merged

AlbertShown merged 1 commit into
webui-dev:mainfrom
ViktorJannicke:fix/redirect-keep-query

Conversation

@ViktorJannicke

Copy link
Copy Markdown
Contributor

Problem

When WebUI answers / or a folder URL with a 302 to the index file, it drops the request's query string: /?a=1&b=2 is redirected to index.html, not index.html?a=1&b=2. A page that reads its state from location.search (a deep link, a callback URL, ?lang=…) loses it on the first load.

All four index redirects in src/webui.c pass only the index path to mg_send_http_redirect():

  • _webui_http_handler(), root-folder mode (webui_set_root_folder()):
    • the / index (GET /?a=1&b=2 → index.html);
    • a subfolder index (GET /sub/?a=1&b=2 → /sub/index.html).
  • _webui_external_file_handler(), file-handler mode (webui_set_file_handler()):
    • the start-file rewrite (webui_start_server(win, "index.html"), GET /?a=1&b=2 → /index.html);
    • the folder-index probe (GET /sub/?a=1&b=2 → /sub/index.html).

Fix

A new static helper, _webui_http_redirect_keep_query(), now sends these four redirects and appends the request's query to the Location.

  • CivetWeb hands over query_string still percent-encoded, since WebUI doesn't enable decode_query_string. So the query is passed through as received, %xx included.
  • Bytes outside visible ASCII, and #, are percent-encoded. The Location then stays one header line, and # can't start a fragment. CivetWeb already answers a raw space or 0x7F in the request line with 400.
  • A query longer than 8 KiB gets 414 URI Too Long instead of being copied into the header. CivetWeb's own request limit (16 KiB by default) still applies first.
  • A redirect without a query is unchanged.

No API or ABI change. Only src/webui.c changes; include/, bridge/ and src/civetweb/ are untouched. The helper is static. nm -D --defined-only of libwebui-2.so lists the same 196 symbols before and after this change, for gcc and clang, release and debug builds. Language bindings need no change.

Testing

Built on Linux x64 with gcc 16.2 and clang 21:

  • make and make debug, plus the CMake build with examples;
  • no new warnings, also with -Wall -Wextra;
  • all C examples build, including test_index_redirect.

I checked each of the four paths with a small harness (not part of this PR). It starts the server without a browser, and I sent requests with curl and over a raw socket:

Request Before After
…/?a=1&b=2 302 to …index.html 302 to …index.html?a=1&b=2
no query 302 to …index.html same
raw bytes \x80\xC3\xA9\xFF, raw # query dropped %80%C3%A9%FF, %23
%20%23%41 query dropped passed through unchanged
8192-byte query query dropped kept
8193 to 15000 bytes query dropped 414
curl -L …/?a=1&b=2 200 for /index.html 200 for /index.html?a=1&b=2

I also ran the library and the harness under AddressSanitizer with LeakSanitizer, and they reported nothing. I couldn't test on Windows or macOS; the change has no platform-specific code.

🤖 Generated with Claude Code

When WebUI answers `/` or a folder URL with a 302 to the index file, it
passed only the index path to `mg_send_http_redirect()` and dropped the
request's query string. `/?a=1&b=2` landed on `index.html` without
`?a=1&b=2`, so deep links and callback URLs that carry their state in the
query lost it. This affected both the local root-folder mode
(`_webui_http_handler()`: the `/` index and the subfolder index) and the
custom file handler mode (`_webui_external_file_handler()`: the start-file
rewrite and the folder-index probe).

A new internal helper, `_webui_http_redirect_keep_query()`, now sends these
four redirects and appends the original query to the Location. The query
is still percent-encoded as received, so it is passed through unchanged;
only bytes outside visible ASCII and `#` are percent-encoded, which keeps
the Location a single header line and stops `#` from turning into a
fragment. Queries longer than 8 KiB are answered with 414 instead of
being echoed back. Redirects without a query are unchanged.

The public API and ABI are unchanged; the helper is static and no header
or exported symbol changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlbertShown
AlbertShown merged commit 8a55352 into webui-dev:main Nov 26, 2026
26 checks passed
@AlbertShown

Copy link
Copy Markdown
Member

Thank you @ViktorJannicke

ViktorJannicke added a commit to Runic-Artifex/cs-webui that referenced this pull request Nov 26, 2026
#37)

* Adopt the official WebUI nightly 8a55352c2

Pin the official nightly release built from webui-dev/webui
8a55352c2d7b6c0be9a149af417373714a390cb8 (release body "Generated from
commit 8a55352c...", assets built 2026-11-26). The manifest digests match
GitHub's release assets API and the verified bootstrap downloads;
flake.nix and flake.lock pin the same revision.

include/webui.h is byte-identical (112 exports), and the dynamic exports
of the official linux-x64 .so are identical to 24c7d196e, so
CsWebUi.Native and the public API need no change.

Upstream changes since 24c7d196e:

- a89c4ebf7 (webui-dev/webui#729): the redirects from / and folder URLs
  to their index file keep the request's query string, with a file
  handler and with a root folder. Bytes outside visible ASCII and '#' in
  the query are percent-encoded; a query over 8 KiB gets 414.

WebUiIndexRedirectTests covers / and a folder URL with SetFileHandler and
with SetRootFolder; both tests fail against 24c7d196e.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Release CS-WebUI 2.5.0-beta.4.11

Set the version to 2.5.0-beta.4.11 and FileVersion to 2.5.0.11, and add
the release notes for the query string on index redirects (#33,
webui-dev/webui#729).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@ViktorJannicke
ViktorJannicke deleted the fix/redirect-keep-query branch November 27, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants