Repository navigation
Fix projected ops mutating the shared create object - #782
draarivpatel-ui wants to merge 2 commits into
Conversation
projectOp() projected op.create in place. Ops from pubsub are shared between subscribed agents and Agent._onOp only makes a shallow copy, so whichever agent projected the create first rewrote the create data for every other subscriber. An agent subscribed to the unprojected collection could then send the client projected create data. Project a copy of the create object instead, matching how projectEdit already returns a new component array. Fixes share#779
alecgibson
left a comment
There was a problem hiding this comment.
Thanks for picking this up. The fix looks right to me. It's what the contract in Agent._onOp already asks for ("projections and any user middleware copy deep properties as needed when they modify the op"), and it matches what projectEdit already does for op.op. My main ask is about the test: I'd rather it ran the repro from #779 end to end than hand-copy _onOp in a unit test.
Nit (personal preference): test/projections.js:271 — If we keep a unit test, I don't think the shallow copy of op is needed. Projecting op directly and asserting that the original create is untouched checks the same thing ("projectOp doesn't mutate what it's given") without copying _onOp. If the copy does stay, ../lib/util is already required at the top for clone, so shallowCopy could come from there too.
Generated by Claude Code via /alec-review. Patterns derived from Alec's historical review comments — review with that in mind.
| ); | ||
| }); | ||
|
|
||
| it('does not mutate the create object on a shallow copy of the op', function() { |
There was a problem hiding this comment.
Could we test this "deep" in test/client/projections.js instead? This test copies _onOp's shallow copy by hand, so if _onOp ever changes how it copies, this test will keep passing without proving anything. The repro from #779 drops straight in as a client test. I tried it: it fails on main (expected { age: 5 } to deeply equal { age: 5, color: 'spotted' }) and passes with your fix. That file also runs against the other DB adapters, so we get those for free.
it('does not project the create sent to an unprojected subscriber', function(done) {
var connection = this.connection;
var summary = this.backend.connect().get('dogs_summary', 'spot');
var full = this.backend.connect().get('dogs', 'spot');
summary.subscribe(function(err) {
if (err) return done(err);
full.subscribe(function(err) {
if (err) return done(err);
full.on('create', function() {
expect(full.data).eql({age: 5, color: 'spotted'});
done();
});
connection.get('dogs', 'spot').create({age: 5, color: 'spotted'});
});
});
});(The subscription order matters: the projected agent has to sanitize first.)
| // Pubsub hands the same op object to every subscribed agent, and | ||
| // agent._onOp only makes a shallow copy, so the create object is | ||
| // shared. Projecting one agent's copy must not rewrite the create | ||
| // data seen by other subscribers. |
There was a problem hiding this comment.
Do we need this comment? It mostly repeats the test name and the PR description, and it will drift the moment _onOp changes. A client test like the one above explains itself.
| // The op may be a shallow copy of an op object shared with other | ||
| // subscribers, so project a copy of the create instead of mutating it |
There was a problem hiding this comment.
(Matter of taste.) Do we need this comment either? _onOp already spells out the contract, and the regression test fails if someone "simplifies" the copy away. I'd keep the why in the commit message, where git blame finds it. You've basically already written it in the PR description.
|
Thanks, updated in de017df. The regression now runs end to end in |
Fixes #779
Bug
projectOp()projectsop.createin place. Ops emitted from pubsub are shared between every subscribed agent, andAgent._onOp()only makes a shallow copy, so all agents share the samecreateobject. Whichever agent'ssanitizeOpprojects the create first rewritescreate.datafor everyone else, so a subscriber on the unprojected collection (or a differently projected one) sends its client projected create data until it fetches again.Fix
Project a copy of the
createobject and assign it back onto the op, matching howprojectEditalready builds a new component array rather than editing it in place.Test
Added a regression test that mirrors
Agent._onOp: shallow-copy the op, project the copy, and assert the sharedcreateobject keeps its original data while the copy is projected. It fails on main (create.dataends up filtered) and passes with this fix. Full suite: 1113 passing.