Skip to content

Honor content type overrides - #1016

Open
kennethr127 wants to merge 3 commits into
dotnet:mainfrom
kennethr127:kennethr127/honor-content-type-overrides
Open

Honor content type overrides#1016
kennethr127 wants to merge 3 commits into
dotnet:mainfrom
kennethr127:kennethr127/honor-content-type-overrides

Conversation

@kennethr127

@kennethr127 kennethr127 commented May 6, 2026

Copy link
Copy Markdown

Currently, the Overrides in the [Content_Types].xml file in the package are not being honored during signing. This update addresses that issue by checking for any overrides when retrieving the ContentType.

Fixes #1015

@kennethr127
kennethr127 force-pushed the kennethr127/honor-content-type-overrides branch from db8f4bc to 6cd7611 Compare May 6, 2026 21:56
@kennethr127

This comment was marked as resolved.

@kennethr127
kennethr127 marked this pull request as ready for review May 6, 2026 22:24
@kennethr127
kennethr127 requested a review from a team as a code owner May 6, 2026 22:24

@dtivel dtivel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennethr127, thank you for contributing this fix!

I’m requesting a few focused changes before merging:

  • Avoid stripping trailing slashes when matching override part names, since that can make malformed names match valid parts.
  • Make the Override and Default modes explicit when selecting content types.
  • Simplify the test to open the source VSIX once in read-only mode.
  • Use separate NotNull and Equal assertions for clearer failures.

With those changes, this should be ready to merge.

Comment on lines +194 to +206
string path;
using (var package = ShadowCopyPackage(SamplePackageWithOverrides, out path, OpcPackageFileMode.ReadWrite))
{
var partToCheck = new Uri("/extension.vsixmanifest", UriKind.Relative);
var part = package.GetPart(partToCheck);
Assert.True(part != null && part.ContentType == "text/xml");
}
using (var package = OpcPackage.Open(path, OpcPackageFileMode.ReadWrite))
{
var partToCheck = new Uri("/extension.vsixmanifest", UriKind.Relative);
var part = package.GetPart(partToCheck);
Assert.True(part != null && part.ContentType == "text/xml");
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennethr127, is the second open/assertion necessary? Nothing modifies the package between assertions, and ContentType does not depend on read/write mode. Opening SamplePackageWithOverrides once in read-only mode with OpcPackage.Open(...) should provide the same coverage while removing the shadow copy and duplicate verification.

Comment on lines +63 to +64
var defaultContentType = Package.ContentTypes.FirstOrDefault(ct => string.Equals(ct.Extension, extension, StringComparison.OrdinalIgnoreCase));
var overrideContentType = Package.ContentTypes.FirstOrDefault(ct => string.Equals(ct.PartName?.Trim('/'), _path, StringComparison.OrdinalIgnoreCase));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennethr127, use TrimStart('/') instead of Trim('/'). A trailing slash is invalid for an OPC part name, but removing it can silently make a malformed override match a different valid part. For example:

  • /folder/file.txt/ would incorrectly match folder/file.txt.
  • /extensionless/ would incorrectly match the part extensionless.

Removing only the required leading slash avoids accepting malformed override names. Also filter Mode by OpcContentTypeMode.Override and Default to make the intended precedence explicit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be good to add some tests for those cases.

{
var partToCheck = new Uri("/extension.vsixmanifest", UriKind.Relative);
var part = package.GetPart(partToCheck);
Assert.True(part != null && part.ContentType == "text/xml");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennethr127, use separate assertions so failures identify whether the part was missing or had the wrong content type:

Assert.NotNull(part);
Assert.Equal("text/xml", part.ContentType);

Assert.True(part != null && part.ContentType == "text/xml") loses that diagnostic information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Content type overrides in [Content_Types].xml are not honored

4 participants