Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters in Content-Type header aren't always ignored #109

Open
hyjial opened this issue Feb 29, 2024 · 0 comments
Open

Parameters in Content-Type header aren't always ignored #109

hyjial opened this issue Feb 29, 2024 · 0 comments

Comments

@hyjial
Copy link

hyjial commented Feb 29, 2024

Trigger

The HTML document below makes Firefox ESR 115.7.0 from OpenBSD ports send a POST request with Content-Type = application/x-www-form-urlencoded;charset=UTF-8.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="application/javascript">
function onLoad(e) {
        const curr_url = new URL(document.URL);
        if (!curr_url.protocol.startsWith('http'))
                return;
        const url = new URL('/events', curr_url.origin);
        const body = new URLSearchParams([
                ['name', 'page_viewed'],
                ['value', 'index'],
        ]);
        const options = {
                body: body,
                method: 'POST',
        };
        var p = fetch(url, options);
        p.then(() => {}, () => {});
}

window.addEventListener('load', onLoad);
</script>
</body>
</html>

Problem

Kcgi doesn't parse the request body correctly because here, it compares the full value of the Content-Type header to application/x-www-form-urlencoded and doesn't ignore the possible parameters in the header value, like it does in the multipart/form-data case. As a result the request body is presented to the application as a single pair with the empty string as key.

Fix

The patch below solves the issue and makes the body appear as two pairs with key name and value.

--- kcgi-VERSION_0_13_3/child.c	Sat Dec  2 20:50:03 2023
+++ kcgi/child.c	Thu Feb  8 11:30:13 2024
@@ -1466,7 +1466,7 @@
 	}
 
 	if (cp != NULL) {
-		if (strcasecmp(cp, "application/x-www-form-urlencoded") == 0)
+		if (strncasecmp(cp, "application/x-www-form-urlencoded", 33) == 0)
 			parse_pairs_urlenc(pp, b);
 		else if (strncasecmp(cp, "multipart/form-data", 19) == 0) 
 			parse_multi(pp, cp + 19, b, bsz);

Probably worth applying the same fix to the text/plain case.

N.B.: I tested both problem and fix with kcgi 0.13.3 from OpenBSD ports, but this part of the code hasn't changed since the release.

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

No branches or pull requests

1 participant