Repository navigation
Invoke-WebRequest and Invoke-RestMethod do not decode content in accordance with BOM/Content-Type #11547
Copy link
Copy link
Open
Labels
HacktoberfestPotential candidate to participate in HacktoberfestPotential candidate to participate in HacktoberfestIn-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issueIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues
Description
Unrecognizable and processed, garbled.
Example
$url='https://storage.live.com/items/A78ACCAEBB24EDD7!37945?&authkey=!APfFKTYtceWCfG0'
$g='./xmltest'
$reg='pN|utf'
((irm $URL) -split "[`r`n]+") -match $reg
irm $URL -outfile $g
(get-content $g)-match $regExpected
PS /sh> irm $URL
xml Folder
--- ------
version="1.0" encoding="utf-8" Folder
PS /sh> (irm $URL).Folder.Items.Document
ItemType ResourceID RelationshipName
-------- ---------- ----------------
Document A78ACCAEBB24EDD7!37948 测试.jsonResults
PS /sh> (iwr $URL).Headers.'Content-Type'
text/xml
PS /sh> ((irm $URL) -split "[`r`n]+") -match $reg
<?xml version="1.0" encoding="utf-8"?>
<RelationshipName>æµè¯.json</RelationshipName>
<RelationshipName>BingClients</RelationshipName>Read saved files,Seems no problem.
PS /s> (get-content ../aa/irm )-match 'pN|utf'
<?xml version="1.0" encoding="utf-8"?>
<RelationshipName>测试.json</RelationshipName>
<RelationshipName>BingClients</RelationshipName>
PS /sdcard/Documents/sh>curl
PS /sdcard/Documents/sh> ((curl $URL) -split "[`r`n]+") -match $reg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2693 100 2693 0 0 2170 0 0:00:01 0:00:01 --:--:-- 2170
<?xml version="1.0" encoding="utf-8"?>
<RelationshipName>测试.json</RelationshipName>
<RelationshipName>BingClients</RelationshipName>Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
HacktoberfestPotential candidate to participate in HacktoberfestPotential candidate to participate in HacktoberfestIn-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issueIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues
Activity
he852100 commentedon Jan 10, 2020
iSazonov commentedon Jan 11, 2020
he852100 Please add info about PowerShell version. Can you repo with latest PowerShell Core build?
he852100 commentedon Jan 12, 2020
Windows.net
PowerShell obeys the standard by assuming ISO-8859-1, but unfortunately the site is using UTF-8.
he852100 commentedon Jan 12, 2020
Ilya (@iSazonov) It can be determined that powershell does not recognize utf8bom
iSazonov commentedon Jan 12, 2020
he852100 I guess it comes from .Net Core.
scriptingstudio commentedon Jan 13, 2020
That comes from PS5 and older. If website saying, i'm
utf8, why doesiwrreturnascii?mklement0 commentedon May 31, 2020
Note: I don't know what the intended behavior is, but here is what seems to be happening:
Because the response doesn't indicate a character encoding (
charset) in itsContent-Typeheader field (text/xmlrather thantext/xml; charset=utf-8), PowerShell defaults to ISO-8859-1, in accordance with the - obsolete since 2014 - RFC 2616.Because it blindly assumes ISO-8859-1, the UTF-8 BOM is read as data, and the payload is therefore not recognized as XML, which falls back to a(n incorrectly decoded) string instead of returning an
XmlDocumentinstance.Note that current RFC, RFC 7231, no longer mandates an overall default and instead defers to the default encoding of the given media type.
For XML, RFC 7303 mandates looking at the BOM first and if there is none at the
charsetattribute in theContent-Typeheader. If that isn't present either, respect the encoding specified in the XML declaration, and if there is none, default to UTF-8.Given that HTML5 now also defaults to UTF-8 and given that RFC 2616 is obsolete, we should consider implementing the following logic in both
Invoke-WebRequestandInvoke-RestMethod:charsetattribute inContent-Type<?xml version="1.0" encoding="ISO-8859-1" ?>) / HTML<meta>element, if presentiSazonov commentedon Jun 1, 2020
Currently we have many workarounds. I guess they comes from PS 5.0.
Now we could use HttpContent.ReadAsStringAsync() method. It seems it already has the decoding logic
https://github.com/dotnet/runtime/blob/bd6cbe3642f51d70839912a6a666e5de747ad581/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs#L182
mklement0 commentedon Jun 1, 2020
That's promising, Ilya (@iSazonov), but it looks like the referenced method gives precedence to the
charsetattribute over the payload's BOM, correct?This is the reverse of how XML data is supposed to be handled according to RFC 7303 (leaving the additional need to respect an encoding in the XML declaration aside), and, arguably, for all textual media types, according to section "5. Security Considerations" of RFC 6657:
A BOM is an instance of in-band information, whereas the
charsetheader-field attribute is out-of-band information; therefore, the BOM should take precedence.Therefore, the method you link to wouldn't solve the problem described in #12861, for instance.
iSazonov commentedon Jun 2, 2020
It looks like a .Net bug. You could open new issue in .Net Runtime repo.
In common, I guess we could simplify the PowerShell code if we would follow the .Net API.
[-][My bug report]irm,iwr get xml Problem[/-][+]Invoke-WebRequest and Invoke-RestMethod do not decode content in accordance with BOM/Content-Type[/+]SteveL-MSFT commentedon Oct 5, 2022
@PowerShell/wg-powershell-cmdlets reviewed this. We agree that the BOM should take precedence and where it makes sense, the web cmdlets should have the same behavior as curl. We're explicitly not making any statement about implementation
microsoft-github-policy-service commentedon Nov 15, 2024
📣 Hey he852100, how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback