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

Check SubResource Integrity #9947

Merged
merged 21 commits into from
Mar 1, 2024
Merged

Check SubResource Integrity #9947

merged 21 commits into from
Mar 1, 2024

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Jul 21, 2021

Fixes #9941
Fixes #11915

@hishamco
Copy link
Member Author

@agriffard @deanmarcussen what I notice that many files in my local don't have the same SRI, for example ~/OrchardCore.Resources/Scripts/js.cookie.js so when I download that file from CDN I got the same exact SRI

Could you please let me know if there's something I can do? That's why the Test fails in both my end and GitHub

@hishamco
Copy link
Member Author

The following are the file that have the exact same SRI:

  • ~/OrchardCore.Resources/Scripts/jquery-ui-i18n.min.js
  • ~/OrchardCore.Resources/Scripts/vue-multiselect.min.js
  • ~/OrchardCore.Resources/Scripts/vue-multiselect.min.js

@agriffard agriffard mentioned this pull request Jul 30, 2021
@deanmarcussen
Copy link
Member

I would suggest picking a specific file that doesn't match, and using the online https://www.srihash.org/ that I suspect @agriffard is using to test why one is not working, perhaps they are using different hashing methods.

@hishamco
Copy link
Member Author

@agriffard are using SHA384 when you generate the hash in SRI? Or are you using another one?

@agriffard
Copy link
Member

@agriffard are using SHA384 when you generate the hash in SRI? Or are you using another one?

Yes, I do. I always use https://www.srihash.org/ and the default encryption is SHA-384.

@hishamco
Copy link
Member Author

@agriffard how can I ensure that all the resources in my side are up to date, so I can validate the SRI for them

@hishamco
Copy link
Member Author

FYI I tried npm run but I'm getting an error

@hishamco
Copy link
Member Author

@agriffard could you let me know what should I do here to make a successful restore for npm packages. I need to finalize this PR

@agriffard
Copy link
Member

@agriffard could you let me know what should I do here to make a successful restore for npm packages. I need to finalize this PR

npm install

@hishamco
Copy link
Member Author

NPM

@Skrypt
Copy link
Contributor

Skrypt commented Oct 5, 2021

@hishamco Update your node.js and/or npm to latest LTS version.

@hishamco
Copy link
Member Author

hishamco commented Oct 5, 2021

I already updated Node JS, I need to check again, coz it success with UI Testing project .. I will have a try

@hishamco hishamco changed the title Check SubResourceIntegrity Check SubResource Integrity Oct 5, 2021
@hishamco
Copy link
Member Author

hishamco commented Oct 5, 2021

Gulp

@Skrypt
Copy link
Contributor

Skrypt commented Oct 5, 2021

Once you installed the new node.js and npm try and do a cleanup of the node_modules folder. Then redo npm install.

@hishamco
Copy link
Member Author

@Skrypt is package-lock.json play an important role, what I notice is when I download the exact file from the CDN the SRI validated successfully. This make me think that some of my local assest are not updated

I tried npm run rebuild with no luck :(

@Skrypt
Copy link
Contributor

Skrypt commented Oct 17, 2021

Sometimes I do a git clean -xdf to clean also the package-lock.json files and retrieve those from the repository. It can be a node_modules cache on your computer too. There are so many things that can go wrong in node.js dependency hell...

@hishamco hishamco added the ready label May 30, 2023
@hishamco
Copy link
Member Author

@agriffard this PR is ready, I know the build will fail because I discover there are many invalid SRI, so I will create another PR to fix them. Once you merge the other PR we could rebase on main then the build will pass

@agriffard
Copy link
Member

agriffard commented May 30, 2023

I cancelled the workflows, the build was running without progress for 1h40.

@hishamco
Copy link
Member Author

That's why I didn't the build failed, I might need to check the logs

@hishamco
Copy link
Member Author

Seems the resources take time to validate the SRI, which is weird!!

@Piedone
Copy link
Member

Piedone commented Jan 16, 2024

Is this something you'd like to revisit any time soon @hishamco? Would be quite useful.

@hishamco
Copy link
Member Author

Sure, there are many PRs on my list :)

@Piedone
Copy link
Member

Piedone commented Feb 4, 2024

Let me know if you'd like a review here.

@hishamco hishamco requested a review from Piedone February 4, 2024 22:39
Copy link
Member

@Piedone Piedone left a comment

Choose a reason for hiding this comment

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

@hishamco
Copy link
Member Author

@Piedone time to merge :)

@@ -6,6 +6,8 @@ namespace OrchardCore.Tests.Modules.OrchardCore.Resources;

public class SubResourceIntegrityTests
{
private static readonly HttpClient _httpClient = new();
Copy link
Member

Choose a reason for hiding this comment

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

You also need to dispose this. It doesn't actually need to be static, you can just add a using var into the test method.

Copy link
Member Author

Choose a reason for hiding this comment

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

You also need to dispose this

Agree

It doesn't actually need to be static

The GetSubResourceIntegrityAsync() is static

Copy link
Member

Choose a reason for hiding this comment

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

And you can change that, because you have the infinite power of Visual Studio at your hand :). However, I'm not arguing for that. You can just pass HttpClient into the method. The latest implementation of disposing a static field from an instance method is a wrong approach to this.

Copy link
Member Author

Choose a reason for hiding this comment

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

And you can change that, because you have the infinite power of Visual Studio at your hand

Sure :)

Copy link
Member

Choose a reason for hiding this comment

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

I committed what I think is simpler, please check.

@Piedone
Copy link
Member

Piedone commented Mar 1, 2024

Please request review when you're done, because I just noticed this randomly.

// Assert
var resourceManifest = resourceManagementOptions.ResourceManifests.First();

using var httpClient = new HttpClient();
Copy link
Member Author

@hishamco hishamco Mar 1, 2024

Choose a reason for hiding this comment

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

@Piedone If you remember you told me to use the same instance of HttpClient which you insisted a lot before, now I see you are a new instance :)

Copy link
Member

Choose a reason for hiding this comment

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

This is still one instance during the lifetime of the test, but we don't go the roundabout way of passing a field to a local function.

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, I think we are fine now for merge this, right?

Copy link
Member

Choose a reason for hiding this comment

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

If you're OK with it then yes.

@hishamco hishamco merged commit 4847f89 into main Mar 1, 2024
5 checks passed
@hishamco hishamco deleted the hishamco/#9941 branch March 1, 2024 21:13
@sebastienros
Copy link
Member

Use the new static instances instead of creating new ones: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha384.hashdata?view=net-8.0

@sebastienros
Copy link
Member

I am right that these will run also locally?
If so would you want to create an XUnit attribute to add on the methods which would skip the test if the "CI" environment variable is defined?

public class CiOnlyFactAttribute : FactAttribute
{
    public override string Skip
    {
        get
        {
            // "CI" is defined by GitHub actions
            // "BUILD_BUILDID" is defined by Azure DevOps

            if (Environment.GetEnvironmentVariable("BUILD_BUILDID") != null || 
                Environment.GetEnvironmentVariable("CI") != null)
            {
                return $"nameof(CiOnlyFactAttribute) tests are not run locally. To run them locally create a \"CI\" environment variable.";
            }

            return null!;
        }
    }
}

@hishamco
Copy link
Member Author

hishamco commented Mar 6, 2024

We could do that, but I'm not sure do we still need Azure DevOps, while we're using GitHub actions?

@Piedone
Copy link
Member

Piedone commented Mar 6, 2024

Why would we skip these in CI?

@sebastienros
Copy link
Member

The opposite, skip these locally but not in CI. My wording was wrong, the code is correct ;)

@hishamco
Copy link
Member Author

hishamco commented Mar 8, 2024

I thought what the Seb meant at the first glance 😂, I will submit a PR

@hishamco
Copy link
Member Author

The opposite, skip these locally but not in CI. My wording was wrong, the code is correct ;)

Seb seems the code is wrong, because the fact should be null when the environment valriable exists not the opposite :)

urbanit pushed a commit to urbanit/OrchardCore that referenced this pull request Mar 18, 2024
Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
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.

Check resource definition checksum as part of the CI build Check SubResourceIntegrity
6 participants