This article describes which parts of your environment PostSharp trusts. Use it to decide how to secure a shared build machine and the servers that your application uses with PostSharp Caching.
Build time
The project being built is trusted
PostSharp executes aspect code from the project and from its references while it builds the project. This is the same trust that MSBuild gives to the project: building a project runs code from that project. Build only projects and packages that you trust.
The operating system user account is the boundary
PostSharp stores the compiler tools, the caches, and the build server pipe in locations that belong to the current user. By default, these are %LOCALAPPDATA%\PostSharp on Windows and $HOME/.local/share/postsharp on Linux and macOS. PostSharp relies on the operating system to allow only the owner to write to these locations. It does not add access control entries to them and does not verify the integrity of the files that it finds there. All processes that run under one user account have the same trust.
If you move these directories with the PostSharp*Directory MSBuild properties, choose a location that only the build user can write to.
On a shared build machine, run builds of different trust levels under different user accounts.
postsharp.config files in parent directories
PostSharp loads every postsharp.config file in the directory of the project and in all its parent directories, up to the root of the file system. See Configuring Projects Using postsharp.config. A postsharp.config file can load plug-in assemblies and add aspects, so it can execute code during the build.
PostSharp does not check who owns a postsharp.config file. A user who can create files in a parent directory of your project, for instance in the root of a drive or in the folder that contains all workspaces of a shared build agent, can therefore influence the builds below this directory.
MSBuild has the same behavior: it loads Directory.Build.props and Directory.Build.targets from parent directories, and these files can run arbitrary MSBuild tasks. PostSharp does not extend this exposure. To control it, restrict write access to the parent directories of your projects.
Run time
The cache server is trusted
PostSharp Caching serializes the return values of cached methods and stores them in the caching back-end. When it reads a value, the serializer takes the type of the value from the cached data. A party that can write to the cache can therefore choose which type your application instantiates. With serializers that are based on BinaryFormatter or NetDataContractSerializer, this can lead to code execution in your application.
PostSharp treats the cache as part of your application. A party that can write to the cache can also replace the result of any cached method, so the integrity of the application depends on the integrity of the cache regardless of the serializer. Apply the following measures:
- Require authentication on the cache server and restrict network access to the applications that use it.
- Do not share a cache server or a Redis database between applications with different trust levels.
- Do not use BinarySerializer. It is obsolete.
- If you need to restrict the types that can be created from cache content, derive from JsonCachingSerializer and override
ResolveTypeNameto accept known types only.
See Also
Other Resources
Configuring Projects Using postsharp.config
Using Redis Cache
BinaryFormatter security