Skip to content

Commit a367ecc

Browse files
committed
šŸ” Hand a value body down the call stack, where a document cannot reach it
The named-context attack was fixed by sealing the carrier, but the capability was still exported: an eval block's imports are hoisted and resolved like any module's, so a document could import `claimReturn` and `selectReturnValue` from this package's own source by file URL, read the real carrier out of `createContext("expand.return")`, and call the engine's helpers with the engine's carrier. A value root declaring `returns: { type: number }` published "schema-bypassed" at exit 0 without executing its authored <Return>. Sealing the carrier harder cannot fix that, because the two capabilities compose: anything published contextually is readable, and any exported function acting on what was published is callable. So selection now uses neither. A `ReturnBody` is a plain object whose claim, selection and settlement are closures over state nothing else holds, and it reaches the `<Return>` that claims it as an ordinary parameter of expansion — down the call stack, which document code cannot read or replace. Ownership is unchanged and now explicit at every site rather than ambient: a component invocation passes none, caller projection passes the caller's, dynamically scanned markdown and generated fragments pass none, and the two owners settle from the object their own expansion created and held as a local. `ActiveReturn` is gone; nothing exported takes another body's. RV10e gains the direct-import case: a document imports this module by file URL, uses every export it still has, and reaches for the old context. The run fails with the existing missing-return diagnostic, publishes nothing, and the schema-invalid string never reaches the caller. Refs #640
1 parent 9a28c95 commit a367ecc

5 files changed

Lines changed: 179 additions & 202 deletions

File tree

ā€Žpackages/core/src/execute.tsā€Ž

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ import {
8282
validateBodyStructure,
8383
createBlockCounter,
8484
} from "./expand.ts";
85-
import {
86-
ActiveReturn,
87-
createReturnFrame,
88-
missingReturnMessage,
89-
settleReturn,
90-
} from "./return-flow.ts";
85+
import { createReturnBody, missingReturnMessage } from "./return-flow.ts";
9186
import type { BlockCounter } from "./expand.ts";
9287
import {
9388
DocumentationError,
@@ -1600,7 +1595,9 @@ function* runValueRoot(
16001595
): Operation<DocumentResult> {
16011596
// Created outside the scope the body runs in, so the value it selected is
16021597
// still readable after that scope — and its teardown — has finished.
1603-
const carrier = createReturnFrame("__root__", returns);
1598+
// The root's own, held as a local and handed to the segments it expands, so
1599+
// nothing a document can read or replace decides what this root returns.
1600+
const ownBody = createReturnBody("__root__", returns);
16041601

16051602
yield* scoped(function* () {
16061603
yield* Component.around(
@@ -1612,11 +1609,6 @@ function* runValueRoot(
16121609
{ at: "min" },
16131610
);
16141611

1615-
// The root's own frame, published for the whole body: a `<Return>` under
1616-
// `<If>` or inside a `<Loop>` is reached by ordinary expansion and finds it,
1617-
// because those directives keep the ambient frame.
1618-
yield* ActiveReturn.set(carrier);
1619-
16201612
for (const segment of root.bodySegments) {
16211613
const expanded = yield* expandSegmentsWithin(
16221614
[segment],
@@ -1629,6 +1621,7 @@ function* runValueRoot(
16291621
0,
16301622
checkedFailures,
16311623
authority,
1624+
ownBody,
16321625
);
16331626
for (const resolved of expanded) {
16341627
const text = renderSegment(resolved);
@@ -1642,9 +1635,7 @@ function* runValueRoot(
16421635

16431636
yield* refuseCheckedFailure(checkedFailures);
16441637
// Read after the body finished, so a return anywhere in it has executed.
1645-
// Settled from the carrier this root minted and has held since, not from the
1646-
// context, so a document that replaces the context selects nothing.
1647-
const selected = settleReturn(carrier);
1638+
const selected = ownBody.settle();
16481639
if (!selected) {
16491640
throw new Error(missingReturnMessage("__root__"));
16501641
}
@@ -1785,6 +1776,7 @@ function* documentWorkflow(
17851776
rootPath,
17861777
checkedFailures,
17871778
authority,
1779+
undefined,
17881780
);
17891781
const text = selected.map(renderSegment).join("");
17901782
// An empty buffered root emits no output event.
@@ -1811,6 +1803,7 @@ function* documentWorkflow(
18111803
0,
18121804
checkedFailures,
18131805
authority,
1806+
undefined,
18141807
);
18151808

18161809
while (emittedThrough < produced.length) {

0 commit comments

Comments
Ā (0)