History

Parse auth profile and ref hint tables safely

Rhys Sullivan committed 8 months ago

Commit b11f3f21 parent f6c35902 files · +16 −5Browse files

Unified · Split

2 files
  1. executor/apps/web/src/lib/convex-provider.tsx +6 −2
  2. executor/packages/database/src/runtime/workspace_tools.ts +10 −3
executor/apps/web/src/lib/convex-provider.tsx+6−2
@@ -2,6 +2,7 @@ · 1 unmodified line
22
33 import { createContext, useCallback, useContext, useMemo } from "react";
44 import { ConvexProviderWithAuth } from "convex/react";
5+import { z } from "zod";
56 import {
67 AuthKitProvider,
78 useAccessToken,
@@ -36,6 +37,8 @@ type WorkosAuthProfile = { · 28 unmodified lines
3637 avatarUrl?: string | null;
3738 };
3839
40+const recordSchema = z.record(z.unknown());
41+
3942 function readNonEmptyString(value: unknown): string | undefined {
4043 if (typeof value !== "string") {
4144 return undefined;
@@ -57,11 +60,12 @@ function getRecordString(record: Record<string, unknown>, keys: string[]): strin · 15 unmodified lines
5760 }
5861
5962 function buildWorkosAuthProfile(user: unknown): WorkosAuthProfile | null {
60− if (!user || typeof user !== "object") {
63+ const parsedUser = recordSchema.safeParse(user);
64+ if (!parsedUser.success) {
6165 return null;
6266 }
6367
64− const userRecord = user as Record<string, unknown>;
68+ const userRecord = parsedUser.data;
6569 const firstName = getRecordString(userRecord, ["firstName", "first_name", "givenName", "given_name"]);
6670 const lastName = getRecordString(userRecord, ["lastName", "last_name", "familyName", "family_name"]);
6771 const combinedName = [firstName, lastName].filter(Boolean).join(" ").trim();
executor/packages/database/src/runtime/workspace_tools.ts+10−3
@@ -255,12 +255,19 @@ function buildBoundedOpenApiRefHintTables( · 254 unmodified lines
255255 let remainingBudget = MAX_REF_HINT_METADATA_BYTES;
256256
257257 const tables = externalArtifacts
258− .filter((artifact) => typeof artifact.openApiSourceKey === "string" && typeof artifact.openApiRefHintTable === "object")
258+ .filter(
259+ (
260+ artifact,
261+ ): artifact is CompiledToolSourceArtifact & { openApiSourceKey: string; openApiRefHintTable: Record<string, string> } => (
262+ typeof artifact.openApiSourceKey === "string"
263+ && artifact.openApiRefHintTable !== undefined
264+ ),
265+ )
259266 .map((artifact) => {
260− const sourceKey = artifact.openApiSourceKey!;
267+ const sourceKey = artifact.openApiSourceKey;
261268 const refs: Array<{ key: string; hint: string }> = [];
262269
263− const rawEntries = Object.entries(artifact.openApiRefHintTable ?? {})
270+ const rawEntries = Object.entries(artifact.openApiRefHintTable)
264271 .filter((entry): entry is [string, string] => typeof entry[0] === "string" && typeof entry[1] === "string");
265272
266273 for (const [key, rawHint] of rawEntries) {