Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-11432): guard CodeEditor against stack overflow on huge/deeply-nested JSON (#6817)
Aaron Perezf37111f6540a
diff --git a/skyvern-frontend/src/routes/workflows/components/CodeEditor.largeJson.test.tsx b/skyvern-frontend/src/routes/workflows/components/CodeEditor.largeJson.test.tsxnew file mode 100644index 000000000..de5a5d80c--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/components/CodeEditor.largeJson.test.tsx@@ -0,0 +1,99 @@+import { render } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { CodeEditor } from "./CodeEditor";+import { isOversizedDocument } from "./oversizedDocument";++// CodeEditor mounts the real editor immediately only when IntersectionObserver+// is absent; otherwise it renders a lazy-mount placeholder with no `.cm-content`.+// Force the global off so these real-DOM assertions are deterministic even if a+// polyfill (or a future jsdom) provides one.+let savedIntersectionObserver: typeof IntersectionObserver | undefined;+beforeEach(() => {+ savedIntersectionObserver = globalThis.IntersectionObserver;+ // @ts-expect-error deleting an optional global for test determinism+ delete globalThis.IntersectionObserver;+});+afterEach(() => {+ if (savedIntersectionObserver) {+ globalThis.IntersectionObserver = savedIntersectionObserver;+ }+});++// style-mod (used by CodeMirror) prefixes every generated highlight-token+// class with U+037C, so syntax highlighting is active iff a span inside a line+// carries such a class. Plain text only yields structural spans like+// `cm-matchingBracket`.+const STYLE_MOD_PREFIX = "ͼ";++function highlightTokenSpanCount(container: HTMLElement): number {+ return Array.from(container.querySelectorAll(".cm-line span")).filter(+ (span) => (span.getAttribute("class") ?? "").includes(STYLE_MOD_PREFIX),+ ).length;+}++function isLineWrapping(container: HTMLElement): boolean {+ return Boolean(+ container+ .querySelector(".cm-content")+ ?.classList.contains("cm-lineWrapping"),+ );+}++function deeplyNestedJson(depth: number): string {+ return "[".repeat(depth) + "1" + "]".repeat(depth);+}++describe("isOversizedDocument", () => {+ it("treats small, shallow JSON as normal", () => {+ const value = JSON.stringify({ a: 1, b: [1, 2, 3], c: "hi" }, null, 2);+ expect(isOversizedDocument(value)).toBe(false);+ });++ it("flags documents longer than the character threshold", () => {+ const value = `"${"a".repeat(60_000)}"`;+ expect(isOversizedDocument(value)).toBe(true);+ });++ it("flags deeply nested JSON beyond the depth cap", () => {+ expect(isOversizedDocument(deeplyNestedJson(250))).toBe(true);+ });++ it("does not flag moderately nested JSON within the depth cap", () => {+ expect(isOversizedDocument(deeplyNestedJson(20))).toBe(false);+ });++ it("ignores brackets inside string literals when measuring depth", () => {+ const value = JSON.stringify({ note: "[".repeat(500) }, null, 2);+ expect(isOversizedDocument(value)).toBe(false);+ });+});++describe("CodeEditor large-document guard", () => {+ it("highlights and wraps normal JSON", () => {+ const value = JSON.stringify({ a: 1, b: [1, 2, 3], c: "hi" }, null, 2);+ const { container } = render(<CodeEditor value={value} language="json" />);+ expect(highlightTokenSpanCount(container)).toBeGreaterThan(0);+ expect(isLineWrapping(container)).toBe(true);+ });++ it("disables highlighting and wrapping for deeply nested JSON", () => {+ const value = deeplyNestedJson(250);+ const { container } = render(<CodeEditor value={value} language="json" />);+ expect(highlightTokenSpanCount(container)).toBe(0);+ expect(isLineWrapping(container)).toBe(false);+ });++ it("disables highlighting and wrapping for very large JSON", () => {+ const value = `"${"a".repeat(60_000)}"`;+ const { container } = render(<CodeEditor value={value} language="json" />);+ expect(highlightTokenSpanCount(container)).toBe(0);+ expect(isLineWrapping(container)).toBe(false);+ });++ it("renders deeply nested JSON without throwing", () => {+ const value = deeplyNestedJson(5_000);+ expect(() =>+ render(<CodeEditor value={value} language="json" />),+ ).not.toThrow();+ });+});diff --git a/skyvern-frontend/src/routes/workflows/components/CodeEditor.tsx b/skyvern-frontend/src/routes/workflows/components/CodeEditor.tsxindex eb2e5c44d..ec3c39fb2 100644--- a/skyvern-frontend/src/routes/workflows/components/CodeEditor.tsx+++ b/skyvern-frontend/src/routes/workflows/components/CodeEditor.tsx@@ -8,6 +8,7 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";import { cn } from "@/util/utils";import { useDebouncedCallback } from "use-debounce";+import { isOversizedDocument } from "./oversizedDocument";import "./code-mirror-overrides.css";function getLanguageExtension(language: "python" | "json" | "html") {@@ -144,17 +145,24 @@ function CodeEditorImpl({if (!viewRef.current) viewRef.current = viewUpdate.view;}, []);+ const oversized = useMemo(+ () => isOversizedDocument(internalValue),+ [internalValue],+ );+ const effectiveLineWrap = lineWrap && !oversized;+// Memoize the extension tuple so React hands CodeMirror a stable// reference across renders. Without this, a parent re-render would// rebuild the array (and anything spread in) every cycle and trigger// unnecessary editor state reconfiguration.const extensions = useMemo<Extension[]>(() => {- const exts: Extension[] = language- ? [- getLanguageExtension(language),- lineWrap ? EditorView.lineWrapping : [],- ]- : [lineWrap ? EditorView.lineWrapping : []];+ const exts: Extension[] = [];+ if (language && !oversized) {+ exts.push(getLanguageExtension(language));+ }+ if (effectiveLineWrap) {+ exts.push(EditorView.lineWrapping);+ }if (extraExtensions) {exts.push(...extraExtensions);}@@ -162,7 +170,7 @@ function CodeEditorImpl({exts.push(fullHeightExtension);}return exts;- }, [language, lineWrap, extraExtensions, fullHeight]);+ }, [language, oversized, effectiveLineWrap, extraExtensions, fullHeight]);const style: React.CSSProperties = { fontSize };if (fullHeight) {diff --git a/skyvern-frontend/src/routes/workflows/components/oversizedDocument.ts b/skyvern-frontend/src/routes/workflows/components/oversizedDocument.tsnew file mode 100644index 000000000..2f0805cb9--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/components/oversizedDocument.ts@@ -0,0 +1,51 @@+const LARGE_DOCUMENT_CHAR_THRESHOLD = 50_000;+const MAX_STRUCTURE_DEPTH = 200;++function getMaxStructureDepth(text: string, cap: number): number {+ let depth = 0;+ let max = 0;+ let inString = false;+ let escaped = false;+ for (let i = 0; i < text.length; i++) {+ const ch = text[i];+ if (inString) {+ if (escaped) {+ escaped = false;+ } else if (ch === "\\") {+ escaped = true;+ } else if (ch === '"') {+ inString = false;+ }+ continue;+ }+ if (ch === '"') {+ inString = true;+ } else if (ch === "{" || ch === "[") {+ depth += 1;+ if (depth > max) {+ max = depth;+ if (max > cap) {+ return max;+ }+ }+ } else if (ch === "}" || ch === "]") {+ if (depth > 0) {+ depth -= 1;+ }+ }+ }+ return max;+}++/**+ * Large or deeply nested documents drive CodeMirror's view-layer RangeSet scan+ * and line-wrapping measurement into per-level recursion that overflows the+ * call stack (SKY-11432). Above these bounds the editor falls back to plain,+ * unwrapped text instead of syntax-highlighted, wrapped rendering.+ */+export function isOversizedDocument(value: string): boolean {+ if (value.length > LARGE_DOCUMENT_CHAR_THRESHOLD) {+ return true;+ }+ return getMaxStructureDepth(value, MAX_STRUCTURE_DEPTH) > MAX_STRUCTURE_DEPTH;+}diff --git a/skyvern-frontend/src/util/lazyWithReload.test.ts b/skyvern-frontend/src/util/lazyWithReload.test.tsnew file mode 100644index 000000000..e3177e839--- /dev/null+++ b/skyvern-frontend/src/util/lazyWithReload.test.ts@@ -0,0 +1,85 @@+import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";++import {+ installChunkLoadErrorHandler,+ isChunkLoadError,+} from "./lazyWithReload";++const RELOAD_GUARD_KEY = "skyvern.chunkReloadAt";++function dispatchUnhandledRejection(reason: unknown): void {+ const event = new Event("unhandledrejection", {+ cancelable: true,+ }) as Event & { reason: unknown };+ event.reason = reason;+ window.dispatchEvent(event);+}++describe("isChunkLoadError", () => {+ it("recognizes a webpack/Clerk ChunkLoadError string", () => {+ expect(isChunkLoadError("ChunkLoadError: Loading chunk 344 failed")).toBe(+ true,+ );+ });++ it("recognizes a ChunkLoadError instance by its message", () => {+ const error = new Error("Loading chunk 344 failed.");+ error.name = "ChunkLoadError";+ expect(isChunkLoadError(error)).toBe(true);+ });++ it("recognizes a named-chunk failure", () => {+ expect(+ isChunkLoadError("Loading chunk vendors-node_modules_clerk failed"),+ ).toBe(true);+ });++ it("still recognizes Vite dynamic-import failures", () => {+ expect(+ isChunkLoadError(+ new Error("Failed to fetch dynamically imported module: /assets/x.js"),+ ),+ ).toBe(true);+ });++ it("ignores unrelated errors", () => {+ expect(+ isChunkLoadError(new Error("Cannot read properties of undefined")),+ ).toBe(false);+ expect(isChunkLoadError(null)).toBe(false);+ expect(isChunkLoadError(undefined)).toBe(false);+ });+});++describe("installChunkLoadErrorHandler", () => {+ const reloadMock = vi.fn();++ beforeAll(() => {+ Object.defineProperty(window, "location", {+ configurable: true,+ value: { reload: reloadMock },+ });+ installChunkLoadErrorHandler();+ });++ beforeEach(() => {+ reloadMock.mockClear();+ sessionStorage.removeItem(RELOAD_GUARD_KEY);+ });++ it("reloads once on a ChunkLoadError unhandledrejection", () => {+ dispatchUnhandledRejection("ChunkLoadError: Loading chunk 344 failed");+ expect(reloadMock).toHaveBeenCalledTimes(1);+ });++ it("does not reload again within the guard window", () => {+ dispatchUnhandledRejection("ChunkLoadError: Loading chunk 344 failed");+ dispatchUnhandledRejection("ChunkLoadError: Loading chunk 344 failed");+ expect(reloadMock).toHaveBeenCalledTimes(1);+ });++ it("ignores unrelated unhandledrejections", () => {+ dispatchUnhandledRejection(new Error("some unrelated rejection"));+ expect(reloadMock).not.toHaveBeenCalled();+ });+});diff --git a/skyvern-frontend/src/util/lazyWithReload.ts b/skyvern-frontend/src/util/lazyWithReload.tsindex 42249fd4b..b0acf738d 100644--- a/skyvern-frontend/src/util/lazyWithReload.ts+++ b/skyvern-frontend/src/util/lazyWithReload.ts@@ -4,6 +4,10 @@ export const CHUNK_LOAD_PATTERNS = [/Failed to fetch dynamically imported module/i,/Importing a module script failed/i,/error loading dynamically imported module/i,+ // Webpack/Clerk emit a named ChunkLoadError ("Loading chunk 344 failed")+ // when a hashed chunk 404s after a deploy; recover the same way.+ /ChunkLoadError/i,+ /Loading chunk [\w-]+ failed/i,];const RELOAD_GUARD_KEY = "skyvern.chunkReloadAt";@@ -12,7 +16,7 @@ const RELOAD_GUARD_WINDOW_MS = 10_000;// iframes that throw on access). Prevents a reload-storm within one session.let inMemoryReloadFiredAt = 0;-function isChunkLoadError(error: unknown): boolean {+export function isChunkLoadError(error: unknown): boolean {if (!error) return false;const message =error instanceof Error