Skip to content

Make StructuredClone Taint Aware#275

Draft
leeN wants to merge 3 commits into
SAP:main-gecko-devfrom
leeN:structured_clone_tainting
Draft

Make StructuredClone Taint Aware#275
leeN wants to merge 3 commits into
SAP:main-gecko-devfrom
leeN:structured_clone_tainting

Conversation

@leeN
Copy link
Copy Markdown
Collaborator

@leeN leeN commented Mar 21, 2025

This change adds taint support to the StructuredClone machinery.

Before, when directly invoking window.structuredClone() to copy an object or indirectly, by sending a tainted value via postMessage, the taint was lost.

This adds support to StructuredClone to send tainted values. It works as follows:

Based on the TaintSpew code I extracted a generic method to serialize StringTaint objects as JSON. When a tainted String is getting cloned, we detect this and instead send a Tainted String, which essentially attaches a second string, containing the serialized taint object. On the receiver side, we detect this, deserialize the JSON to a StringTaint (the majority of this PR actually) and attach it to the returned string.

IN JS shell this can be tested as follows:

o = serialize(String.tainted("foo") + " - " + String.tainted("bar"));
o2 = deserialize(o);
o2.taint
o3 = serialize({k: String.tainted("bar"), kkk: "value"});
o4 = deserialize(o3);
o4.k.taint

@leeN leeN added the enhancement New feature or request label Mar 21, 2025
@leeN leeN requested a review from tmbrbr March 21, 2025 13:12
@leeN leeN marked this pull request as draft March 21, 2025 13:12
@leeN leeN self-assigned this Mar 21, 2025
Comment thread js/src/jstaint.cpp
return printer.release(cx);
}

void JS::WriteTaintToJSON(const StringTaint& taint, js::JSONPrinter& json) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function duplicated from the TaintSpew code? If so could we re-use it here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has some changes, but I can unify them to avoid duplication!

@leeN
Copy link
Copy Markdown
Collaborator Author

leeN commented Mar 25, 2025

Hmmm, there are some mochtests failing, but those seem to fail on the main branch as well.

When logging the taints that get cloned, I noticed we taint a ton of stuff in the browser chrome. This makes sense, as the chrome is effectively HTML+JS as well, but this is something we might want to discuss.

It eats some performance, as creating TaintOperations is fairly costly, and it is unclear to me whether these are flows we are interested in. I will investigate how to detect this and maybe make it configurable. I.e., have a flag to disable taint introduction when running inside the browser's chrome.

@leeN leeN force-pushed the structured_clone_tainting branch from 79132fd to 9b699b9 Compare March 26, 2025 08:30
leeN added 3 commits July 30, 2025 11:58
This change adds taint support to the StructuredClone machinery.

Before, when directly invoking `window.structuredClone()` to copy an object or indirectly, by sending a tainted value via postMessage, the taint was lost.

This adds support to StructuredClone to send tainted values. It works as follows:

Based on the TaintSpew code I extracted a generic method to serialize `StringTaint` objects as JSON. When a tainted String is getting cloned, we detect this and instead send a Tainted String, which essentially attaches a second string, containing the serialized taint object.
On the receiver side, we detect this, deserialize the JSON to a `StringTaint` (the majority of this PR actually) and attach it to the returned string.

IN JS shell this can be tested as follows:

```javascript
o = serialize(String.tainted("foo") + " - " + String.tainted("bar"));
o2 = deserialize(o);
o2.taint
o3 = serialize({k: String.tainted("bar"), kkk: "value"});
o4 = deserialize(o3);
o4.k.taint
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants