From 4ed317961872cb0dd76a17cdbaea6c4d2d4e7edc Mon Sep 17 00:00:00 2001 From: Yazeed Loonat Date: Tue, 7 May 2024 13:01:30 -0700 Subject: [PATCH] fix: Frontend server request ip updates (#4066) * fix: testing how throttling works on server side requests * fix: updates to make testing easier * fix: possible fix * fix: some small tweaks * fix: updates * fix: forwards requester IP to API for throttling * fix: missed an update * fix: test update --- .../partners/src/pages/listings/[id]/edit.tsx | 9 +- .../src/pages/listings/[id]/index.tsx | 9 +- sites/public/src/lib/hooks.ts | 95 +++++++++++-------- sites/public/src/pages/index.tsx | 5 +- sites/public/src/pages/listing/[id].tsx | 9 +- .../public/src/pages/listing/[id]/[slug].tsx | 12 ++- sites/public/src/pages/listings.tsx | 7 +- .../src/pages/preview/listings/[id].tsx | 11 ++- 8 files changed, 102 insertions(+), 55 deletions(-) diff --git a/sites/partners/src/pages/listings/[id]/edit.tsx b/sites/partners/src/pages/listings/[id]/edit.tsx index 1844fc2aee..8dae5061a0 100644 --- a/sites/partners/src/pages/listings/[id]/edit.tsx +++ b/sites/partners/src/pages/listings/[id]/edit.tsx @@ -60,12 +60,17 @@ const EditListing = (props: { listing: Listing }) => { ) } -export async function getServerSideProps(context: { params: Record }) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getServerSideProps(context: { params: Record; req: any }) { let response try { response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, { - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": + context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress, + }, }) } catch (e) { console.log("e = ", e) diff --git a/sites/partners/src/pages/listings/[id]/index.tsx b/sites/partners/src/pages/listings/[id]/index.tsx index d37dc465f2..3be1b33468 100644 --- a/sites/partners/src/pages/listings/[id]/index.tsx +++ b/sites/partners/src/pages/listings/[id]/index.tsx @@ -125,12 +125,17 @@ export default function ListingDetail(props: ListingProps) { ) } -export async function getServerSideProps(context: { params: Record }) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getServerSideProps(context: { params: Record; req: any }) { let response try { response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, { - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": + context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress, + }, }) } catch (e) { return { notFound: true } diff --git a/sites/public/src/lib/hooks.ts b/sites/public/src/lib/hooks.ts index 33675dc5ef..027e81bcbf 100644 --- a/sites/public/src/lib/hooks.ts +++ b/sites/public/src/lib/hooks.ts @@ -59,20 +59,24 @@ export const useGetApplicationStatusProps = (listing: Listing): ApplicationStatu return props } -export async function fetchBaseListingData({ - additionalFilters, - orderBy, - orderDir, - limit, -}: { - additionalFilters?: ListingFilterParams[] - orderBy?: ListingOrderByKeys[] - orderDir?: OrderByEnum[] - limit?: string -}) { +export async function fetchBaseListingData( + { + additionalFilters, + orderBy, + orderDir, + limit, + }: { + additionalFilters?: ListingFilterParams[] + orderBy?: ListingOrderByKeys[] + orderDir?: OrderByEnum[] + limit?: string + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + req: any +) { let listings = [] try { - const { id: jurisdictionId } = await fetchJurisdictionByName() + const { id: jurisdictionId } = await fetchJurisdictionByName(req) if (!jurisdictionId) { return listings @@ -109,7 +113,10 @@ export async function fetchBaseListingData({ paramsSerializer: (params) => { return qs.stringify(params) }, - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": req.headers["x-forwarded-for"] ?? req.socket.remoteAddress, + }, }) listings = response.data?.items @@ -120,36 +127,45 @@ export async function fetchBaseListingData({ return listings } -export async function fetchOpenListings() { - return await fetchBaseListingData({ - additionalFilters: [ - { - $comparison: EnumListingFilterParamsComparison["="], - status: ListingsStatusEnum.active, - }, - ], - orderBy: [ListingOrderByKeys.mostRecentlyPublished], - orderDir: [OrderByEnum.desc], - }) +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function fetchOpenListings(req: any) { + return await fetchBaseListingData( + { + additionalFilters: [ + { + $comparison: EnumListingFilterParamsComparison["="], + status: ListingsStatusEnum.active, + }, + ], + orderBy: [ListingOrderByKeys.mostRecentlyPublished], + orderDir: [OrderByEnum.desc], + }, + req + ) } -export async function fetchClosedListings() { - return await fetchBaseListingData({ - additionalFilters: [ - { - $comparison: EnumListingFilterParamsComparison["="], - status: ListingsStatusEnum.closed, - }, - ], - orderBy: [ListingOrderByKeys.mostRecentlyClosed], - orderDir: [OrderByEnum.desc], - limit: "10", - }) +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function fetchClosedListings(req: any) { + return await fetchBaseListingData( + { + additionalFilters: [ + { + $comparison: EnumListingFilterParamsComparison["="], + status: ListingsStatusEnum.closed, + }, + ], + orderBy: [ListingOrderByKeys.mostRecentlyClosed], + orderDir: [OrderByEnum.desc], + limit: "10", + }, + req + ) } let jurisdiction: Jurisdiction | null = null -export async function fetchJurisdictionByName() { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function fetchJurisdictionByName(req: any) { try { if (jurisdiction) { return jurisdiction @@ -159,7 +175,10 @@ export async function fetchJurisdictionByName() { const jurisdictionRes = await axios.get( `${process.env.backendApiBase}/jurisdictions/byName/${jurisdictionName}`, { - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": req.headers["x-forwarded-for"] ?? req.socket.remoteAddress, + }, } ) jurisdiction = jurisdictionRes?.data diff --git a/sites/public/src/pages/index.tsx b/sites/public/src/pages/index.tsx index e1163239c1..bd0c70a6f2 100644 --- a/sites/public/src/pages/index.tsx +++ b/sites/public/src/pages/index.tsx @@ -114,8 +114,9 @@ export default function Home(props: IndexProps) { ) } -export async function getStaticProps() { - const jurisdiction = await fetchJurisdictionByName() +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getStaticProps(context: { req: any }) { + const jurisdiction = await fetchJurisdictionByName(context.req) return { props: { jurisdiction }, diff --git a/sites/public/src/pages/listing/[id].tsx b/sites/public/src/pages/listing/[id].tsx index 19ea6da04a..9a1f4ab14b 100644 --- a/sites/public/src/pages/listing/[id].tsx +++ b/sites/public/src/pages/listing/[id].tsx @@ -9,12 +9,17 @@ export default function ListingRedirect(props: Record) { ) } -export async function getServerSideProps(context: { params: Record }) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getServerSideProps(context: { params: Record; req: any }) { let response try { response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, { - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": + context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress, + }, }) } catch (e) { return { notFound: true } diff --git a/sites/public/src/pages/listing/[id]/[slug].tsx b/sites/public/src/pages/listing/[id]/[slug].tsx index 8e61810677..c446ae7471 100644 --- a/sites/public/src/pages/listing/[id]/[slug].tsx +++ b/sites/public/src/pages/listing/[id]/[slug].tsx @@ -119,17 +119,23 @@ export default function ListingPage(props: ListingProps) { export async function getServerSideProps(context: { params: Record locale: string + // eslint-disable-next-line @typescript-eslint/no-explicit-any + req: any }) { let response - try { response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, { - headers: { language: context.locale, passkey: process.env.API_PASS_KEY }, + headers: { + language: context.locale, + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": + context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress, + }, }) } catch (e) { return { notFound: true } } - const jurisdiction = fetchJurisdictionByName() + const jurisdiction = fetchJurisdictionByName(context.req) return { props: { listing: response.data, jurisdiction: await jurisdiction } } } diff --git a/sites/public/src/pages/listings.tsx b/sites/public/src/pages/listings.tsx index 4c48cbe963..583c41dd3d 100644 --- a/sites/public/src/pages/listings.tsx +++ b/sites/public/src/pages/listings.tsx @@ -71,9 +71,10 @@ export default function ListingsPage(props: ListingsProps) { ) } -export async function getServerSideProps() { - const openListings = fetchOpenListings() - const closedListings = fetchClosedListings() +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getServerSideProps(context: { req: any }) { + const openListings = fetchOpenListings(context.req) + const closedListings = fetchClosedListings(context.req) return { props: { openListings: await openListings, closedListings: await closedListings }, diff --git a/sites/public/src/pages/preview/listings/[id].tsx b/sites/public/src/pages/preview/listings/[id].tsx index 6977f7c57c..d9b5d8c64d 100644 --- a/sites/public/src/pages/preview/listings/[id].tsx +++ b/sites/public/src/pages/preview/listings/[id].tsx @@ -44,18 +44,23 @@ export default function ListingPage(props: ListingProps) { ) } -export async function getServerSideProps(context: { params: Record }) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function getServerSideProps(context: { params: Record; req: any }) { let response try { response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, { - headers: { passkey: process.env.API_PASS_KEY }, + headers: { + passkey: process.env.API_PASS_KEY, + "x-forwarded-for": + context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress, + }, }) } catch (e) { return { notFound: true } } - const jurisdiction = fetchJurisdictionByName() + const jurisdiction = fetchJurisdictionByName(context.req) return { props: { listing: response.data, jurisdiction: await jurisdiction } } }