Skip to content

port registry usage to use PowerShell API - #3209

Closed
shiftkey wants to merge 13 commits into
masterfrom
i-hate-you-registry-with-the-fire-of-a-thousand-suns
Closed

shiftkey wants to merge 13 commits into
masterfrom
i-hate-you-registry-with-the-fire-of-a-thousand-suns

Conversation

@shiftkey

@shiftkey shiftkey commented Nov 1, 2017 •

Copy link
Copy Markdown
Member

Fixes #3105
Fixes #3123

Strap yourselves in folks, it's time for a story.

Group Policy is a thing which lets administrators lock down their Windows machines. And here's the group policy for stopping apps from writing to the registry:

In theory, reg.exe shouldn't need to write the registry when you do REG QUERY [key] but here we are:

And we can't fix reg.exe so I'm all:

And I start looking at alternatives. I really want to not use PowerShell here but it turns out we're already doing something very similar in squirrel-updater.ts:

  • we find powershell.exe - so we assume it's already installed

const systemRoot = process.env['SystemRoot']
if (systemRoot) {
const system32Path = Path.join(process.env.SystemRoot, 'System32')
powershellPath = Path.join(
system32Path,
'WindowsPowerShell',
'v1.0',
'powershell.exe'
)
} else {
powershellPath = 'powershell.exe'
}

  • we then spawn a process, handling encodings

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)
`,
]

  • we parse the output

const stdout = await spawn(powershellPath, args)
const pathOutput = stdout.replace(/^\s+|\s+$/g, '')
return pathOutput.split(/;+/).filter(segment => segment.length)

Fiiine, let's do this. This means I avoid the worries about active code pages from before.

  • tests all pass
  • unify powershell-esque code into a reusable module
  • cleanup jank from previous reg.exe approach
  • Out-String the result to ensure it's formatted nicely for parsing

This circumvents the group policy which restricts
apps writing to the registry, which is what reg.exe
does despite it's best intentions. We can't fix that
here, so instead we're going to use the PowerShell API
for interacting with the registry, which has it's own
quirks. Because everything is terrible.
@shiftkey
shiftkey force-pushed the i-hate-you-registry-with-the-fire-of-a-thousand-suns branch from 7f12cfe to 5f39cff Compare November 1, 2017 01:49
@shiftkey shiftkey added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Nov 1, 2017
@shiftkey

shiftkey commented Nov 1, 2017

Copy link
Copy Markdown
Member Author

@damieng just a heads up - winreg also depends on reg.exe, not sure if you've had any reports like this but it's used here in Atom.

@shiftkey

shiftkey commented Nov 1, 2017

Copy link
Copy Markdown
Member Author

The only reservation I have about this PR is the performance impact - it goes from 50-70ms to 300-600ms, which becomes significant on the preferences view when we do this a bunch concurrently.

@niik

niik commented Nov 1, 2017

Copy link
Copy Markdown
Member

The only reservation I have about this PR is the performance impact - it goes from 50-70ms to 300-600ms, which becomes significant on the preferences view when we do this a bunch concurrently.

Could we use something which accesses the native APIs directly? Like https://www.npmjs.com/package/windows-registry ?

@damieng

damieng commented Nov 1, 2017

Copy link
Copy Markdown

Yeah we've had a couple of reports of problems with not being allowed access to reg.exe but we use it to setup our shell integration on launch from the installer/updater as well as in the settings view where you can toggle the various integration options.

That windows registry npm package does look useful though. My experience with doing crazy shell tricks to work around issues tells me whatever you come up with will be flakey between OS versions. (We do a powershell trick for --wait and it's been a crazy ride)

@shiftkey

shiftkey commented Nov 1, 2017 •

Copy link
Copy Markdown
Member Author

Could we use something which accesses the native APIs directly? Like https://www.npmjs.com/package/windows-registry ?

@niik this is a good shout. I'm not sure why I avoided it when I was initially looking at our registry support - maybe because the API was rather raw? I'll give it a shot and see if it's faster than the PS approach.

EDIT: I think it was the lack of type declarations that meant I skipped over it at the time

EDIT 2: arggggh native module isn't getting pulled in by webpack:

EDIT 3: testing it outside of Desktop suggests that it doesn't support enumerating the range of values - we can workaround this, but then it's more code. I also had a minor PR that's still not been merged, so getting additional work incorporated seems unlikely in the short term: CatalystCode/windows-registry-node#52

@shiftkey shiftkey removed the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Nov 2, 2017
@begoldsm

begoldsm commented Nov 3, 2017

Copy link
Copy Markdown

@shiftkey huge ups for even doing this heavy lifting investigative work. Thank you thank you thank you!

@begoldsm begoldsm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Out of curiosity, is it possible to do the registry read check one time (during initial install) and then save the values that are important to the app in AppData? That way you can require elevation once (during initial setup) and then retrieve the info from your local app data for every subsequent launch. That would get around the performance issue here and wouldn't require a reliance on PowerShell.

@j-f1

j-f1 commented Nov 7, 2017

Copy link
Copy Markdown
Contributor

@begoldsm

Is it possible to do the registry read check one time?

This sounds like a good idea, but how would it handle installing or uninstalling supported editors?

@begoldsm

begoldsm commented Nov 7, 2017

Copy link
Copy Markdown

@j-f1 that is a good question. There could, perhaps, be an explicit option to "add/remove" an editor which could require elevation when it does it's check? I am not familiar with all the surface area where GitDesktop reads from the registry.
Ideally it would be possible to just read from the registry using standard tools, but from my quick search PowerShell is the only one I have found that is built in.

@shiftkey

shiftkey commented Nov 7, 2017

Copy link
Copy Markdown
Member Author

Out of curiosity, is it possible to do the registry read check one time (during initial install) and then save the values that are important to the app in AppData?

This doesn't cover the ability for users to add and remove items, as @j-f1 mentioned. The app refreshes it's list of editors and shells on each launch but will defer it until you do something with the app (like open the Options view). That won't cover everyone (for example, if you an install an editor after checking your options and noticing it's not there), but keeps the code simple enough.

There could, perhaps, be an explicit option to "add/remove" an editor which could require elevation when it does it's check?

I'd rather not make this something that requires manual work by the users, let alone all the work to manage elevation correctly.

I am not familiar with all the surface area where GitDesktop reads from the registry.

Here's the current list:

@begoldsm

Copy link
Copy Markdown

@shiftkey ah that makes sense. Then yeah, I am not sure if there is a better way in windows to read from the registry when this group policy exists :(

@j-f1

j-f1 commented Nov 17, 2017

Copy link
Copy Markdown
Contributor

@shiftkey

getting additional work incorporated seems unlikely in the short term: CatalystCode/windows-registry-node#52

You could fork the repo and npm install desktop/windows-registry-node.

@shiftkey

Copy link
Copy Markdown
Member Author

@j-f1 after spending a bunch of time with windows-registry-node I don't think it's worth trying to mash in here. I've got some simple native code which I'll package up once it's working correctly and use that instead.

@damieng

damieng commented Nov 17, 2017 via email

Copy link
Copy Markdown

@shiftkey

shiftkey commented Nov 17, 2017 •

Copy link
Copy Markdown
Member Author

@damieng sure thing - I only currently need RegEnumValue (and crimping a lot from this sample) but let me know if there's other functionality aside from win-shell.js that's needed and I can pull that in at some stage.

@shiftkey

shiftkey commented Dec 1, 2017

Copy link
Copy Markdown
Member Author

Alright, I went back to the drawing board and have a working registry API that's pure C++. The start is desktop/registry-js#9 and I'll open a PR to migrate us over to this new library once it does what we need it to do.

@shiftkey shiftkey closed this Dec 1, 2017
@shiftkey
shiftkey deleted the i-hate-you-registry-with-the-fire-of-a-thousand-suns branch December 3, 2017 05:30
@shiftkey shiftkey mentioned this pull request Dec 5, 2017
18 tasks done
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.

6 participants