Google appengine + applet AccessControlException

I am having a problem getting my applet to connect back to a google appengine servlet. The HttpURLConnection always fails with:
java.security.AccessControlException: access denied (java.net.SocketPermission 72.14.213.141 resolve)

I have tried a bunch of stuff to get it working (can’t even connect to the applet getCodebase() URL) and I’m out of ideas. One thing that is interesting is the exception has an IP address in it rather than the host name, is google doing some funny redirect that is confusing the applet or have I done something stupid?

Start with plain socket access:


  new Socket(Applet.getDocumentBase().getHost(), 80);

and check whether that works.

Nope that doesn’t work either.

You have to use URLConnections; (NB fragmentary code only - omitted error trapping &c for clarity)


String data="Hello Servlet!";
url = new URL(codeBase+appName);
urlConnection = url.openConnection();
// inform servlet that this is a service request
urlConnection.setRequestProperty("Content-Type", "application/service");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// disable caching
urlConnection.setDefaultUseCaches(false);
urlConnection.setUseCaches(false);
outputStream = urlConnection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();

// get response
inputStream = urlConnection.getInputStream();
&c.

Why would a URLConnection work and an HttpURLConnection not work?

There is nothing magical about URLConnection. It creates a Socket under the hood.

So the security manager complains about: “72.14.213.141”

Could you check the hostname in the addressbar of your browser and see what it IP it resolves to? (ping it). Please make sure you use exactly the same (sub)domain.

Yeah, that is what I don’t get. The URL is constructed with a hostname / path and the security manager is complaining about an IP address. Google uses load balancing so I assume each request could go to a different IP address.

Visiting again I get error with :
74.125.127.141
Pinging the hostname gives me the same IP.

How does the applet sandbox check addresses?

The sandbox is pretty silly actually. It checks the IP address only. It grabs the IP from the browser, through the plugin, and compares any domainname/IP you try to connect to, to that IP. If it’s not the same IP, an exception is thrown.

The should go to the same load-balancing machine first though, even if that routes it to a different box on the backend.

Are you connecting back to the exact same host, or is it a subdomain? I know that in theory the sandbox should allow applets served from “host.com” to connect to subdomains “appengine.host.com”, but in practice the security manager doesn’t like that. :-\

Actually, the rules are quite clear: it’s all IP based. If the subdomain has the same IP as the domain, you can connect to it.

I am using a subdomain of appspot.com (the google appengine host) and if I ping the mysubdomain.appspot.com I get the same IP address as it is complaining about in the AccessControlException. What gives?

Why would

new Socket(Applet.getDocumentBase().getHost(), 80);

ever fail?


String host=Applet.getDocumentBase().getHost();
System.out.println(host);

InetAddress addr=InetAddress.getByName(host);
System.out.println(addr);
System.out.println(addr.getHostAddress());

What is printed?

synthpatches.appspot.com
synthpatches.appspot.com/74.125.127.141
74.125.127.141
java.security.AccessControlException: access denied (java.net.SocketPermission 74.125.127.141:80 connect,resolve)

Any ideas?

Are the JAR files hosted on the same IP?


MyApplet.this.getClass().getProtectionDomain().getCodeSource().getLocation();

The jar is located at the same IP.

Can’t getProtectionDomain() from applet sandbox.
java.security.AccessControlException: access denied (java.lang.RuntimePermission getProtectionDomain)

Please read this thread and try to see whether that works:
http://www.java-gaming.org/index.php/topic,18652.0.html

Especially:
http://www.java-gaming.org/index.php/topic,18652.msg146559.html#msg146559

Yay, got it working! Just not quite sure how. Did about a million things (including operating system upgrade) cleaned every cache I could find, re-deployed everything and all of a sudden it works. It would be really nice if those AccessControlExceptions were a bit more specific (telling me the IP it is expecting to match for example).

Thanks for you help Riven.

Please also apply my suggested nonsense-fix in that thread.

There will be a lot more people with that ACE in your applet, if you don’t.

Will do, I did make a few other changes to the applet that may have fixed this because of that issue. I moved some code from the Applet constructor into init() and also changed the URL constructor to use the 3-argument protocol,server,file. The applet/server networking seems very brittle and is a nightmare to debug also there is precious little documentation about it on the web.