Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSCredential containing German umlauts in New-WebServiceProxy

I try to create a PSCredential object with a password that contains german umlauts and pass it to the New-WebServiceProxy cmdlet. The code works as expected as long as the password doesn't contain any umlauts like in the following example:

$secp = ConvertTo-SecureString 'abÜ312!' -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential('\user@db', $secp)
$proxy = New-WebServiceProxy -Uri "https://example.com/webservice/myWs?wsdl" -Credential $mycreds

In this case, I get the following error message:

New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.

When I capture the traffic using e. g. Fiddler, I see that the New-WebServiceProxy cmdlet is adding the credentials as Basic Authorization with a base64 encoding:

GET https://example.com/webservice/myWs?wsdl HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)
Authorization: Basic dXNlckBkYjphYtwzMTIh
Host: example.com

This is how the decoded Base64 string looks like in Utf8:

String in UTF8

So it looks like PowerShell is encoding the Umlauts as an ANSI string. When I manually base64 encode the Credentials (dXNlckBkYjphYsOcMzEyIQ== instead of dXNlckBkYjphYtwzMTIh) and reply the fiddler request, I get the desired response.

Unfortunately, I am not able to use this workaround because the New-WebServiceProxy doesn't allow me to add the Authorization header myself.

Any ideas?

like image 792
Martin Brandl Avatar asked Apr 28 '21 13:04

Martin Brandl


2 Answers

Interesting. Your workaround led me to this more general workaround, which does not depend on saving incorrectly encoded script files. Instead, it incorrectly encodes the password string only

Let me know if this works.

$pw = 'abÜ312!'
# incorrectly encode the UTF8-bytes as ANSI (this yields abÜ312!)
$dummy = [Text.Encoding]::Default.GetString([Text.Encoding]::UTF8.GetBytes($pw))
$secp = ConvertTo-SecureString $dummy -AsPlainText -Force

I assume this will not work for any characters, but I don't know enough about encodings to say for sure.

like image 179
marsze Avatar answered Oct 24 '22 01:10

marsze


The linked answer from mklement0 led me to the solution:

The script was written and saved using PowerShell ISE. I just realized that ISE was saving the file using the UTF-8 with BOM encoding. If I change the encoding to UTF-8 everything works.

Here is a short script to change the encoding of a file to UTF-8:

$scriptPath = "c:/path/to/script.ps1"
$content = Get-Content -Raw $scriptPath
[System.IO.File]::WriteAllLines($scriptPath, $content, (New-Object System.Text.UTF8Encoding))
like image 2
Martin Brandl Avatar answered Oct 24 '22 03:10

Martin Brandl