PAC files and Java applets and "Expected ;" errors

I was recently asked to look at an issue one of my clients was facing with their proxy auto-configuration (PAC) file, throwing strange compilation errors, usually saying things like “Expected ‘;’”

Bizarrely, this was only appearing on certain sites. Debugging work showed it was only appearing on sites using Java applets. After a bit of testing and bug searching, I worked out that Java is actually a lot pickier about its PAC file syntax than IE or Firefox were. For example, the following code works in IE:

if // Define if you are in a subnet  
(isInNet(myIpAddress(), "10.130.0.0", "255.255.0.0"))
// Send you to the <site> proxy
return "PROXY proxy.example.com.au:8080;DIRECT";  

But throws the shown error when Java applets load. The fix was to use “strict” syntax rules, meaning the same code would become:

if (  
  // Define if you are in a Subnet
  isInNet(myIpAddress(), "10.130.0.0", "255.255.0.0")
) {
  // Send you to the <site> proxy
  return "PROXY proxy.example.com.au:8080; DIRECT";
}

Note the extra parentheses () and braces {}. If you want to test if you are being affected by this issue, fire up tcpdump/Wireshark and watch the requests fly as Java loads. If they’re heading to the proxy when they shouldn’t, or vice-versa, you’re probably running into this and you should check your syntax carefully.

Want an easy way to check your syntax? Remember that its just JavaScript, so you can use a syntax-aware editor (like Geany), or run it through JSLint if you’re feeling brave.