Skip to content
Draft
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
20 changes: 17 additions & 3 deletions packages/anvil-react/src/plugins/core/valueSetFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ const under = (issues: ValidationIssue[], path: string) =>
i.path.startsWith(`${path}[`),
);

/**
* Entry `index` of a `contained` list, read only when `index` is a number that
* names one of the list's own entries, so a key such as `__proto__` cannot
* reach a prototype.
*/
const containedAt = (contained: unknown, index: unknown): unknown =>
(
Array.isArray(contained) &&
typeof index === "number" &&
Object.hasOwn(contained, index)
) ?
contained[index]
: undefined;

/** compose.include[j] of `vs`, replacing missing or malformed parts with empty ones. */
const includeAt = (vs: ValueSet, j: number): ValueSetInclude => {
const compose: NonNullable<ValueSet["compose"]> =
Expand Down Expand Up @@ -118,7 +132,7 @@ const ContainedIdInput = ({
setDraft(next);
store.update(
(doc) => {
const target = doc.root.contained?.[index];
const target = containedAt(doc.root.contained, index);
if (!isObject(target)) return;
if (next === undefined) delete target.id;
else target.id = next;
Expand Down Expand Up @@ -163,7 +177,7 @@ export const ValueSetEditor = ({
const { store } = useEditor();
const base = `Questionnaire.contained[${index}]`;
const issues = under(useItemIssues(null), base);
const vs = document.root.contained?.[index];
const vs = containedAt(document.root.contained, index);
if (!isValueSet(vs)) return null;

const id = typeof vs.id === "string" ? vs.id : undefined;
Expand All @@ -176,7 +190,7 @@ export const ValueSetEditor = ({
) =>
store.update(
(doc) => {
const target = doc.root.contained?.[index];
const target = containedAt(doc.root.contained, index);
if (isValueSet(target)) recipe(target);
},
{
Expand Down
45 changes: 44 additions & 1 deletion packages/anvil-react/test/valueSets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
render,
within,
} from "@testing-library/react";
import { AnvilEditor, defaultPlugins } from "../src";
import {
AnvilEditor,
defaultPlugins,
definePlugin,
ValueSetEditor,
} from "../src";
import { at } from "./helpers";

afterEach(cleanup);
Expand Down Expand Up @@ -212,6 +217,44 @@ describe("editor UI", () => {
expect(problems(out)).toEqual([]);
});

it("ignores an index that is not a number, such as a prototype property", () => {
const keys = ["__proto__", "constructor", "prototype", "0"];
const Editors = () => (
<>
<ValueSetEditor index={0} />
{keys.map((key) => (
<ValueSetEditor key={key} index={key as unknown as number} />
))}
</>
);
const plugin = definePlugin({
id: "host",
sections: [{ id: "host", label: "Host", target: "questionnaire" }],
questionnaireFields: [
{ id: "host.valueSets", section: "host", component: Editors },
],
});
const store = createEditorStore(base);
const { getAllByRole } = render(
<AnvilEditor store={store} plugins={[...defaultPlugins, plugin]} />,
);

// One editor from the core value set field and one for index 0; none for the keys.
const editors = getAllByRole("region", { name: /^Value set / });
expect(editors.map((region) => region.getAttribute("aria-label"))).toEqual([
"Value set frequency",
"Value set frequency",
]);
const id = within(at(editors, 1)).getByLabelText("Value set id");
fireEvent.change(id, { target: { value: "often" } });
fireEvent.blur(id);
expect(at(toQuestionnaire(store.getState().document).contained, 0)).toEqual(
{ ...frequency, id: "often" },
);
expect(Object.hasOwn(Array.prototype, "id")).toBe(false);
expect(Object.hasOwn(Object.prototype, "id")).toBe(false);
});

it("adds units with UCUM defaults", () => {
const store = createEditorStore({
resourceType: "Questionnaire",
Expand Down
Loading