Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

A webhook address passes our check, then we resolve the name again, because a name can answer twice

A customer can register an https address and we will POST a signed event to it when a candidate starts or finishes. That is a feature that makes our servers connect to somewhere a user chose, which is the textbook way to

A customer can register an https address and we will POST a signed event to it when a candidate starts or finishes. That is a feature that makes our servers connect to somewhere a user chose, which is the textbook way to end up reading your own cloud metadata service.

The behaviour is documented on the developers page if you want to see what a customer sees. This is what sits behind it.

The cheap check comes first and says why

An address is accepted only if it is https, has no user name or password, uses the standard port, is a domain name rather than an IP address, and is not a name that only means something inside a private network. Each failure has its own sentence, because the person typing it is a customer, not an attacker:

if (url.protocol !== 'https:') return { ok: false, error: 'The address must start with https://.' };
if (url.port) return { ok: false, error: `Use the standard https port. Take :${url.port} out of the address.` };
if (hostname.startsWith('[') || isIP(hostname) !== 0) return { ok: false, error: 'Use a domain name, not an IP address.' };

One small thing worth knowing: the URL parser drops :443 from an https address, so any port still present is a different one. There is no need to compare against 443 yourself.

A good name can point at a bad address

The string check is not enough, because anybody can make hooks.example.com resolve to 10.0.0.5. So before every send we resolve the name and put every address it returned through a second check. One private address among them is enough to refuse the name, since we cannot know which one a connection would pick.

That second check is where the time went. It handles the ordinary private ranges, and then the ways of writing an IPv4 address inside an IPv6 one:

  • ::ffff:10.0.0.1, a mapped address
  • 64:ff9b::/96, reached through NAT64
  • 2002::/16, reached through 6to4
  • the old IPv4-compatible form under ::/96

Each of those is parsed back to the IPv4 address it wraps and judged as that. Outside 2000::/3 everything is refused, which removes unique-local, link-local and multicast in one line. Anything the parser cannot read is refused too. The failure direction matters: an address we cannot understand is not one we connect to.

The second lookup is the attack

Here is the part that is easy to get wrong. Suppose you resolve the name, check the addresses, and then call fetch(url). fetch resolves the name again by itself. A name can be set up to answer with a public address the first time and a private one the second, with a very short TTL. Your check passed on one answer and your connection used another.

So we do not use fetch for this. We use Node's https.request and replace its lookup with the addresses we already checked:

lookup: (_hostname, options, callback) => {
  if (options.all) callback(null, addresses);
  else callback(null, addresses[0].address, addresses[0].family);
},

The connection goes to the addresses we judged, and the certificate is still verified against the name, so TLS is not weakened by it. The callback has two shapes because Node asks for every address when it tries IPv6 and IPv4 side by side, and for one otherwise. Handling only one of them works in testing and breaks on someone's dual-stack host.

Three smaller decisions

Redirects are failures. https.request does not follow them, and we kept it that way. A 3xx would send us to a location nobody checked. The delivery list tells the owner "Your server answered 301, a redirect, which we do not follow", which is a sentence a person can act on.

We never read the answer. Only the status code matters, so the response is destroyed as soon as it arrives. None of the body is read or kept, which also means a hostile server cannot make us buffer anything.

It is checked again on every send. An address that was fine when it was added is validated again each time, because the rules may have been tightened since.

Try it

You can read how a receiver should verify these requests on the developers page, and the security page describes the rest of how we treat customer data.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.