Security How-to

NuGet can now refuse packages published this week. Here's what that stops in an agent's hands, and the gaps it leaves

The .NET 11 SDK adds a per-source minimum publish age. Real output from every case that matters to an agent that adds packages, plus a 40-line CI check for the cases NuGet doesn't cover.

A gold blueprint drawing of a straight path blocked by a gate with a timer arc beside it and curved branches looping around

An agent asked to “add Redis caching” runs one command, dotnet package add StackExchange.Redis, and takes whatever NuGet calls the latest version. If a maintainer’s account was compromised an hour ago, the latest version is the poisoned one, and it’s now in your project file, your restore cache and the agent’s next dotnet build. Nobody had a chance to notice.

The .NET 11 SDK, released on 10 November 2026, adds a setting that closes that window for package adds and updates: a minimum publish age per package source. Set it to 72 hours, and dotnet package add skips anything published in the last three days. It’s opt-in, it needs NuGet 8.0 or newer, and it covers less than the name suggests. This piece runs each case against nuget.org so you can see which ones are covered, then adds a small CI check for the rest.

The npm equivalent has its own post . If your agent also touches a lockfile, the lockfile gate is the other half of this one.

Turn it on

The setting is the minPublishAgeHours attribute on a source in nuget.config, in whole hours. Commit the file at the repository root so the agent, CI and every contributor share it. Microsoft’s docs recommend exactly that.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" minPublishAgeHours="72" />
  </packageSources>
  <minPublishAgeExceptions />
</configuration>

Two details matter. <clear /> stops sources from user-level config leaking in with no age at all. The empty minPublishAgeExceptions element matters because that section replaces any inherited exception list instead of adding to it, so an exception in a machine-wide file can’t quietly switch your policy off.

You can also write the attribute from the command line:

dotnet nuget update source nuget.org --min-publish-age-hours 48 --configfile nuget.config

Run it and the file changes as you’d hope:

Package source "nuget.org" was successfully updated.
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" minPublishAgeHours="48" />

That option comes from the SDK’s dotnet nuget add source and update source commands, per the .NET 11 SDK release notes. The setting is “available starting with NuGet 8.0, .NET SDK 11.0.100, and Visual Studio 18.12”, so a build agent on an older SDK doesn’t have the feature at all.

What it stops: the agent’s package add

Take an empty net11.0 console project with the 72-hour config above. On 13 November 2026, Swashbuckle.AspNetCore 11.0.1 had been on nuget.org for under a day (published 12 November at 12:19 UTC) and 11.0.0 for under three days. The 10.3.1 release dated from 19 October. With no version given:

dotnet package add Swashbuckle.AspNetCore
info : PackageReference for package 'Swashbuckle.AspNetCore' version '10.3.1' added to file '/tmp/age/Shop/Shop.csproj'.

The same command in a project with an age-free source picks 11.0.1:

info : PackageReference for package 'Swashbuckle.AspNetCore' version '11.0.1' added to file '/tmp/age/ctl/C.csproj'.

That’s the whole feature in two lines. The agent didn’t need to know about the policy, and no prompt had to ask for caution. The legacy spelling dotnet add package behaves the same way: with StackExchange.Redis, whose 3.4.35 was 21 hours old, it added 3.4.31 (published on 2 November).

Updates are handled too, and they say so out loud. With the project pinned to an old 3.4.1:

dotnet package update
Packages with newer versions that have not reached the configured minimum publish age: StackExchange.Redis.
  Shop:
    Updating StackExchange.Redis 3.4.1 to 3.4.31.

That message is worth having in an agent’s transcript. The agent can report “a newer version exists but is too young” instead of concluding it’s already current.

What it doesn’t stop: four gaps

The docs are blunt about the limits: the age “filters automatic version choices; it isn’t a general rule that blocks package downloads.” Each gap below is one an agent can fall through by accident.

An explicit version wins

Ask for a version by name and the filter steps aside:

dotnet package add Swashbuckle.AspNetCore --version 11.0.1
info : PackageReference for package 'Swashbuckle.AspNetCore' version '11.0.1' updated in file '/tmp/age/Shop/Shop.csproj'.

There was no error, even though the config says 72 hours and that version was under 24 hours old. It’s a sensible escape hatch for a human who has reviewed a release. It’s a hole when a prompt says “use the latest StackExchange.Redis, version 3.4.35”, or when an agent copies a version number out of a blog post or a README snippet.

A hand-edited version restores fine

Edit the Version attribute directly and restore doesn’t care how old it is:

sed -i 's/Version="[^"]*"/Version="3.4.35"/' Shop.csproj
dotnet restore
  Determining projects to restore...
  Restored /tmp/age/Shop/Shop.csproj (in 1.26 sec).

Agents edit project files directly all the time, especially when a package is already in the file and they’re “bumping” it. Microsoft’s page says the same: restore doesn’t check age or warn when a restored version is too new, and that includes transitive dependencies.

Floating versions fail loudly

With a nonzero age on a source, a floating reference such as 3.4.* stops restore:

/tmp/age/Shop/Shop.csproj : error NU1020: The following PackageReference items cannot specify a floating version because package source cooldown is enabled: StackExchange.Redis.
  Failed to restore /tmp/age/Shop/Shop.csproj (in 17 ms).

That one is a feature, not a gap: NuGet won’t silently pick a young version for you. It also doesn’t fall back to an older one, so the agent has to pin a version. Expect it to try a floating version first and recover from the error, which is the behaviour you want.

An exception is permanent for that package

Exceptions are patterns using Package Source Mapping syntax, and they apply to every version of a matching package on every source:

<minPublishAgeExceptions>
  <package pattern="StackExchange.*" />
</minPublishAgeExceptions>

With that in place, the same dotnet package add StackExchange.Redis picks 3.4.35, the 21-hour-old version. Use exceptions for your own internal packages (Contoso.*), not for a public package you got tired of waiting for. To adopt one reviewed release, pass --version once and leave the policy alone.

Close the transitive gap in CI

Because restore never checks age, a young version can arrive through a hand edit or as a dependency of something you added. A short script reads obj/project.assets.json after restore, looks up each package’s publish date on nuget.org and fails the build if anything is younger than the floor. It only uses the nuget.org registration API, which is public and returns a published date per version.

#!/usr/bin/env python3
"""Fail if any restored NuGet package (direct or transitive) is younger than N hours."""
import datetime, gzip, json, sys, urllib.request

MIN_HOURS = int(sys.argv[1]) if len(sys.argv) > 1 else 72
ASSETS = sys.argv[2] if len(sys.argv) > 2 else "obj/project.assets.json"
INDEX = "https://api.nuget.org/v3/registration5-gz-semver2/{}/index.json"
now = datetime.datetime.now(datetime.timezone.utc)

def get(url):
    raw = urllib.request.urlopen(url, timeout=30).read()
    try:
        raw = gzip.decompress(raw)
    except OSError:
        pass
    return json.loads(raw)

def published(pkg_id, version):
    index = get(INDEX.format(pkg_id.lower()))
    for page in index["items"]:
        if "items" not in page:
            page = get(page["@id"])
        for item in page["items"]:
            entry = item["catalogEntry"]
            if entry["version"].lower() == version.lower():
                return datetime.datetime.fromisoformat(entry["published"].replace("Z", "+00:00"))
    return None

libs = [k for k, v in json.load(open(ASSETS))["libraries"].items() if v["type"] == "package"]
bad = 0
for lib in sorted(libs):
    pkg_id, version = lib.split("/")
    when = published(pkg_id, version)
    if when is None:
        print(f"UNKNOWN {lib}: no publish date on nuget.org")
        bad += 1
        continue
    age = (now - when).total_seconds() / 3600
    if age < MIN_HOURS:
        print(f"TOO NEW {lib}: published {when:%Y-%m-%d %H:%M}Z, {age:.0f}h old (minimum {MIN_HOURS}h)")
        bad += 1
print(f"checked {len(libs)} packages, {bad} problem(s)")
sys.exit(1 if bad else 0)

With StackExchange.Redis hand-edited to 3.4.35 and restored, the script exits with 1:

TOO NEW RESPite/3.4.35: published 2026-11-12 14:12Z, 21h old (minimum 72h)
TOO NEW StackExchange.Redis/3.4.35: published 2026-11-12 14:13Z, 21h old (minimum 72h)
checked 3 packages, 2 problem(s)

Note the first line. RESPite wasn’t in the project file at all. It came in as a dependency of StackExchange.Redis 3.4.35, which is the transitive gap in action: NuGet’s own age setting would never have looked at it. Change the version to 3.4.31 and the script exits with 0:

checked 3 packages, 0 problem(s)

Wire it in after the restore step, with the same number as minPublishAgeHours:

- run: dotnet restore
- run: python3 scripts/package_age_gate.py 72

Two caveats on the script. It trusts nuget.org’s published date, so for a private feed that mirrors public packages, the date may be the copy time and not the original release. And a package that isn’t on nuget.org at all prints UNKNOWN and fails; if you use internal feeds, skip packages that match your own prefixes before you call the API. The script is also a gate on the result, not a fix: when it fails, a person decides whether to wait or to review that release.

The patch-delay trade-off

A floor delays good releases as well as bad ones. Microsoft’s page spells out that dotnet package update --vulnerable doesn’t bypass the age, so a fix published yesterday for a CVE in your tree won’t be chosen automatically, and the existing reference stays as it is. NuGet Audit still reports the vulnerability, and you can adopt the fix on purpose with an explicit --version after reading the release.

That is the right order for a security fix: a human decides, the agent doesn’t. The docs also note that Dependabot has its own settings, so configure its cooldown separately and don’t assume this one applies.

Three days is a starting point and not a figure from the NuGet docs, which don’t recommend one. Pick the number your team can live with, and revisit it the first time a fix is stuck behind the floor.

Does this work with any agent?

Yes. The policy lives in nuget.config, so it applies to whatever runs dotnet package add, whether that’s Claude Code, Codex, Copilot’s agent or a person. The check is a script over project.assets.json. Neither needs agent-specific hooks, which is also why neither can be skipped by an agent that ignores its instructions file.

First 15 minutes

Install SDK 11.0.100, commit the nuget.config above at the root with minPublishAgeHours="72", and run dotnet package add for a package that shipped this week to see it choose the older release. Then copy the script into scripts/, run it after dotnet restore locally, and add it to the pipeline. If your build agents still use an older SDK, they lack the feature, so check dotnet --version in CI before you trust it.