Disclaimer: This article reflects my personal views and experiences and does not represent the official stance of Cloudflare. PkgGuard is a personal project, not a Cloudflare product, and the Forward action is in public beta at the time of writing.

What happens when a Gateway policy can run your own code.


TL;DR — Gateway’s package registry security already sees every package your developers download: the ecosystem, the name, the version, and who asked. What it cannot know on its own is which of those packages are malicious this week. The Forward action closes that gap. Gateway keeps doing the proxying, the TLS decryption and the identity, and hands matching traffic to a Worker where you write the logic. I used it to build PkgGuard, which blocks known-malicious npm, PyPI, Cargo, RubyGems, NuGet and Go packages using the OSV.dev feed, with a single Gateway rule. The package use case is only the example. The real story is that you can now put your own decision in the path of traffic Cloudflare already inspects.

The code is public at github.com/macharpe/pkgguard , with the README, the architecture and the v0.4.0 release.


The question that started this

I was giving a customer a demo of Gateway’s package registry security feature. The setup was simple: a laptop enrolled in Cloudflare One, TLS decryption on for the registry hosts, and the pkg.* selectors, which let you write policies on pkg.ecosystem, pkg.name and pkg.version. I wrote the obvious one:

1
pkg.ecosystem == "npm" and pkg.name == "event-stream"

Action: Block. I ran npm install in front of them, and it stopped. It worked exactly as documented, and the room liked it.

Then the customer asked the question that changed my week: “Nice. But how do I easily know what I should block?”

It was the right question, and I did not have a good answer. Gateway was perfectly placed to stop the download. It just had no way to know what to stop. The event-stream incident is years old. Malicious packages keep arriving: typosquats, hijacked maintainer accounts, a compromised release of a library you have used for ten years. Every one of them is a package name that nobody on your team had heard of until someone else reported it.

That is the gap I hear about from security teams. They do not lack a place to enforce. They lack a way to turn “the community just found out this package is bad” into “our developers cannot download it”, without a person in the middle copying names into a policy list. That question is the reason this post exists.

Policies are great at decisions you can write down

Gateway policies excel when the decision is something you can state: block this ecosystem, block that package, only allow our internal registry, deny this category for that group. Those are rules, and rules belong in policies.

Malicious packages are a different shape of problem. They are a stream. OSV.dev, the open vulnerability database, publishes new records every day, and the index I built holds about 278,000 of them across six ecosystems. The decision I wanted is not a rule you write once. It is a lookup against data that keeps moving, with a little judgment on top:

  • block what is malware,
  • only log what is a vulnerability,
  • and keep up with the feed without anyone editing anything.

You cannot write that as an expression. You can write it as a function.

One Forward rule instead of 250,000 policies

Gateway Forward: your code on the traffic Gateway already inspects

This is the part I find genuinely exciting. A Gateway HTTP policy with the Forward action sends matching, permitted traffic to a Worker in your account. Gateway keeps all the hard parts: the proxy connection, TLS decryption, upstream routing. Your Worker gets a standard Request, plus context about the connection and the user (identity, device posture), and then decides what happens:

  • pass the request through with next(request),
  • modify the request or the response,
  • respond itself, without the request ever reaching the destination,
  • or start background work after the response, like a notification or a write to storage.

That is the entire contract. A simplified version of what PkgGuard does looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
export class GatewayMiddleware extends WorkerEntrypoint {
  async handle(request, context, next) {
    const pkg = parsePackageUrl(request.url);       // npm, PyPI, Cargo...
    const verdict = pkg && (await lookup(pkg));     // local index, OSV on a miss
    if (verdict === "malware") {
      return new Response("Blocked: known-malicious package", { status: 403 });
    }
    return next(request);                           // otherwise, carry on
  }
}

Where the Forward action sits in Gateway

Two things make this more powerful than it looks.

Gateway does the part you would otherwise have to build. To enforce a decision on developer traffic, you normally need a proxy fleet, TLS interception, device enrollment and an identity source. With Forward, all of that is already there. The Worker simply inherits it, including who is on the other end of the request. My alerts say which user downloaded which package because Gateway told the Worker.

One rule carries many decisions. A static policy encodes one decision. A Forward rule encodes a function. PkgGuard has one Gateway rule, scoped to named identities, and the thousands of decisions behind it live in code and data where they are easy to test, version and update. That is also why it scales: adding the sixth ecosystem did not add a rule.

And the order of operations keeps you safe. HTTP request policies run first, so a Block policy you already have still wins. Forward only handles what you scoped it to and what the rest of Gateway permitted.

Why OSV.dev is a good candidate

A function needs a data source, and this is where OSV.dev earned its place:

  • One schema, many ecosystems. OSV aggregates advisories from the GitHub Advisory Database, the PyPI Advisory Database, the Go Vulnerability Database, RustSec and the OpenSSF Malicious Packages project, among others, in one format. One parser serves six ecosystems.
  • Malware is labeled. Malicious packages carry MAL- identifiers, so “is this malware?” has an answer in the data instead of in a heuristic.
  • Version ranges are machine-readable. Each record says which versions are affected, in the ecosystem’s own versioning rules. That matters: 1.10 and 1.9 do not compare the same way in every language.
  • It is easy to mirror. There are bulk dumps per ecosystem for the first load, a “what changed” feed for daily updates, and a live query API for anything the index does not know yet.
  • It is open. The data is free to use under its sources’ open licences (mostly CC-BY, CC0 and Apache 2.0), and a community keeps feeding it.

One lesson from the data is worth sharing. The MAL- prefix is not enough on its own. event-stream, the example everyone remembers, is a reviewed GitHub advisory with a GHSA- identifier, and it never received a MAL- one. PkgGuard also treats the malicious-code weakness classes (CWE-506, 507, 510, 511 and 912) as malware. That second rule is what catches it, and it is the first test I would write again.

The flow

PkgGuard components and data flow

Here is what happens when a developer runs npm install:

  1. The Cloudflare One client sends the download through Gateway, which decrypts it and recognizes a package download.
  2. The Forward rule matches the user and the traffic, and Gateway calls the Worker.
  3. The Worker parses the ecosystem, the name and the version, and checks its index: a KV store rebuilt from OSV. Only on a miss does it query OSV live, with a short timeout.
  4. Malware gets a 403 and a short reference code such as PG-7Q2K4M. Everything else continues with next(request).
  5. Off the request path, the decision is written to a database, and malware triggers a chat alert.

A second Worker, behind Cloudflare Access, serves the dashboard and refreshes the index from OSV every day. The download path never waits on it, and if the filter itself hits an internal error, it lets the download through rather than break someone’s build.

Block malware, log vulnerabilities

I made one policy decision early that shaped everything else: vulnerabilities are logged, never blocked.

If you block every package with a known CVE, you will break most real builds. Developers will notice within a day, the exception requests will pile up, and the control gets switched off. A control that gets switched off protects nothing. So the filter blocks what is malicious, and records everything else for the people whose job is to patch.

That distinction is only possible because the decision lives in code. Gateway’s selectors tell you what was downloaded. The Worker adds what we know about it.

What it looks like

PkgGuard dashboard, Detections tab

The dashboard is where it becomes visible to a security team: what was blocked, what was only flagged, for whom, and which advisory it matched. The developer sees a 403 and, depending on the tool, a short explanation with the reference code. They quote it, and the team finds the event with one search.

I rolled it out the way I would advise any customer to. First observe-only: the Worker records what it would block and blocks nothing, so you can read real detections before anyone is affected. Then enforcement, one ecosystem at a time. At any point, disabling the Forward rule restores normal downloads within seconds, because a control you cannot switch off is a control people are afraid to switch on.

What else could you run here?

Package security happens to be a clean example, because the decision needs live data that Gateway does not have. But the pattern is much wider. Anything where “allow or block” is not the whole answer fits:

  • Coach instead of block. Return a custom page that explains the policy and where to ask for an exception.
  • Redact on the way out. Rewrite fields in JSON traffic before they leave.
  • Watermark or capture. Tag downloads with the user, or store a copy for review.
  • Ask your own system. Call an internal API or a ticketing system before letting a request through.
  • Notify the right person. Push a message when a specific user touches a specific service.

Cloudflare publishes Worker templates for several of these, and they are a good place to start. If you can describe the decision in code, you can run it on traffic Gateway already inspects. The “custom logic” is as open as a Worker is.

Know what you’re getting

I would rather you hear the limits from me than discover them later.

  • It blocks known-malicious packages. It is not a vulnerability scanner. CVEs are logged, never blocked, by design.
  • It only sees downloads that cross the network. A package already sitting in a developer’s local cache installs without a new request, so it is neither checked nor recorded.
  • Gateway has to decrypt the registry traffic, and the device needs an identity. A registry covered by a Do Not Inspect rule is silently out of scope, so the two scopes must match. An empty dashboard can look like a clean one.
  • Gateway only forwards what it classifies. For example, NuGet package names with look-alike characters never reach the Worker, and most NuGet malware uses them. Measure what your Worker actually sees.
  • Registries clean up quickly. For several ecosystems, most malicious packages are already gone when a block would matter. NuGet is where blocking pays off most. For others, the value is mostly the vulnerability log, and protection against mirrors that still serve what the public registry dropped.
  • Clients differ. npm prints the reason, pip prints only 403 Forbidden, and Go sends every blocked download twice. The explanation reaches developers through the alert and the dashboard when the tool will not show it.
  • Forward is a public beta and it fails closed. A Worker error blocks the request rather than bypassing the Worker, and it does not see CONNECT tunnels or WebSocket traffic. Workers pricing applies.

None of that changes the main point. It tells you where to test.

Try it

If you already use Gateway with the Cloudflare One client, the Forward action is a small step from where you are. Start with a Worker that only adds a response header, then scope a rule to one test user, and build from there.


Questions or feedback?

Feel free to reach out via email or connect on LinkedIn . I’m always happy to talk about Gateway, Workers, or what you would put in a Forward rule.


Thanks to the OSV.dev and OpenSSF communities for making this data open.