News for hackers. Stuff that matters.
Re:All About E: The Language That Infiltrated JavaScript by goto11 on Thursday October 27, 2022 @02:07AM (#33353776)
I'm unclear about what kind of threat "freeze" protects against. Is it just protecting against your own mistakes, or against malicious code somehow running in the same JavaScript process?
Re:All About E: The Language That Infiltrated JavaScript by etc_passwd on Thursday October 27, 2022 @02:18AM (#33353849)
the ability to create and send an object that cannot be modified
Re:All About E: The Language That Infiltrated JavaScript by beebeepka on Thursday October 27, 2022 @02:51AM (#33354092)
I think Object.freeze is insufficient but there is value in immutable properties of objects. I wish TS understood this
Re:All About E: The Language That Infiltrated JavaScript by kragen on Thursday October 27, 2022 @02:51AM (#33354098)
Malicious code running in the same JS process, specifically malicious code that modifies an object you give it (that you retain references to) or that gives you an object and then changes it later. Running multiple mutually untrusting pieces of code in the same process was the main point of E, and that's what the modifications to JS being discussed here are aimed at.

The reason for such "frozen" objects is explained in http://www.erights.org/elang/kernel/auditors/index.html.

Re:All About E: The Language That Infiltrated JavaScript by goto11 on Thursday October 27, 2022 @02:37PM (#33361996)
Interesting! But I have a hard time understanding the use case in the context of a web page. If you import malicious code in a web app, wouldn't it be able to wreck all kinds of havoc anyway?
Re:All About E: The Language That Infiltrated JavaScript by kragen on Thursday October 27, 2022 @03:49PM (#33363052)
The objective of Jessie and the other capability-secure dialects of JS is to allow you to import malicious code in a web app without allowing it to either wreak or wreck any havoc. You can imagine lots of ways this could be useful.
Re:All About E: The Language That Infiltrated JavaScript by goto11 on Thursday October 27, 2022 @05:13PM (#33364121)
I guess I have a hard time seeing how Object.freeze() protects against malicious code, when any malicious script can still access the global object? I can understand something like the "Shadow realms" proposal, where a modules access to global objects can be constrained.
Re:All About E: The Language That Infiltrated JavaScript by kragen on Thursday October 27, 2022 @06:36PM (#33364928)
In things like Jessie or ADsafe, confined scripts are screened to ensure that they do not access the global object.
Re:All About E: The Language That Infiltrated JavaScript by goto11 on Friday October 28, 2022 @09:55AM (#33371428)
So am I correct in the understanding that Object.freeze in JavaScript does not provide any useful protection against malicious code? But in a different language, or a hypothetical future version of JavaScript, it might play a role in such a protection?
Re:All About E: The Language That Infiltrated JavaScript by kragen on Friday October 28, 2022 @12:45PM (#33373893)
I think it provides, at best, very limited useful protection against malicious code that has access to the whole JS language, but it means that Jessie can include enormously more of JS than ADsafe did.
Re:All About E: The Language That Infiltrated JavaScript by cztomsik on Friday October 28, 2022 @10:04AM (#33371528)
ShadowRealm + Object.freeze() could be enough to create safe env for untrusted code.
Re:All About E: The Language That Infiltrated JavaScript by vanderZwan on Thursday October 27, 2022 @06:55AM (#33355764)
So first of all the article is a bit confusing: Object.freeze does make an object immutable:

    const t = Object.freeze({foo: "bar"});
    // throws an error in strict mode,
    // silently does nothing otherwise
    t.foo = "baz";
    // prints "bar"
    console.log(t.foo);
The tricky part is that it does not do so recursively:

    const t2 = Object.freeze({foo: {bar: "baz"}}); 
    // bar is still mutable
    t2.foo.bar = "oh no!";
    // prints "oh no!"
    console.log(t2.foo.bar);
Still, one can easily write a function to recursively do a depth-first walk on an object and make everything immutable, so this is not really a concern in practice (if anything it gives you more flexibility).

Anyway, what I think Miller meant is that immutability is not the main reason why they introduced Object.freeze.

If I understand correctly, the main idea is to prevent people from inserting malicious code in the prototype chain (or anywhere else they might be able to).

Because JavaScript is prototype based I could in theory create a new Array class that uses the original Array class as its own prototype, then replace the Array class with itself. If you then create a new array it would still work like a regular one - it just goes up the prototype chain after all.

The malicious part is that this custom array class could then also override all Array methods to secretly watch your data and send stuff to the web without you noticing, or do anything else really. Because, again, it can just call the original method of its prototype to also behave as normal without anyone noticing.

Object.freeze would let you make sure none of the fundamental JavaScript objects can be tampered with.

The other thing is that you can ensure that any the library you create cannot be tampered with by third-party libraries.

Re:All About E: The Language That Infiltrated JavaScript by bluetech on Thursday October 27, 2022 @09:20AM (#33357071)
> Object.freeze does make an object immutable

If you look at the Hardened Javascript example:

    const makeCounter = () => {
      let count = 0;
      return harden({
        incr: () => (count += 1),
        decr: () => (count -= 1),
      });
    };
The "counter object" here is mutable. I think that's what they mean.
Re:All About E: The Language That Infiltrated JavaScript by vanderZwan on Thursday October 27, 2022 @09:38AM (#33357291)
Well yeah, it's closing over a variable that is not actually part of the properties of the function objects that are returned.

Object.freeze makes the shape (and properties) of an object immutable. It makes no guarantees about variables being closed over by a function. Those are different semantics altogether.

That feels similar to my earlier example of properties of nested objects not being affected by Object.freeze. And in this case, being able to freeze closed variables would in a sense mean exposing those variables. Which is not how closed variables work in JS (in fact it's one of the best ways to mimic "private properties" - except they're not really properties).

Re:All About E: The Language That Infiltrated JavaScript by afiori on Thursday October 27, 2022 @09:29AM (#33357178)
There are two types of immutability in JS: one is rebinding a variable (forbidden by the const keyword) and the other is modifying an object (forbidden by freeze)

In javascript these are similar as environment closures behave sort of similar to prototype chains.

Re:All About E: The Language That Infiltrated JavaScript by cromwellian on Thursday October 27, 2022 @11:36AM (#33359195)
One way to work around this is to serialize the argument to harden back to source, eval() it and then harden, which will prevent the captures from working.
Re:All About E: The Language That Infiltrated JavaScript by _glass on Thursday October 27, 2022 @09:42AM (#33357354)
BTW Deep Freeze is part of the Mozilla documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...