How to optimize and improve a proxy.pac file

How do you optimize or improve a proxy.pac file? Make it free of errors in every engine, safe when something breaks, fast, easy for the next person to read, and no longer than it needs to be. This checklist is sorted by those five goals. Each point is short and links to the page or rule that explains it in full.

The opposite view, the mistakes that most often break routing, is on the page with the 20 most common PAC and wpad.dat mistakes. If you are new to PAC files, start with what a PAC file is. A complete file that follows every point below is the annotated reference proxy.pac.

How do I make the file free of errors?

A PAC file that fails does not show an error to the user. The browser simply connects directly, so mistakes stay hidden.

How do I make the file safe?

“Fail open” means that an error lets traffic out without the proxy. These points keep the file from failing open and keep look-alike names out.

  • End the file with an unconditional default route as the last statement.
  • Use a second proxy as fallback, not DIRECT. "PROXY p1:8080; DIRECT" sends everything directly while the proxy is down. If a direct fallback is intended, say so in a comment. The rules on a missing second proxy and on DIRECT as fallback explain both cases.
  • Do not use a try/catch that returns DIRECT, because it turns every error into a bypass.
  • Match domains with a leading dot: write dnsDomainIs(host, ".corp.example") and *.corp.example, never corp.example without the dot or corp.*. Without the dot, dnsDomainIs and a leading * match look-alike domains, and a trailing * matches any ending an attacker chooses.
  • Match IP ranges with isInNet on IP addresses, not with text prefixes such as 10.*.
  • Serve the file over HTTPS with the type application/x-ns-proxy-autoconfig and status 200. Over plain HTTP, anyone on the network path can rewrite it. WPAD discovery is always plain HTTP; the WPAD page explains the risks and how to reduce them.
  • Block direct internet access at the firewall. A client can ignore the PAC file, so it is not a security control on its own. With a firewall that only lets clients out through the proxy, a broken PAC file shows up as an error instead of as uncontrolled access. Whether a PAC file is secure at all, and where it is still the right tool, is explained on the page should you still use a PAC file.

How do I make the file fast?

How do I make the file easy to read and support?

  • Put a version and an owner in the first comment lines. When a user reports a problem, you can then ask which version their browser has.
  • Comment the default route, because it decides every request that no rule matched (why it needs a comment).
  • Keep the proxy string in one place, so that a change of proxy is one edit.
  • Use braces on every if.
  • Keep the file in version control and change it through review, like code.

How do I keep only the entries the file needs?

  • List your real short names instead of sending every name without a dot direct with isPlainHostName. Then a mistyped public name reaches the proxy and its logs.
  • Remove conditions that repeat.
  • Give each exception a comment with its reason, so the next person can tell whether it is still needed. Exceptions without a reason are never removed.
  • Keep the file small and flat. A file with hundreds of rules or deep nesting is slow and hard to review.

Check the file against this list

Paste the file into the PAC file tester. It runs the file in the engine profile you choose (Chromium, Firefox, pacparser, or one of the two Windows profiles, WinHTTP and WinINet) and grades it against the rule catalogue; a file that follows every point on this page gets an A+. Paste the output of curl -sI https://pac.corp.example/proxy.pac next to the file to grade how it is served. To start a new file instead, use the PAC file generator or one of the PAC file examples.

Frequently asked questions

How do I optimize a proxy.pac file?

Write it in plain ES5 JavaScript and end it with an unconditional default route to two proxies, so it runs without errors everywhere and does not fail open. Decide by the host name and call isInNet only on IP addresses, so DNS does not slow it down. Lower-case the host once, match domains with a leading dot, comment the default route, keep the proxy string in one place, and remove entries nobody needs. Then paste it into the tester at findproxyforurl.net/check/, which grades it against every point.

Are the best practices for wpad.dat different from those for proxy.pac?

The file itself follows the same rules, because a wpad.dat is a PAC file. Only the delivery differs. WPAD discovery fetches the file over plain HTTP from a name that anyone on the network path may be able to answer, so own every wpad name in your DNS suffixes or, better, configure the PAC URL explicitly.

Should a PAC file end with DIRECT as a fallback?

Only if uninspected traffic during a proxy outage is acceptable. A second proxy as fallback keeps traffic inspected. If you do keep DIRECT, say so in a comment so the next person knows it is intended.

May I write an IP address with leading zeros or in hex in a PAC file?

Do not. 010.1.2.3, 0x0a.1.2.3 and 167838211 are read differently by the browsers, by pactester and by Windows; a leading zero can mean octal, so 010 is 8. Even where it works, the form is confusing and leads to mistakes. Write four plain decimal numbers such as 10.1.2.3.

Last updated: