Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Rewrite SafeRoleEnvironment to not use Assembly.LoadWithPartialName #339

Merged
merged 4 commits into from Aug 23, 2018

Conversation

scottbommarito
Copy link

Assembly.LoadWithPartialName is insecure (as caught by Roslyn).

Fortunately, we don't have to use reflection at all, because we can simply try-catch the RoleEnvironment accesses instead.

@scottbommarito
Copy link
Author

scottbommarito commented Aug 17, 2018

Deploying to DEV to test.

@scottbommarito
Copy link
Author

Works great on DEV--functional tests passed and no exceptions or failed requests in AI.

}
catch (Exception e)
{
// If the assembly is not available, a file load exception will be thrown.
// Otherwise, it must be an exception thrown by the loaded assembly itself.
if (!(e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException))
{
throw;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This try-catch will be expensive if these methods are called often. We should try to call once, and if we get one of these exceptions, we should enable a circuit breaker.

Copy link
Author

Choose a reason for hiding this comment

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

added...deploying to DEV again to test

{
return RoleEnvironment.GetLocalResource(name).RootPath;
return TryGetField(() => RoleEnvironment.GetLocalResource(name).RootPath, out path);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can GetLocalResource return null? What is the expected behaviour in that case?

Copy link
Author

Choose a reason for hiding this comment

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

If GetLocalResource is null, it will throw a NullReferenceException, which will be bubbled up to the caller of this method. This is the same behavior as before.

/// <returns>Loaded assembly, if any.</returns>
private static Assembly GetServiceRuntimeAssembly()
private static bool _assemblyIsAvailable = true;
private static bool TryGetField<T>(Func<T> getValue, out T value)
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for generics here. The return value is always string.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch.

The idea, however, is that this will be a proxy for every use of RoleEnvironment, so keeping it like this will make it easier for us to add additional methods if we need to access fields of RoleEnvironment that do not return a string.

If I had noticed that all of our uses of RoleEnvironment were strings initially I would have made it a Func<string>, but at this point there's no reason for the change.

Copy link
Contributor

Choose a reason for hiding this comment

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

This introduces an unnecessary complexity to the code. Agree that it's not a bug deal.

@skofman1
Copy link
Contributor

How did you test the code (besides deploying to dev)?

Copy link
Contributor

@skofman1 skofman1 left a comment

Choose a reason for hiding this comment

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

please respond to comments

@scottbommarito
Copy link
Author

@skofman1 -

I deployed it to DEV (where RoleEnvironment is available) and confirmed that it's able to access RoleEnvironment (deployment ID is being reported).

I also ran it locally (where RoleEnvironment isn't available) and confirmed that it ignored the RoleEnvironment fields.

@scottbommarito scottbommarito merged commit b2a7b2e into dev Aug 23, 2018
@scottbommarito scottbommarito deleted the sb-saferroleenvironment branch August 23, 2018 23:39
ryuyu added a commit that referenced this pull request Sep 11, 2018
* Feed2Catalog:  replace inner loops with one outer loop (#329)
Resolve NuGet/Engineering#1526.
* V3:  rename asynchronous IStorage methods (#336)
Progress on NuGet/NuGetGallery#6267.
* Test:  fix flaky basic search tests (#337)
Progress on NuGet/NuGetGallery#6292.
* Add signing to search service (#338)
Progress on https://github.com/NuGet/Engineering/issues/1644
* Test:  add registration tests (#341)
Progress on NuGet/NuGetGallery#6317.
* remove unused scripts (#340)
* MonitoringProcessor:  improve throughput (#342)
Progress on NuGet/NuGetGallery#6327.
* Catalog2Dnx:  improve throughput (#335)
Progress on NuGet/NuGetGallery#6267.
* Functional tests should not be dependent on a specific hash and file size of the test package (#343)
* Add MicroBuild dependency and signing of output DLLs (#345)
Progress on https://github.com/NuGet/Engineering/issues/1644
* Rewrite SafeRoleEnvironment to not use Assembly.LoadWithPartialName (#339)
* Test:  improve and add registration tests (#346)
More progress on NuGet/NuGetGallery#6317.
* Use ServerCommon commit that is consistent with other repositories (#347)
Progress on https://github.com/NuGet/Engineering/issues/1644
* [Monitoring] Ensure packages are signed (#348)
This adds a validation to ensure indexed packages are signed by verifying that the package contains a [package signature file](https://github.com/NuGet/Home/wiki/Package-Signatures-Technical-Details#-the-package-signature-file).
This validation is disabled for now. To enable this validation, the flag `-expectsSignature true` must be added to ng.exe's command line arguments. This validation will be enabled once all packages are repository signed.
Part of NuGet/Engineering#1627
* V3:  make stylistic consistency and cleanup changes (#349)
Progress on NuGet/NuGetGallery#6411.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants