JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
15.75k stars 2.28k forks source link

Please adjust functions to support Constrained Languagemode in powershell. #1100

Closed borren closed 2 years ago

borren commented 2 years ago

Code of Conduct

What happened?

With security tightening by the week nowdays, you should really join us who are trying our best to stay in a non elevated prompt! Running powershell in Constrained languagemode does not permit the use of [any.system.class]::invocationcall but for the base functionality it seems there are only a few of those methods in use. bild

All the EnvironmentVariable handling should be really easy to take care of with regular powershell functions with the help of $ENV:varname and/or in combination with Set-Item

Not sure about how to solve the [console]::OutputEncoding = New-Object System.Text.UTF8Encoding, i havent looked at the sourcecode. But i think there should is a simple solution för that as well, without having to invoke a .sourced class.

Love your work!! /borren

Version

5.10.1

Theme

default jandedobbeleer.omp.json and my custom theme are both affected :)

What OS are you seeing the problem on?

Windows

Which shell are you using?

powershell

Relevant log output

New-Object:
Line |
   3 |  … ing = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
     |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot create type. Only core types are supported in this language mode.
InvalidOperation:
Line |
  16 |      $value = [System.Environment]::GetEnvironmentVariable($Name)
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                     | Cannot invoke method. Method invocation is supported only on core types in this language mode.                   InvalidOperation:                                                                                                       Line |                                                                                                                    18 |          [System.Environment]::SetEnvironmentVariable($Name, $false)
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot invoke method. Method invocation is supported only on core types in this language mode.
InvalidOperation:
Line |
  16 |      $value = [System.Environment]::GetEnvironmentVariable($Name)
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot invoke method. Method invocation is supported only on core types in this language mode.
InvalidOperation:
Line |
  18 |          [System.Environment]::SetEnvironmentVariable($Name, $false)
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot invoke method. Method invocation is supported only on core types in this language mode.
lnu commented 2 years ago

Where it comes from: https://github.com/JanDeDobbeleer/oh-my-posh/pull/528

~~By default, powershell core switched to utf8nobom(https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.1).
This is used for regular powershell:~~

# Powershell doesn't default to UTF8 just yet, so we're forcing it as there are too many problems
# that pop up when we don't
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

so if you're using powershell core, maybe an extra step could be done to do a check before doing this. But if it's a regular powershell window, I'm not sure we can avoid this.

for info, I checked without setting the encoding, and even with the latest version of powershell core my encoding is not set to utf-8. image and with powershell 5.1: image

JanDeDobbeleer commented 2 years ago

@lnu @borren I can set the env vars using Set-Content -Path $Name -Value $false, that's an easy fix. But for the encoding I just don't seem to find a way.

JanDeDobbeleer commented 2 years ago

@borren I have no alternative for setting UTF-8 correctly. Is it an option for you to set $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage" after importing oh-my-posh?

lnu commented 2 years ago

@borren I have no alternative for setting UTF-8 correctly. Is it an option for you to set $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage" after importing oh-my-posh?

It seems it can be enforced using policies so in that case you have no choice.

Could chcp 65001 be an alternative? Not tested yet but it seems it changes output and input encoding of the console.

=> nope, it does not work...

borren commented 2 years ago

@borren I have no alternative for setting UTF-8 correctly. Is it an option for you to set $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage" after importing oh-my-posh? It is no problem in itself. The setting is forced by policy in my case (but as local admin i can work around it).But it would be nice to not have to run in elevated mode on corporate controlled computers that has a little more security. I can do some digging of my own when i get som e playtime. The fixes for setting env:vars is a good leap forward. Wish powershell wasnt so picky when running $PROFILE scripts, if it is a normal script, signing it is enough for it to work. But one key point in using your beloved oh-my-posh is that it gets loaded by the profile :)

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.