The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
enh(docs): more checks for external CodeSample (#29799)
Charisdbbbc20d635b
diff --git a/apps/docs/app/contributing/content.mdx b/apps/docs/app/contributing/content.mdxindex 12cba239f87..a185cff8895 100644--- a/apps/docs/app/contributing/content.mdx+++ b/apps/docs/app/contributing/content.mdx@@ -167,7 +167,7 @@ const PI = 3.14```````-Of, you can use the `<$CodeSample />` component to include code samples from a source code file.+Or, you can use the `<$CodeSample />` component to include code samples from a source code file.If the file is within the `supabase/supabase` repo's `examples` directory:@@ -183,7 +183,7 @@ meta="display/path.js"/>```-If the file is within some other GitHub repo (note that the repo must be public):+If the file is within some other GitHub repo:```mdx<$CodeSample@@ -197,6 +197,8 @@ meta="display/path.js"/>```+The repo must be public, the org must be on the allow list, and the commit must be an immutable SHA (not a mutable tag or branch name).+### IconsThe following icons are available. They can be styled with [Tailwind](https://tailwindcss.com/) classes:diff --git a/apps/docs/features/directives/CodeSample.ts b/apps/docs/features/directives/CodeSample.tsindex d97f0c3fe79..ab297b45e6b 100644--- a/apps/docs/features/directives/CodeSample.ts+++ b/apps/docs/features/directives/CodeSample.ts@@ -48,6 +48,8 @@ import { z, type SafeParseError } from 'zod'import { fetchWithNextOptions } from '~/features/helpers.fetch'import { EXAMPLES_DIRECTORY } from '~/lib/docs'+const ALLOW_LISTED_GITHUB_ORGS = ['supabase'] as [string, ...string[]]+const linesSchema = z.array(z.tuple([z.coerce.number(), z.coerce.number()]))const linesValidator = z.string()@@ -73,7 +75,9 @@ type AdditionalMeta = {const codeSampleExternalSchema = z.object({external: z.coerce.boolean().refine((v) => v === true),- org: z.string(),+ org: z.enum(ALLOW_LISTED_GITHUB_ORGS, {+ errorMap: () => ({ message: 'Org must be one of: ' + ALLOW_LISTED_GITHUB_ORGS.join(', ') }),+ }),repo: z.string(),commit: z.string(),path: z.string().transform((v) => (v.startsWith('/') ? v : `/${v}`)),diff --git a/apps/docs/features/directives/utils.ts b/apps/docs/features/directives/utils.tsindex eb67d138d91..d52e1fd94cd 100644--- a/apps/docs/features/directives/utils.ts+++ b/apps/docs/features/directives/utils.ts@@ -9,6 +9,7 @@ import { mdxjs } from 'micromark-extension-mdxjs'import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition'import remarkPyMdownTabs from '~/lib/mdx/plugins/remarkTabs'import { getGitHubFileContents } from '~/lib/octokit'+import { getGitHubFileContentsImmutableOnly } from '~/lib/octokit'import { codeSampleRemark } from './CodeSample'type Transformer = (ast: Root) => Root | Promise<Root>@@ -31,6 +32,8 @@ export function preprocessMdxWithDefaults(mdx: string) {return preprocessMdx(mdx, [remarkMkDocsAdmonition(),remarkPyMdownTabs(),- codeSampleRemark({ fetchFromGitHub: getGitHubFileContents }),+ codeSampleRemark({+ fetchFromGitHub: getGitHubFileContentsImmutableOnly,+ }),])}diff --git a/apps/docs/lib/octokit.ts b/apps/docs/lib/octokit.tsindex c1ccf9073cb..155e6f20720 100644--- a/apps/docs/lib/octokit.ts+++ b/apps/docs/lib/octokit.ts@@ -35,7 +35,7 @@ export async function getGitHubFileContents({repo,path,branch,- options: { onError },+ options: { onError, fetch },}: {org: stringrepo: string@@ -63,7 +63,7 @@ export async function getGitHubFileContents({path: path,ref: branch,options: {- fetch: fetchRevalidatePerDay,+ fetch: fetch ?? fetchRevalidatePerDay,},})if (response.status !== 200 || !response.data) {@@ -79,3 +79,80 @@ export async function getGitHubFileContents({onError?.(err)}}++export async function getGitHubFileContentsImmutableOnly({+ org,+ repo,+ branch,+ path,+ options: { onError, fetch },+}: {+ org: string+ repo: string+ branch: string+ path: string+ options: {+ onError: (err?: unknown) => void+ fetch?: (info: RequestInfo, init?: RequestInit) => Promise<Response>+ }+}) {+ const isImmutableCommit = await checkForImmutableCommit({+ org,+ repo,+ branch,+ })+ if (!isImmutableCommit) {+ throw Error('The commit is not an immutable commit SHA. Tags and branch names are not allowed.')+ }++ return getGitHubFileContents({+ org,+ repo,+ branch,+ path,+ options: { onError, fetch },+ })+}++async function checkForImmutableCommit({+ org,+ repo,+ branch,+ options: { fetch: _fetch = fetch } = {},+}: {+ org: string+ repo: string+ branch: string+ options?: {+ /**+ *+ * A custom fetch implementation to control Next.js caching.+ */+ fetch?: (info: RequestInfo, init?: RequestInit) => Promise<Response>+ }+}) {+ try {+ const response = await octokit().request('GET /repos/{owner}/{repo}/git/commits/{commit_sha}', {+ owner: org,+ repo: repo,+ commit_sha: branch,+ headers: {+ 'X-GitHub-Api-Version': '2022-11-28',+ },+ options: {+ fetch: _fetch,+ },+ })+ if (response.status === 200) {+ return true+ } else {+ throw Error(+ "Checking for an immutable commit didn't throw an error, but it also didn't return a 200. Erring on the side of safety, assuming this is not an immutable commit.",+ { cause: response }+ )+ }+ } catch (err) {+ console.error('Not an immutable commit: %o', err)+ return false+ }+}