Skip to content

a sad tale of unicode account names, shells and code pages - #2624

Merged
joshaber merged 16 commits into
masterfrom
its-just-a-series-of-bytes-they-said
Sep 8, 2017
Merged

joshaber merged 16 commits into
masterfrom
its-just-a-series-of-bytes-they-said

Conversation

@shiftkey

@shiftkey shiftkey commented Sep 6, 2017 •

Copy link
Copy Markdown
Member

Notice anything wrong with this picture?

I'll give you a hint:

I'm not quite familiar with how Git Bash is differentiating between these two, but it's unimportant to the problem at hand, which is #2585. It took a while to get to the bottom of why the Atom installation wasn't being detected for @DRSAgile, but it's related to having non-ASCII characters in their account name.

Here's my test account running this example script to read the registry:

C:\Users\hösentägen\Desktop\testing>node index.js
got: [
  {
    "name": "DisplayIcon",
    "type": "REG_SZ",
    "value": ""C:\\Users\\h�sent�gen\\AppData\\Local\\atom\\app.ico"
  },
  {
    "name": "DisplayName",
    "type": "REG_SZ",
    "value": "Atom"
  },
  {
    "name": "DisplayVersion",
    "type": "REG_SZ",
    "value": "1.19.5"
  },
  {
    "name": "InstallDate",
    "type": "REG_SZ",
    "value": "20170906"
  },
  {
    "name": "InstallLocation",
    "type": "REG_SZ",
    "value": ""C:\\Users\\h�sent�gen\\AppData\\Local\\atom"
  },
  {
    "name": "Publisher",
    "type": "REG_SZ",
    "value": "GitHub Inc."
  },
  {
    "name": "QuietUninstallString",
    "type": "REG_SZ",
    "value": "\""C:\\Users\\h�sent�gen\\AppData\\Local\\atom\\Update.exe\" --uninstall -s"
  },
  {
    "name": "UninstallString",
    "type": "REG_SZ",
    "value": "\""C:\\Users\\h�sent�gen\\AppData\\Local\\atom\\Update.exe\" --uninstall"
  },
  {
    "name": "EstimatedSize",
    "type": "REG_DWORD",
    "value": "0x2bc0e"
  },
  {
    "name": "NoModify",
    "type": "REG_DWORD",
    "value": "0x1"
  },
  {
    "name": "NoRepair",
    "type": "REG_DWORD",
    "value": "0x1"
  },
  {
    "name": "Language",
    "type": "REG_DWORD",
    "value": "0x409"
  }
]

It's those � characters that are the root cause of the issue.

If you're particularly sharp you might have noticed that directory path in the prompt is rendered correctly. Your eyes aren't deceiving you.

But it's 2017! Doesn't Windows support Unicode?

Yes, Windows supports Unicode character sets and code pages. But specific parts of the Windows operating system, for compatibility reasons, don't have this enabled by default - like the Windows Console. It turns out we already encountered this problem in a different context when we were retrieving the PATH environment variable for the current user using a PowerShell script (shout out to the Atom team for finding this earlier):

  const args = [
    '-noprofile',
    '-ExecutionPolicy',
    'RemoteSigned',
    '-command',
    // Set encoding and execute the command, capture the output, and return it
    // via .NET's console in order to have consistent UTF-8 encoding.
    // See http://stackoverflow.com/questions/22349139/utf-8-output-from-powershell
    // to address https://github.com/atom/atom/issues/5063
    `
      [Console]::OutputEncoding=[System.Text.Encoding]::UTF8
      $output=[environment]::GetEnvironmentVariable('Path', 'User')
      [Console]::WriteLine($output)
    `,
  ]

  const stdout = await spawn(powershellPath, args)
  ...

Why do we need to do these shenanigans?

Compatibility, mostly. If you're running the Command Prompt in Windows 10 chances are you have the same code page set as the IBM PC supported. That's right, Code Page 437, baby!

Before going any further, I should give some back story. Code pages are conceptually similar to character sets, but because of History™ there's lots of platform-specific code pages still floating around. The documented list of these is pretty entertaining read, if only to understand how messy this is.

Most code pages are a superset of ASCII, so any time you see that � or garbled text you're likely dealing with that problem. This ignores discussions around fonts, but I'm not going to deal with that here.

Anyway, Windows has an active code page set which affects how characters are rendered in a terminal. This also affects the behaviour of Win32 APIs - for example, ShellExecuteA is the ANSI API for ShellExecute and it's behaviour is affected by the active code page. ShellExecuteW is the Unicode version and is unaffected by the active code page.

Oh, and the active code page is set globally, so you need to be really, really careful 😱

Did you know that everything is terrible?

What can we do here?

The excellent people over at node-winreg pointed out that you probably need to care about the active code page for any UTF-8 data it reads, and suggested this workaround:

An even better approach would be to extract and store the value returned by a call to chcp prior setting the console to UTF-8 and resetting the codepage after your script is done.

That's the approach I've gone with here, and I'll dig more into this now that I know where I'm heading.

But doesn't Node support Unicode? Why not just use that?

By the time Node gets the chance to read these bytes into the stream, they're already invalid. We need to tell the shell which code page to use before spawning reg.exe so we get the right characters back.

  • add getActiveCodePage method to retrieve the current code page
  • add some registry tests as a safety harness
  • move the registry lookup code into an external script that manages the code page

@shiftkey shiftkey changed the title a sad tale of unicode account names and shells and code pages a sad tale of unicode account names, shells and code pages Sep 6, 2017
@shiftkey
shiftkey force-pushed the its-just-a-series-of-bytes-they-said branch from 7cf6da8 to 33c2f21 Compare September 6, 2017 08:00
@joshaber

joshaber commented Sep 6, 2017

Copy link
Copy Markdown
Contributor

giphy-2

@shiftkey shiftkey added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Sep 7, 2017

@joshaber joshaber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📟

Comment thread app/src/lib/registry.ts Outdated
}

const activeCodePage = await getActiveCodePage()
if (activeCodePage) {

This comment was marked as spam.

This comment was marked as spam.

Comment thread app/test/unit/shell-test.ts Outdated
})
}

if (process.platform !== 'win32') {

This comment was marked as spam.

Comment thread app/src/lib/shell.ts Outdated
const result = chcpOutputRegex.exec(output)
if (result) {
const value = result[1]
resolve(parseInt(value, 10))

This comment was marked as spam.

@joshaber joshaber self-assigned this Sep 7, 2017
@shiftkey

shiftkey commented Sep 8, 2017

Copy link
Copy Markdown
Member Author

🤾‍♂️

@joshaber

joshaber commented Sep 8, 2017

Copy link
Copy Markdown
Contributor

giphy-2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants