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

Allow case insensitive comparison of X-Frame-Options #8993

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using NuGetGallery.FunctionalTests.Helpers;
Expand All @@ -23,7 +24,7 @@ public override IEnumerator<WebTestRequest> GetRequestEnumerator()
// Send a request to home page and check for security headers.
var homePageRequest = new WebTestRequest(UrlHelper.BaseUrl);
homePageRequest.ParseDependentRequests = false;
homePageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Frame-Options: deny").Validate;
homePageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Frame-Options: DENY", StringComparison.OrdinalIgnoreCase).Validate;
homePageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-XSS-Protection: 1; mode=block").Validate;
homePageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Content-Type-Options: nosniff").Validate;
homePageRequest.ValidateResponse += new ValidationRuleFindHeaderText("Strict-Transport-Security: max-age=31536000").Validate;
Expand All @@ -32,7 +33,7 @@ public override IEnumerator<WebTestRequest> GetRequestEnumerator()
// Send a request to Packages page and check for security headers.
var packagesPageRequest = new WebTestRequest(UrlHelper.PackagesPageUrl);
packagesPageRequest.ParseDependentRequests = false;
packagesPageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Frame-Options: deny").Validate;
packagesPageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Frame-Options: DENY", StringComparison.OrdinalIgnoreCase).Validate;
packagesPageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-XSS-Protection: 1; mode=block").Validate;
packagesPageRequest.ValidateResponse += new ValidationRuleFindHeaderText("X-Content-Type-Options: nosniff").Validate;
packagesPageRequest.ValidateResponse += new ValidationRuleFindHeaderText("Strict-Transport-Security: max-age=31536000").Validate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.VisualStudio.TestTools.WebTesting;
using System;

namespace NuGetGallery.FunctionalTests.Helpers
{
Expand All @@ -12,15 +13,22 @@ public class ValidationRuleFindHeaderText
: ValidationRule
{
private readonly string _findText;
private readonly StringComparison _stringComparison;

public ValidationRuleFindHeaderText(string findText)
public ValidationRuleFindHeaderText(string findText) : this(findText, StringComparison.Ordinal)
{
_findText = findText;
}

public ValidationRuleFindHeaderText(string findText, StringComparison stringComparison)
{
_findText = findText;
_stringComparison = stringComparison;
}

public override void Validate(object sender, ValidationEventArgs e)
{
e.IsValid = e.Response.Headers.ToString().Contains(_findText);
e.IsValid = e.Response.Headers.ToString().IndexOf(_findText, _stringComparison) >= 0;
}
}
}