views:

229

answers:

4

Hi experts,,,

the two ways that I know to get the page url are: 1.through the applet class: Applet.getDocumentBase()

2.through the netscape javascript library: JSObject.getWindow(this).eval("location.href")

First, what are the differences between those two methods and advantages of each,

Secondly, can users trick the url of those methods in someway? to make the applet think it's embeded in http://www.stackoverflow.com/index.html for example?

+3  A: 

If you look at the code for Applet.getDocumentBase() (sun's implementation of AppletStub), you'll see that it is also getting the location using JSObject. But it has a lot of additional code around it, and is guaranteed to work across all browsers and setups. So better use Applet.getDocumentBase()

Bozho
does this mean that the JSObject is included in the JDK? in other words no need for me to ship it with my applet package?
Bassel Alkhateeb
part of plugin.jar - so yes, included.
Bozho
+1 Vendor-recommended ways are preferred above other ways.
BalusC
A: 

JSObject is buggy in some browsers (IE and Safari especially). Applet.getDocumentBase() is a lot safer.

Josh Yeager
but Applet.getDocumentBase() uses JSObject in the background to achieve that according to Bozho
Bassel Alkhateeb
+2  A: 

The answer is easier than you think.

You should use Applet.getDocumentBase in case the implementation varies between platforms or JVMs, or is altered at a later date. Just because JSObject is the way that Sun's JVM gets this value doesn't mean that, say, IBM's JVM works that way.

R. Bemrose
+2  A: 

Pretty sure users could spoof it:

  1. Download your .jar and host on a server controlled by the user
  2. Change their etc/hosts file so that stackoverflow.com points to the IP of their server
  3. Configure the server to serve requests for stackoverflow.com
  4. Visit the page on the server with the applet embedded

And the applet will believe it's on stackoverflow.com. There is probably a simpler way to spoof it though!

ZoFreX
Exactly. there isn't a sure way to prevent spoofing.
Pool
What about getCodeBase(), can that be spoofed too?I mean, in the example you mentioned.. will Applet.getCodeBase() return stackoverflow.com ?
Bassel Alkhateeb
Yes, it certainly could. What in particular are you worried about? Someone stealing and rehosting your applet?
Pool
As The Feast said, to really answer this for we need to know why you're worrying about spoofing?
ZoFreX