473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is a closure's scope accessible by untrusted code?

Is the scope of a closure accessible after it's been created? Is it
safe against XSS to use closures to store "private" auth tokens? In
particular, in...
function closure(token) {
return function () {
// code which uses token to authenticate
}
}

foo = closure(secret);
delete secret;

// untrusted code
....can untrusted code access 'secret', or only the code inside foo?

- Andrey
Oct 24 '08 #1
2 1811
On Oct 24, 1:57 pm, Andrey Fedorov wrote:
Is the scope of a closure accessible after it's been created?
The scope is handled internally by the implementation; you probably
mean 'are the objects on the scope chain accessible after it has been
created', or at least those not added to a scope chain with a - with -
statement or the global object (the latter always being accessible).
In pure ECMAScript terms the answer is no. However, in javascript
terms the answer would be 'don't rely on that', as implementations
have provided mechanisms for setting/re-establishing scopes for
function calls. See:-

<URL: http://peter.michaux.ca/article/8069 >
Is it safe against XSS to use closures to store "private"
auth tokens?
Almost certainly not. Not necessarily for any reasons related to
closures but rather questions like how these "auth tokens" got to the
javascript code without being exposed. (If they are in the page source
or the source of an (even dynamically generated) JS file how can you
be certain that the text cannot be retrieved from the DOM, and if XML
HTTP requested how can you be certain that the XSS script did not wrap
the XML HTTP request object, etc.?)
In particular, in...
>function closure(token) {
return function () {
// code which uses token to authenticate
}
}
>foo = closure(secret);
delete secret;
>// untrusted code

...can untrusted code access 'secret', or only the code inside foo?
Even if it cannot does it need to? The - foo - value is exposed and
can be modified, and its source code extracted to form the basis for
that modification.

Richard.
Oct 24 '08 #2
Thanks for the thorough response -
how can you be certain that the XSS script did not wrap
the XML HTTP request object
By making sure all user-submitted data are rendered only after the
closures which use auth-token have been created.

Thanks also for the link to Peter's blog, I was looking for that - the
issue appears to have been closed in in FF >= 3.1. I agree that this
isn't a solid solution, but might provide at least somewhat of a
barrier against some attacks.

Cheers,
Andrey
Oct 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Benjamin | last post by:
Here are some more ideas for how to implement a statement-friendly code block syntax in Python. Hopefully more "Pythonic" (that is, of or pertaining to those features noticably reminiscent of...
27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
4
by: LAN MIND | last post by:
?
2
by: jman | last post by:
for ( var i = 0; i < div.firstChild.childNodes.length; ++i ) { var marker = new Object(); marker.iii = i; sys.addListener( marker, "click", function() { alert(marker.iii); }); }
4
dmjpro
by: dmjpro | last post by:
Today i looked at JavaScript closures. There i came across a thing closure comes in when an inner function defined inside an outer function. So when outer function exits and returns the inner...
4
by: JavascriptProgrammer | last post by:
In the following code: ----------------------- function get() { return function() { alert(x); } }; function foo(s) { var x = s; this.getX = get();
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.