Configuration file
A configuration file holds your policy: the licenses, packages, versions and feeds you allow, deny or want a warning about. You don't need to write one by hand to get started. Run packageguard init to generate one from the licenses your dependencies actually use, and edit it from there.
Risk-based rules, such as a maximum risk score or a minimum package age, live in the same file. They are described in Risk gates and exceptions.
Configuration format
A configuration file looks like this:
{
"$schema": "https://packageguard.org/schemas/config.json",
"settings": {
"allow": {
"prerelease": false,
"licenses": [
"Apache-2.0", // Uses SPDX naming
"MIT",
],
"packages": [
"MyPackage/[7.0.0,8.0.0)",
"Microsoft.Extensions.*"
],
"feeds": [
"*dev.azure.com*"
]
},
"deny": {
"licenses": [],
"packages": [
"ProhibitedPackage",
"Legacy.*"
]
},
"ignoredFeeds": [
"https://pkgs.dev.azure.com/somecompany/project/_packaging/myfeed/nuget/v3/index.json"
]
}
}
This example does the following:
- It allows NuGet and npm packages with the MIT or Apache-2.0 license.
- It always allows packages whose ID starts with
Microsoft.Extensions., and packages that come from a feed ondev.azure.com. - It only allows
MyPackagein version 7. - It denies
ProhibitedPackage, every package whose ID starts withLegacy., and all pre-release versions (such as1.0.2-beta.2).
Both the allow and deny sections support the licenses and packages properties.
Deny rules always take precedence over allow rules. A package that matches the deny section is blocked, whatever the allow section says.
Configuration files may contain comments. Both // line and /* block */ comments are ignored, so you can explain why a license or package is allowed or denied.
Editor support
PackageGuard publishes a JSON schema for the configuration file. Add a $schema property at the top of the file, as in the example above, and editors such as Visual Studio Code, Visual Studio and Rider will complete the setting names, show what each setting does, and flag typos such as maxOveralRisk. PackageGuard itself ignores the $schema property. packageguard init adds it for you.
PackageGuard does not check the file against the schema when it runs. It silently ignores settings it doesn't know, so the schema is the easiest way to catch a misspelled setting.
The schema is stricter than PackageGuard itself:
- PackageGuard reads setting names in any casing, so
"MaxOverallRisk"works too. The schema only accepts the camelCase names used on this site, such asmaxOverallRisk. - PackageGuard also accepts numbers and booleans written as text, such as
"60"or"true". The schema expects real numbers and booleans.
Where PackageGuard looks for configuration
PackageGuard finds configuration files automatically, based on your solution and project structure. You can define a repository-wide policy at the solution level and add project-specific rules where you need them. It looks in this order:
- Solution level:
packageguard.config.jsonin the same folder as your.sln,.slnxorpackage.jsonfile - Solution level:
config.jsonin a.packageguardsubfolder of your solution orpackage.jsonfolder - Project level:
packageguard.config.jsonin individual project folders - Project level:
config.jsonin a.packageguardsubfolder of project folders
PackageGuard merges the settings of all files it finds. For boolean values, project-level settings take precedence over solution-level settings. Arrays (packages, licenses, feeds) are combined. PackageGuard scans one package.json per run, so for npm it uses the configuration that belongs to that folder.
Example: multi-level configuration
Solution-level configuration (MySolution/packageguard.config.json):
{
"settings": {
"allow": {
"licenses": ["MIT", "Apache-2.0"],
"packages": ["Microsoft.*", "System.*"]
},
"deny": {
"packages": ["UnsafePackage"]
}
}
}
Project-level configuration (MySolution/WebProject/packageguard.config.json):
{
"settings": {
"allow": {
"licenses": ["BSD-3-Clause"],
"packages": ["WebSpecificPackage/[1.0.0,2.0.0)"]
}
}
}
The effective configuration for WebProject is:
- Allowed licenses: MIT, Apache-2.0, BSD-3-Clause (merged)
- Allowed packages:
Microsoft.*,System.*,WebSpecificPackage/[1.0.0,2.0.0)(merged) - Denied packages: UnsafePackage (inherited)
Using a specific file
Use --config-path to skip the automatic discovery and use one file:
packageguard --config-path path/to/my-config.json
If the file does not exist, PackageGuard stops with an error instead of using the default policy. That way a typo in the path doesn't go unnoticed.
Running without a configuration
If PackageGuard finds no configuration file, it still runs. It then allows only well-known permissive licenses, such as MIT, Apache-2.0, the BSD licenses and ISC, and applies no other rules. A package with any other license, or with a license that could not be found, is reported as a violation. PackageGuard prints a warning about this once per run.
NuGet source warnings
PackageGuard warns when the effective NuGet configuration lets nuget.org and another enabled feed supply the same package IDs. To fail the scan on these warnings, add this setting to the PackageGuard configuration:
{
"settings": {
"failOnNugetSourceWarnings": true
}
}
The default is false. Each project inherits the setting from its solution configuration unless a project configuration explicitly overrides it. With --config-path, the specified file applies to every project. --ignore-violations prevents these warnings from failing the scan.
See NuGet source warnings for how the check works and its limitations.
Licenses
License names are case-insensitive and follow the SPDX identifier naming conventions. PackageGuard also supports some proprietary Microsoft licenses, such as the one used by the Microsoft.AspNet.WebApi* packages. For those, use the license name Microsoft .NET Library License.
Many packages declare an SPDX license expression instead of a single license. PackageGuard evaluates these expressions:
| Expression | Allowed when | Denied (or warned about) when |
|---|---|---|
MIT OR Apache-2.0 | any option is allowed, because you can choose | every option is denied |
MIT AND Unicode-DFS-2016 | every license is allowed | any license is denied |
GPL-2.0-only WITH Classpath-exception-2.0 | the full term or GPL-2.0-only is allowed | only the full term is denied, because the exception relaxes the license terms |
Apache-2.0+ or GPL-2.0-or-later | the license or its base version (Apache-2.0, GPL-2.0-only) is allowed | the license itself is denied |
Some more details:
- Parentheses work as expected, and the operators are not case-sensitive.
- An entry in your configuration can also be an expression, such as
"GPL-2.0-only WITH Classpath-exception-2.0". - The order of the licenses in an
ORorANDdoes not matter. The entry"MIT OR Apache-2.0"also matchesApache-2.0 OR MIT. - Deprecated identifiers are treated as their current form.
GPL-3.0matchesGPL-3.0-onlyandGPL-2.0+matchesGPL-2.0-or-later. - When an expression is allowed because of one of its licenses,
explainnames that license, for examplematches allow list license entry "MIT" (from "MIT OR Apache-2.0"). When it takes more than one entry, such as both licenses of anAND,explainnames all of them.
The legal risk score follows the same rules: it uses the least restrictive option of an OR and the most restrictive license of an AND.
Packages and versions
Package names under allow.packages and deny.packages support wildcard patterns (* and ?). They can also include a NuGet-compatible version (range), separated by /.
Examples:
Microsoft.Extensions.*matches all package IDs starting withMicrosoft.Extensions.MyCompany.A*matches IDs such asMyCompany.AbstractionsMicrosoft.*/[9.0.0,10.0.0)matches all Microsoft package IDs from version 9.0.0 (inclusive) up to 10.0.0 (exclusive)
Here's a summary of the version-range notations:
| Notation | Valid versions |
|---|---|
Package/1.0 | 1.0 |
Package/[1.0,) | v ≥ 1.0 |
Package/(1.0,) | v > 1.0 |
Package/[1.0] | v == 1.0 |
Package/(,1.0] | v ≤ 1.0 |
Package/(,1.0) | v < 1.0 |
Package/[1.0,2.0] | 1.0 ≤ v ≤ 2.0 |
Package/(1.0,2.0) | 1.0 < v < 2.0 |
Package/[1.0,2.0) | 1.0 ≤ v < 2.0 |
Warnings instead of build failures
Set settings.severity to "warning" to report every non-malicious policy violation as a warning and keep the exit code at 0.
Set allow.severity or deny.severity to change the severity of an entire section.
Individual deny risk gates also accept a matching *Severity setting, such as "maxOverallRiskSeverity": "warning".
A more specific severity always overrides a less specific one: a risk gate's own *Severity setting overrides deny.severity, which overrides settings.severity. For example:
{
"settings": {
"severity": "warning",
"deny": {
"packages": ["Some.Risky.Package"],
"maxOverallRisk": 70,
"maxOverallRiskSeverity": "error"
}
}
}
Here every policy violation is a warning by default, including denying Some.Risky.Package by name, but a package that exceeds the overall risk score of 70 still fails the build.
A warn section works like deny for packages, licenses and prerelease. A match only logs a warning instead of failing the build. A deny match always takes precedence over a warn match on the same package.
{
"settings": {
"warn": {
"licenses": ["GPL-3.0"],
"packages": ["some-package"]
}
}
}
To turn every deny violation into a warning at run time, pass --treat-deny-as-warning or set the PACKAGEGUARD_DENY_AS_WARNING environment variable. This is useful while you work through the violations of a new rule but still want to ship. Setting the variable to any value enables it. Set it to false (case-insensitive) to disable it explicitly. This applies to both the ordinary deny rules and the risk gates.
Feeds
PackageGuard finds the NuGet and npm feeds the same way dotnet, your npm package manager or your IDE does. It also uses the configured credential providers, so authenticated and private feeds work.
To allow every package from a particular feed, even if it doesn't match the licenses or packages under allow, add a feeds element under allow. Each entry is a wildcard pattern that matches the name or URL of the feed.
{
"settings": {
"allow": {
"feeds": ["*dev.azure.com*"]
}
}
}
To stop PackageGuard from accessing a feed at all, add it to the ignoredFeeds element. PackageGuard may still run dotnet restore if project.assets.json doesn't exist yet, and that restore uses all available NuGet feeds. Use --skip-restore to prevent that.