-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add skew protection to all api routes
- Loading branch information
1 parent
718429f
commit 286a1b1
Showing
8 changed files
with
53 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import useSWR, { Fetcher, SWRConfiguration, SWRResponse } from "swr"; | ||
import useSWRImmutable from "swr/immutable"; | ||
import { withSkewProtection } from "../util/withSkewProtection"; | ||
import { FernDocsApiRoute, useApiRoute } from "./useApiRoute"; | ||
|
||
export function useApiRouteSWR<T>( | ||
route: FernDocsApiRoute, | ||
options?: SWRConfiguration<T, Error, Fetcher<T>> & { disabled?: boolean }, | ||
): SWRResponse<T> { | ||
const key = useApiRoute(route); | ||
return useSWR( | ||
options?.disabled ? null : key, | ||
(url): Promise<T> => fetch(url, { headers: withSkewProtection() }).then((r) => r.json()), | ||
options, | ||
); | ||
} | ||
|
||
export function useApiRouteSWRImmutable<T>( | ||
route: FernDocsApiRoute, | ||
options?: SWRConfiguration<T, Error, Fetcher<T>> & { disabled?: boolean }, | ||
): SWRResponse<T> { | ||
const key = useApiRoute(route); | ||
return useSWRImmutable( | ||
options?.disabled ? null : key, | ||
(url): Promise<T> => fetch(url, { headers: withSkewProtection() }).then((r) => r.json()), | ||
options, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import type { APIKeyInjectionConfig } from "@fern-ui/fern-docs-auth"; | ||
import useSWR from "swr"; | ||
import { useApiRoute } from "../hooks/useApiRoute"; | ||
import { useApiRouteSWR } from "../hooks/useApiRouteSWR"; | ||
|
||
const DEFAULT = { enabled: false as const }; | ||
|
||
export function useApiKeyInjectionConfig(): APIKeyInjectionConfig { | ||
const key = useApiRoute("/api/fern-docs/auth/api-key-injection"); | ||
const { data } = useSWR<APIKeyInjectionConfig>( | ||
key, | ||
async (url: string) => { | ||
const res = await fetch(url); | ||
return res.json(); | ||
}, | ||
{ | ||
refreshInterval: (latestData) => (latestData?.enabled ? 1000 * 60 * 5 : 0), // refresh every 5 minutes | ||
}, | ||
); | ||
const { data } = useApiRouteSWR<APIKeyInjectionConfig>("/api/fern-docs/auth/api-key-injection", { | ||
refreshInterval: (latestData) => (latestData?.enabled ? 1000 * 60 * 5 : 0), // refresh every 5 minutes | ||
}); | ||
return data ?? DEFAULT; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function withSkewProtection(headers?: HeadersInit): HeadersInit | undefined { | ||
if (process.env.NEXT_DEPLOYMENT_ID == null) { | ||
return headers; | ||
} | ||
|
||
const h = new Headers(headers); | ||
h.set("x-deployment-id", process.env.NEXT_DEPLOYMENT_ID); | ||
return h; | ||
} |