What is a restricted JavaScript object?

I’m trying to use custom DOM events to communicate between the main world and the isolated world for a page in a webextension in Firefox. When trying to access an event object property, I get an access denied error. Most of what follows is just context. I really want to know what a “restricted” object is rather than how to make it unrestricted.

listener

function onCustom( e ) {
  console.log( e );
  console.log( e.detail );
  console.log( e.detail.plugh );
}
document.addEventListener( 'custom', onCustom );

sender

e = new CustomEvent( 'custom', { detail: {
  plugh: 'xyzzy'
} } );

document.dispatchEvent( e );

If the listener and the sender are both in the same world, it works fine. (This case is just a sanity check, since the point is to communicate between worlds.)

If the listener is in the isolated world and the sender is in the main world, it works fine.

If the listener is in the main world and the sender is in the isolated world, it pukes as follows.

console output

custom { blah, blah, blah, lots of properties }
Object { plugh: "xyzzy" }
Uncaught Error: Permission denied to access property "plugh"

That’s all the error information that there is: an untyped error and a message string.

I can expand the Object (second line) in the devtools console. It’s there.

If I expand the “custom” object (first line) in the devtools console, it says (in part):

  detail: Restricted {  }

The detail property of a CustomEvent can be anything serializable. I can do my own serialization with JSON.stringify() in the sender and deserialize it with JSON.parse() in the listener. It’s a workaround that does work. I still want to know what’s going on with the object that I cannot access it after it was serialized and deserialized transparently for me.

EDIT (from the comments): The documented way to send objects from the isolated world into the main world is cloneInto().

sender (isolated to main only)

e = new CustomEvent( 'custom', cloneInto( { detail: {
  plugh: 'xyzzy'
} }, wrappedJSObject ) );

document.dispatchEvent( e );

15

This answer represents what I’ve learned today from (what I can remember of) a deleted answer from another poster plus the comments.

Because it’s structured data, when sending { detail: { plugh: "xyzzy" } } in an event, the browser makes a structured clone. A common example of structured cloning is passing objects back and forth between realms with web workers. The question doesn’t deal with web workers, but the main and isolated worlds of the content context are different realms, which have different window objects. (Having different window objects is part of how the isolation of “isolated world” works, even though the main and isolated worlds share the DOM and, hence, DOM events. That part I already knew.)

When Firefox serializes the detail object, the object (or at least members of it) retains the characterization of belonging to the isolated world. (I would have thought serialization discards it, but it doesn’t.) In the main world, the deserialized object still belongs to the isolated world. Because the isolated world has a higher privilege level, the deserialized object is inaccessible (“restricted”) in the main world.

Explicitly serializing the object with JSON.stringify() does discard the realm information, so the object can be deserialized without the origin realm information (and it works). Primitives, like strings, apparently don’t have realm information. Mozilla has another solution for content scripts, however.

cloneInto() sets the realm information to whatever else is specified, usng the window of the target realm. Since wrappedJSObject is a reference in the isolated world to the window object in the main world, detail can be characterized as belonging to the main world before serialization. When it’s deserialized, it still belongs to the main world, so it is accessible (not “restricted”) in the main world.

TL;DR: Serialization and deserialization across the main/isolated world boundary retains world information for complex data. Objects belonging to the higher-privileged isolated world are restricted from access in the lower-privileged main world.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật