Skip to content

fix: don't reuse proxies when state symbol refers to stale value #10343

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

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/tidy-starfishes-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: only reuse state proxies that belong to the current value
27 changes: 22 additions & 5 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const READONLY_SYMBOL = Symbol('readonly');
export function proxy(value, immutable = true) {
if (typeof value === 'object' && value != null && !is_frozen(value)) {
if (STATE_SYMBOL in value) {
return /** @type {import('./types.js').ProxyMetadata<T>} */ (value[STATE_SYMBOL]).p;
const metadata = /** @type {import('./types.js').ProxyMetadata<T>} */ (value[STATE_SYMBOL]);
// Check that the incoming value is the same proxy that this state symbol was created for:
// If someone copies over the state symbol to a new object (using Reflect.ownKeys) the referenced
// proxy could be stale and we should not return it.
if (metadata.t === value || metadata.p === value) return metadata.p;
}

const prototype = get_prototype_of(value);
Expand All @@ -51,7 +55,8 @@ export function proxy(value, immutable = true) {
/** @type {import('./types.js').ProxyStateObject<T>} */ (proxy),
immutable
),
writable: false
writable: true,
enumerable: false
});

return proxy;
Expand All @@ -68,7 +73,7 @@ export function proxy(value, immutable = true) {
* @returns {Record<string | symbol, any>}
*/
function unwrap(value, already_unwrapped = new Map()) {
if (typeof value === 'object' && value != null && !is_frozen(value) && STATE_SYMBOL in value) {
if (typeof value === 'object' && value != null && STATE_SYMBOL in value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really even need a STATE_SYMBOL in the value? Then we can unwrap derived proxied objects too.

const unwrapped = already_unwrapped.get(value);
if (unwrapped !== undefined) {
return unwrapped;
Expand Down Expand Up @@ -100,6 +105,7 @@ function unwrap(value, already_unwrapped = new Map()) {
return obj;
}
}

return value;
}

Expand All @@ -124,7 +130,8 @@ function init(value, proxy, immutable) {
v: source(0),
a: is_array(value),
i: immutable,
p: proxy
p: proxy,
t: value
};
}

Expand Down Expand Up @@ -171,6 +178,10 @@ const state_proxy_handler = {
if (DEV && prop === READONLY_SYMBOL) {
return Reflect.get(target, READONLY_SYMBOL);
}
if (prop === STATE_SYMBOL) {
return Reflect.get(target, STATE_SYMBOL);
}

const metadata = target[STATE_SYMBOL];
let s = metadata.s.get(prop);

Expand Down Expand Up @@ -304,7 +315,13 @@ if (DEV) {
*/
export function readonly(value) {
const proxy = value && value[READONLY_SYMBOL];
if (proxy) return proxy;
if (proxy) {
const metadata = value[STATE_SYMBOL];
// Check that the incoming value is the same proxy that this readonly symbol was created for:
// If someone copies over the readonly symbol to a new object (using Reflect.ownKeys) the referenced
// proxy could be stale and we should not return it.
if (metadata.p === value) return proxy;
}

if (
typeof value === 'object' &&
Expand Down
2 changes: 2 additions & 0 deletions packages/svelte/src/internal/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ export interface ProxyMetadata<T = Record<string | symbol, any>> {
i: boolean;
/** The associated proxy */
p: ProxyStateObject<T> | ProxyReadonlyObject<T>;
/** The original target this proxy was created for */
t: T;
}

export type ProxyStateObject<T = Record<string | symbol, any>> = T & {
Expand Down
16 changes: 16 additions & 0 deletions packages/svelte/tests/runtime-runes/samples/state-reuse/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},

html: `<button>state1.value: a state2.value: a</button>`,

async test({ assert, target }) {
const btn = target.querySelector('button');

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>state1.value: b state2.value: b</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
let foo = { value: 'a' }
let state1 = $state(foo);
let state2 = $state(foo);
</script>

<button onclick={() => {
let new_state1 = {};
let new_state2 = {};
// This contains Symbol.$state and Symbol.$readonly and we can't do anything against it,
// because it's called on the original object, not our state proxy
Reflect.ownKeys(foo).forEach(k => {
new_state1[k] = foo[k];
new_state2[k] = foo[k];
});
new_state1.value = 'b';
new_state2.value = 'b';
// $.proxy will see that Symbol.$state exists on this object already, which shouldn't result in a stale value
state1 = new_state1;
// $.proxy can't look into Symbol.$state because of the frozen object
state2 = Object.freeze(new_state2);
}}
>
state1.value: {state1.value}
state2.value: {state2.value}
</button>