Skip to content

fix: untainted prototype access in Angular #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-poems-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rrweb/utils": patch
---

safer access to iframes in safari for untainted prototypes
49 changes: 28 additions & 21 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,36 @@ export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
),
);

if (isUntaintedAccessors && isUntaintedMethods && !isAngularZonePresent()) {
untaintedBasePrototype[key] = defaultObj.prototype as BasePrototypeCache[T];
return defaultObj.prototype as BasePrototypeCache[T];
const isUntainted =
isUntaintedAccessors && isUntaintedMethods && !isAngularZonePresent();
// we're going to default to what we do have
let impl: BasePrototypeCache[T] =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i kept the reorganization here since I did find it difficult to track what was happening previously

defaultObj.prototype as BasePrototypeCache[T];
// but if it is tainted
if (!isUntainted) {
// try to load a fresh copy from a sandbox iframe
let iframeEl: HTMLIFrameElement | undefined = undefined;
try {
iframeEl = document.createElement('iframe');
iframeEl.hidden = true;
document.head.appendChild(iframeEl);
const win = iframeEl.contentWindow;
if (win) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const candidate = (win as any)[key].prototype as BasePrototypeCache[T];
if (candidate) {
impl = candidate;
}
}
} finally {
if (iframeEl) {
document.head.removeChild(iframeEl);
}
}
}

try {
const iframeEl = document.createElement('iframe');
document.body.appendChild(iframeEl);
const win = iframeEl.contentWindow;
if (!win) return defaultObj.prototype as BasePrototypeCache[T];

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const untaintedObject = (win as any)[key]
.prototype as BasePrototypeCache[T];
// cleanup
document.body.removeChild(iframeEl);

if (!untaintedObject) return defaultPrototype;

return (untaintedBasePrototype[key] = untaintedObject);
} catch {
return defaultPrototype;
}
untaintedBasePrototype[key] = impl;
return impl;
}

const untaintedAccessorCache: Record<
Expand Down
Loading