idn: restore MultiByteToWideChar() MB_ERR_INVALID_CHARS flag - #22326
idn: restore MultiByteToWideChar() MB_ERR_INVALID_CHARS flag#22326vszakats wants to merge 3 commits into
MultiByteToWideChar() MB_ERR_INVALID_CHARS flag#22326Conversation
There was a problem hiding this comment.
Pull request overview
This PR restores use of the Windows MultiByteToWideChar() MB_ERR_INVALID_CHARS flag to ensure invalid UTF-8 input is rejected during IDN (punycode) conversions, and aligns related UTF-8→wchar conversion helpers for consistent validation behavior across Windows codepaths.
Changes:
- Use
MB_ERR_INVALID_CHARSin Win32 IDN UTF-8→UTF-16 conversions so invalid UTF-8 fails fast (lib/idn.c). - Pass
MB_ERR_INVALID_CHARSconsistently to the actual conversion calls (not only the sizing call) in curlx UTF-8→wchar helpers (lib/curlx/multibyte.c,lib/curlx/fopen.c). - Minor formatting adjustment to a
MultiByteToWideChar()call in Schannel client cert import (lib/vtls/schannel.c).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/vtls/schannel.c | Formatting-only tweak around MultiByteToWideChar() usage when converting the PKCS#12 password. |
| lib/idn.c | Restores MB_ERR_INVALID_CHARS for Win32 IDN conversions to reject invalid UTF-8 input. |
| lib/curlx/multibyte.c | Ensures the conversion call itself uses MB_ERR_INVALID_CHARS (matching the sizing call). |
| lib/curlx/fopen.c | Same consistency hardening for UTF-8→wchar conversion used by Windows fopen helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I don't have any objections to this but thinking it through if fopen is called with some output filename that is not UTF-8 (like maybe an old local encoded content disposition from the server?), and so it can't convert it, then this change would make it error rather than using placeholder characters. Do you think this scenario is plausible, could it create friction for users who really don't know about encoding etc but just want the file saved regardless of encoding mismatch? |
|
As for IDN, it restores what has been there before my patch a few versions ago I think it's probably better to reject bad input early, as this perhaps avoids letting There is one more place (3 calls) in |
Ok
Those calls are to convert what we assume is UTF-8 encoded Unicode content from the server to the user's terminal. I disagree and think we should continue to print placeholder characters when content can't be converted, rather than error. Some servers aren't going to meet the assumption. Personally I'd rather see the content and I think from the user perspective they probably would too rather than see an error. |
I agree, yes, it's better not to throw away the whole chunk just because Something to perhaps consider is to let is passthrough as-is if the OS is |
Also:
and robustness. (It's not stricly necessary because the initial call
to determine size, with this flag passed, fails already on bad input.)
MultiByteToWideChar()line (formatting).Ref: https://learn.microsoft.com/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
Follow-up to 6694a42 #19798