-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for seekable stream for malformed uri handling
- We must be able to seek to create a temporary file requried for uri handling - We should throw the same exception we did in v2.x - If compat mode is on, this will throw at open time (i.e. it will eagerly load everything) Fixes #1636
- Loading branch information
1 parent
dadb7e2
commit cdac38f
Showing
9 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Framework; | ||
using DocumentFormat.OpenXml.Packaging; | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using DocumentFormat.OpenXml.Validation; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
using static DocumentFormat.OpenXml.Tests.TestAssets; | ||
|
||
namespace DocumentFormat.OpenXml.Tests | ||
{ | ||
public class DocumentOpenTests | ||
|
@@ -35,5 +39,101 @@ public void ThrowIfFileCannotBeFound() | |
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, true, new OpenSettings())); | ||
Assert.Throws<FileNotFoundException>(() => PresentationDocument.Open(NonExistantFile, false, new OpenSettings())); | ||
} | ||
|
||
[Theory] | ||
[InlineData(FileAccess.Read)] | ||
[InlineData(FileAccess.ReadWrite)] | ||
public void RewriteMalformedUriLong(FileAccess access) | ||
{ | ||
// Arrange | ||
const string ExpectedMalformedUri = "mailto:[email protected];%[email protected];%[email protected];%[email protected];%[email protected];%[email protected]?subject=Unsubscribe%20Request&body=Please%20unsubscribe%20me%20from%20all%20future%20communications"; | ||
const string Id = "rId1"; | ||
|
||
using var file = OpenFile(TestFiles.Malformed_uri_long_xlsx, access); | ||
using var stream = file.Open(); | ||
using var doc = SpreadsheetDocument.Open(stream, isEditable: file.IsEditable); | ||
|
||
// Act | ||
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single(); | ||
var hyperlinkRelationship = worksheetPart.HyperlinkRelationships.Single(); | ||
var worksheet = Assert.IsType<Worksheet>(worksheetPart.RootElement); | ||
var hyperlink = Assert.Single(worksheet.Descendants<Hyperlink>()); | ||
|
||
// Assert | ||
Assert.Equal(Id, hyperlinkRelationship.Id); | ||
Assert.Equal(Id, hyperlink.Id); | ||
Assert.Equal(ExpectedMalformedUri, hyperlinkRelationship.Uri.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(FileAccess.Read)] | ||
[InlineData(FileAccess.ReadWrite)] | ||
public void RewriteMalformedUri(FileAccess access) | ||
{ | ||
// Arrange | ||
const string Id = "rId1"; | ||
|
||
using var file = OpenFile(TestFiles.Malformed_uri_xlsx, access); | ||
using var stream = file.Open(); | ||
using var doc = SpreadsheetDocument.Open(stream, isEditable: file.IsEditable); | ||
|
||
// Act | ||
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single(); | ||
var hyperlinkRelationship = worksheetPart.HyperlinkRelationships.Single(); | ||
var worksheet = Assert.IsType<Worksheet>(worksheetPart.RootElement); | ||
var hyperlink = Assert.Single(worksheet.Descendants<Hyperlink>()); | ||
|
||
// Assert | ||
Assert.Equal(Id, hyperlink.Id); | ||
Assert.Equal(Id, hyperlinkRelationship.Id); | ||
Assert.Equal("mailto:one@", hyperlinkRelationship.Uri.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void NonSeekableRewriteMalformedUri() | ||
{ | ||
// Arrange | ||
using var stream = GetStream(TestFiles.Malformed_uri_xlsx); | ||
using var doc = SpreadsheetDocument.Open(new NonSeekableStream(stream), isEditable: false); | ||
|
||
// Act/Assert | ||
var worksheetPart = doc.WorkbookPart.WorksheetParts.Single(); | ||
var exception = Assert.Throws<OpenXmlPackageException>(() => worksheetPart.HyperlinkRelationships.Single()); | ||
|
||
Assert.IsType<UriFormatException>(exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void NonSeekableRewriteMalformedUriCompatMode() | ||
{ | ||
// Arrange | ||
using var stream = GetStream(TestFiles.Malformed_uri_xlsx); | ||
|
||
// Act/Assert | ||
var exception = Assert.Throws<OpenXmlPackageException>(() => SpreadsheetDocument.Open(new NonSeekableStream(stream), isEditable: false, new OpenSettings { CompatibilityLevel = CompatibilityLevel.Version_2_20 })); | ||
Assert.IsType<UriFormatException>(exception.InnerException); | ||
} | ||
|
||
private sealed class NonSeekableStream : DelegatingStream | ||
{ | ||
public NonSeekableStream(Stream innerStream) | ||
: base(innerStream) | ||
{ | ||
} | ||
|
||
public override bool CanSeek => false; | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | ||
|
||
public override long Position | ||
{ | ||
get => throw new NotSupportedException(); | ||
set => throw new NotSupportedException(); | ||
} | ||
|
||
public override long Length => throw new NotSupportedException(); | ||
|
||
public override void SetLength(long value) => throw new NotSupportedException(); | ||
} | ||
} | ||
} |