Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid hyperlink throws exception - Invalid URI: The hostname could not be parsed. #851

Closed
patrickwzl opened this issue Jun 6, 2022 · 10 comments
Labels
Milestone

Comments

@patrickwzl
Copy link

If the content of an Excel contains hyperlink, and the hyperlink is invalid such as "mailto:a@a,com", it will throw the exception "Invalid URI: The hostname could not be parsed.".

@tonyqus
Copy link
Member

tonyqus commented Jun 6, 2022

Can you show me the call stack?

@patrickwzl
Copy link
Author

Just the following code can trigger the problem.

string path = "Some problem Excel files";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
book = new XSSFWorkbook(fs);

Exception stack:
[UriFormatException: Invalid URI: The hostname could not be parsed.]
System.Uri.CreateHostStringHelper(String str, UInt16 idx, UInt16 end, Flags& flags, String& scopeId) +8491635
System.Uri.CreateHostString() +251
System.Uri.EnsureHostString(Boolean allowDnsOptimization) +64
System.Uri.GetComponentsHelper(UriComponents uriComponents, UriFormat uriFormat) +89
System.Uri.ToString() +96
NPOI.POIXMLFactory.GetPackageRelationship(POIXMLDocumentPart parent, PackagePart part) in D:\github\npoi\ooxml\POIXMLFactory.cs:139
NPOI.POIXMLFactory.CreateDocumentPart(POIXMLDocumentPart parent, PackagePart part) in D:\github\npoi\ooxml\POIXMLFactory.cs:51
NPOI.POIXMLDocumentPart.Read(POIXMLFactory factory, Dictionary``2 context) in D:\github\npoi\ooxml\POIXMLDocumentPart.cs:720
NPOI.POIXMLDocumentPart.Read(POIXMLFactory factory, Dictionary``2 context) in D:\github\npoi\ooxml\POIXMLDocumentPart.cs:731
NPOI.POIXMLDocument.Load(POIXMLFactory factory) in D:\github\npoi\ooxml\POIXMLDocument.cs:164
NPOI.XSSF.UserModel.XSSFWorkbook..ctor(Stream is1) in D:\github\npoi\ooxml\XSSF\UserModel\XSSFWorkbook.cs:241

@tonyqus
Copy link
Member

tonyqus commented Jun 7, 2022

Can you upload the file? I'd like to look into the file. Something wrong with the package relationship uri.

@tonyqus
Copy link
Member

tonyqus commented Jun 7, 2022

This is where the exception comes from.

String packName = pr.TargetUri.ToString();// toASCIIString();

@patrickwzl
Copy link
Author

test.xlsx

@tonyqus
Copy link
Member

tonyqus commented Jun 8, 2022

I cannot reproduce the issue with the latest NPOI code. Which NPOI version are you using?

@patrickwzl
Copy link
Author

The latest NPOI version 2.5.6 is used.
I have tried different scenarios and found the problem.

The problem comes from the behaviour changes in .NET framework.
Uri uri = new Uri("mailto:a@a,com"); <-- Throws exception when .NET framework 4.5 or above.

Uri uri = new Uri("mailto:a@a,com");
uri.ToString(); <-- Throws exception before .NET framework 4.5, no probelm on above line.

NPOI has handled the case when Uri uri = new Uri("mailto:a@a,com"); throws exception, so the program does not have problem.
If there is no exception in Uri uri = new Uri("mailto:a@a,com");, then NPOI will throw exception when calling uri.ToString();.

For ASP.NET, even though the system is running latest .NET framework version (e.g. v4.8). If the web.config does not specify the version explicitly <httpRuntime targetFramework="4.x.x" />, the "new Uri" is still using the behaviour like before .NET framework version 4.5.

@tonyqus
Copy link
Member

tonyqus commented Jun 11, 2022

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
</Relationships>

.rels xml example from your uploaded file - test.xlsx

The target Uri of package looks like docProps/app.xml, xl/workbook.xml, docProps/core.xml. There is no comma in it. Are you saying Uri.ToString throws exception with these kind of relative url in .NET framework before 4.5? Or the exception is from another place related to Uri in NPOI code? If so, can you show me the call stack?

@patrickwzl
Copy link
Author

I have created a sample ASP.NET project with NPOI 2.5.6.
testNPOI.zip

Please try to comment and uncomment the line <httpRuntime targetFramework="4.8" /> in web.config.

For newly created ASP.NET projects, this config is added by default in web.config now, but it was not added some years ago by default. Hence, newly created project may not face this problem.

I have simple fix so that the NPOI can work in all situations.
XSSFHyperlink.cs (Line 85)

                Uri target = _externalRel.TargetUri;
                try
                {
                    _location = target.ToString();
                } catch
                {
                    _location = target.OriginalString.Trim().ToLower();
                }

@tonyqus
Copy link
Member

tonyqus commented Jun 12, 2022

Ok. I can reproduce the exception. Nice discovery. Looks Uri.OriginalString is much safer than Uri.ToString() in versions before .NET framework 4.5

@tonyqus tonyqus added bug and removed question labels Jun 12, 2022
@tonyqus tonyqus added this to the NPOI 2.6.0 milestone Jun 12, 2022
artem-iron pushed a commit to jake-codes-at-5-am/npoi that referenced this issue Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants