Skip to content
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

fix: use indirect eval to prevent local scope access in repl playground #11330

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
264 changes: 136 additions & 128 deletions sites/svelte-5-preview/src/lib/Output/srcdoc/index.html
Expand Up @@ -85,7 +85,7 @@
if (action === 'eval') {
try {
const { script } = ev.data.args;
eval(script);
(0, eval)(script);
send_ok();
} catch (e) {
send_error(e.message, e.stack);
Expand Down Expand Up @@ -140,145 +140,153 @@
window.addEventListener('unhandledrejection', (event) => {
parent.postMessage({ action: 'unhandledrejection', value: event.reason }, '*');
});
}).call(this);

let previous = { level: null, args: null };
let previous = { level: null, args: null };

['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => {
const original = console[level];
console[level] = (...args) => {
const stringifiedArgs = stringify(args);
if (previous.level === level && previous.args && previous.args === stringifiedArgs) {
parent.postMessage({ action: 'console', level, duplicate: true }, '*');
} else {
previous = { level, args: stringifiedArgs };

try {
parent.postMessage({ action: 'console', level, args }, '*');
} catch (err) {
parent.postMessage({ action: 'console', level: 'unclonable' }, '*');
}
}

['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => {
const original = console[level];
console[level] = (...args) => {
const stringifiedArgs = stringify(args);
if (previous.level === level && previous.args && previous.args === stringifiedArgs) {
parent.postMessage({ action: 'console', level, duplicate: true }, '*');
} else {
previous = { level, args: stringifiedArgs };
original(...args);
};
});

try {
parent.postMessage({ action: 'console', level, args }, '*');
} catch (err) {
parent.postMessage({ action: 'console', level: 'unclonable' }, '*');
}
}
[
{ method: 'group', action: 'console_group' },
{ method: 'groupEnd', action: 'console_group_end' },
{ method: 'groupCollapsed', action: 'console_group_collapsed' }
].forEach((group_action) => {
const original = console[group_action.method];
console[group_action.method] = (label) => {
parent.postMessage({ action: group_action.action, label }, '*');

original(label);
};
});

const timers = new Map();
const original_time = console.time;
const original_timelog = console.timeLog;
const original_timeend = console.timeEnd;

original(...args);
console.time = (label = 'default') => {
original_time(label);
timers.set(label, performance.now());
};
});

[
{ method: 'group', action: 'console_group' },
{ method: 'groupEnd', action: 'console_group_end' },
{ method: 'groupCollapsed', action: 'console_group_collapsed' }
].forEach((group_action) => {
const original = console[group_action.method];
console[group_action.method] = (label) => {
parent.postMessage({ action: group_action.action, label }, '*');

original(label);
console.timeLog = (label = 'default') => {
original_timelog(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: [`Timer '${label}' does not exist`]
},
'*'
);
}
};
});

const timers = new Map();
const original_time = console.time;
const original_timelog = console.timeLog;
const original_timeend = console.timeEnd;

console.time = (label = 'default') => {
original_time(label);
timers.set(label, performance.now());
};
console.timeLog = (label = 'default') => {
original_timelog(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] },
'*'
);
}
};
console.timeEnd = (label = 'default') => {
original_timeend(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
console.timeEnd = (label = 'default') => {
original_timeend(label);
const now = performance.now();
if (timers.has(label)) {
parent.postMessage(
{
action: 'console',
level: 'system-log',
args: [`${label}: ${now - timers.get(label)}ms`]
},
'*'
);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: [`Timer '${label}' does not exist`]
},
'*'
);
}
timers.delete(label);
};

const original_assert = console.assert;
console.assert = (condition, ...args) => {
if (condition) {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*');
}
original_assert(condition, ...args);
};

const counter = new Map();
const original_count = console.count;
const original_countreset = console.countReset;

console.count = (label = 'default') => {
counter.set(label, (counter.get(label) || 0) + 1);
parent.postMessage(
{ action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] },
{ action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` },
'*'
);
}
timers.delete(label);
};
original_count(label);
};

console.countReset = (label = 'default') => {
if (counter.has(label)) {
counter.set(label, 0);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: `Count for '${label}' does not exist`
},
'*'
);
}
original_countreset(label);
};

const original_trace = console.trace;

const original_assert = console.assert;
console.assert = (condition, ...args) => {
if (condition) {
console.trace = (...args) => {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*');
}
original_assert(condition, ...args);
};

const counter = new Map();
const original_count = console.count;
const original_countreset = console.countReset;

console.count = (label = 'default') => {
counter.set(label, (counter.get(label) || 0) + 1);
parent.postMessage(
{ action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` },
'*'
);
original_count(label);
};

console.countReset = (label = 'default') => {
if (counter.has(label)) {
counter.set(label, 0);
} else {
parent.postMessage(
{
action: 'console',
level: 'system-warn',
args: `Count for '${label}' does not exist`
},
'*'
);
}
original_countreset(label);
};

const original_trace = console.trace;

console.trace = (...args) => {
const stack = new Error().stack;
parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*');
original_trace(...args);
};

function stringify(args) {
try {
return JSON.stringify(args);
} catch (error) {
return null;
parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*');
original_trace(...args);
};

function stringify(args) {
try {
return JSON.stringify(args);
} catch (error) {
return null;
}
}
}
})();
</script>
</head>
<body></body>
Expand Down