PowerShell / Win32-OpenSSH

Win32 port of OpenSSH
7.33k stars 754 forks source link

Wrong locale settings used for error hints #2120

Open zhang-stephen opened 1 year ago

zhang-stephen commented 1 year ago

Prerequisites

Steps to reproduce

  1. use non-English as the default display language, e.g. Simplified Chinese,
  2. enable experimental UTF-8 support, or use command chcp 65001 to enable it for tests
  3. run ssh with a wrong remote address, e.g. ssh pi@raspberrypi4.locax
  4. then the error hints will be printed as UTF-8 hexademical values, instead of readable text in Chinese or other languags.

P.S. Expected behaviour is demostrated by SSH from msys2.

Expected behavior

PS C:\Users\stephen> chcp
Active code page: 65001
PS C:\Users\stephen> C:\Users\stephen\workspace0\dev\msys2\usr\bin\ssh.exe pi@raspberrypi4.locax
ssh: Could not resolve hostname raspberrypi4.locax: Name or service not known

Actual behavior

PS C:\Users\stephen> chcp
Active code page: 65001
PS C:\Users\stephen> ssh pi@raspberrypi4.locax
ssh: Could not resolve hostname raspberrypi4.locax: \344\270\215\347\237\245\351\201\223\350\277\231\346\240\267\347\232\204\344\270\273\346\234\272\343\200\202

Error details

No Errors occurred.

Environment data

PS C:\Users\stephen> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.25393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

OpenSSH_for_Windows_9.2p1, LibreSSL 3.6.2

Visuals

No response

mgkuhn commented 1 year ago

What exact command to I need to enter to reproduce step 1?

zhang-stephen commented 1 year ago

I think the display language should be set in settings app. This link would help you to change the display language of Windows 10/11.

mgkuhn commented 1 year ago

Function do_log() calls strnvis() to prepare that string for display, and looking at openbsd-compat/vis.c, I can see there the code that makes these backslash-octal substitutions in function vis(). Right before it there is an #ifdef WINDOWS and if (flag & VIS_LOG_UTF16) to make that function a bit more 8-bit transparent.

I can also see in log.c

#ifdef WINDOWS
#define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL|VIS_LOG_UTF16)
#else
#define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
#endif

#define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)

which suggests that this only applies to syslog, but not to stderr.

I haven't found yet where any of this substitution behaviour might depend on the locale setting.

zhang-stephen commented 1 year ago

I just found this issue could be reproduced in active code page 936, the GBK encoding for simplified Chinese. It is possible this issue is irrelative to encoding.

Then I have no more idea about the root cause. 😕

P.S. It could be reproduced in active code page 437, the US code page, too...

mgkuhn commented 1 year ago

I was trying to get OpenSSH on Ubuntu Linux 20.04 to say Name or service not known in any language other than English, to see if the same problem exists there, but I'm now not sure that is even possible.

Perhaps OpenSSH on Unix-like environments doesn't even support translated error messages, which presumably would require use of stderror_l instead of stderror (and I can't see the former used).

Perhaps only the Windows version will even try to output non-ASCII error messages?

zhang-stephen commented 1 year ago

I got this non-ascii output on Windows Powerhell only, the cmd.exe is not tested.