payload 3.80.0-internal.8d9ec7c → 3.80.0-internal.cdd7ef7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/auth/endpoints/forgotPassword.d.ts.map +1 -1
  2. package/dist/auth/endpoints/forgotPassword.js +0 -2
  3. package/dist/auth/endpoints/forgotPassword.js.map +1 -1
  4. package/dist/auth/extractJWT.d.ts.map +1 -1
  5. package/dist/auth/extractJWT.js +18 -4
  6. package/dist/auth/extractJWT.js.map +1 -1
  7. package/dist/auth/operations/forgotPassword.d.ts.map +1 -1
  8. package/dist/auth/operations/forgotPassword.js +5 -4
  9. package/dist/auth/operations/forgotPassword.js.map +1 -1
  10. package/dist/auth/sendVerificationEmail.d.ts.map +1 -1
  11. package/dist/auth/sendVerificationEmail.js +5 -4
  12. package/dist/auth/sendVerificationEmail.js.map +1 -1
  13. package/dist/database/getLocalizedPaths.d.ts.map +1 -1
  14. package/dist/database/getLocalizedPaths.js +2 -1
  15. package/dist/database/getLocalizedPaths.js.map +1 -1
  16. package/dist/database/queryValidation/validateQueryPaths.js +1 -1
  17. package/dist/database/queryValidation/validateQueryPaths.js.map +1 -1
  18. package/dist/database/queryValidation/validateSearchParams.d.ts.map +1 -1
  19. package/dist/database/queryValidation/validateSearchParams.js +2 -1
  20. package/dist/database/queryValidation/validateSearchParams.js.map +1 -1
  21. package/dist/exports/shared.d.ts +1 -0
  22. package/dist/exports/shared.d.ts.map +1 -1
  23. package/dist/exports/shared.js +1 -0
  24. package/dist/exports/shared.js.map +1 -1
  25. package/dist/fields/validations.js +1 -1
  26. package/dist/fields/validations.js.map +1 -1
  27. package/dist/fields/validations.spec.js +25 -0
  28. package/dist/fields/validations.spec.js.map +1 -1
  29. package/dist/hierarchy/getInitialTreeData.d.ts +3 -2
  30. package/dist/hierarchy/getInitialTreeData.d.ts.map +1 -1
  31. package/dist/hierarchy/getInitialTreeData.js +4 -5
  32. package/dist/hierarchy/getInitialTreeData.js.map +1 -1
  33. package/dist/index.bundled.d.ts +3 -2
  34. package/dist/types/constants.d.ts +5 -0
  35. package/dist/types/constants.d.ts.map +1 -1
  36. package/dist/types/constants.js +4 -0
  37. package/dist/types/constants.js.map +1 -1
  38. package/dist/uploads/endpoints/getFile.d.ts.map +1 -1
  39. package/dist/uploads/endpoints/getFile.js +7 -1
  40. package/dist/uploads/endpoints/getFile.js.map +1 -1
  41. package/dist/uploads/endpoints/getFileFromURL.d.ts.map +1 -1
  42. package/dist/uploads/endpoints/getFileFromURL.js +67 -28
  43. package/dist/uploads/endpoints/getFileFromURL.js.map +1 -1
  44. package/dist/uploads/getExternalFile.d.ts.map +1 -1
  45. package/dist/uploads/getExternalFile.js +3 -0
  46. package/dist/uploads/getExternalFile.js.map +1 -1
  47. package/dist/uploads/safeFetch.d.ts +1 -1
  48. package/dist/uploads/safeFetch.d.ts.map +1 -1
  49. package/dist/uploads/safeFetch.js.map +1 -1
  50. package/dist/utilities/getRequestOrigin.d.ts +10 -0
  51. package/dist/utilities/getRequestOrigin.d.ts.map +1 -0
  52. package/dist/utilities/getRequestOrigin.js +50 -0
  53. package/dist/utilities/getRequestOrigin.js.map +1 -0
  54. package/dist/utilities/getRequestOrigin.spec.js +151 -0
  55. package/dist/utilities/getRequestOrigin.spec.js.map +1 -0
  56. package/dist/utilities/sanitizeUrl.d.ts +7 -0
  57. package/dist/utilities/sanitizeUrl.d.ts.map +1 -0
  58. package/dist/utilities/sanitizeUrl.js +28 -0
  59. package/dist/utilities/sanitizeUrl.js.map +1 -0
  60. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hierarchy/getInitialTreeData.ts"],"sourcesContent":["import type { TypeWithID } from '../collections/config/types.js'\nimport type { PayloadRequest, Where } from '../types/index.js'\n\nimport { DEFAULT_HIERARCHY_TREE_LIMIT } from '../hierarchy/constants.js'\n\nexport type GetInitialTreeDataArgs = {\n /** Base filter to apply to all queries (e.g., tenant filter) */\n baseFilter?: null | Where\n collectionSlug: string\n expandedNodeIds?: (number | string)[]\n /** Filter tree to only show folders that allow these collection types (or are unrestricted) */\n filterByCollections?: string[]\n limit?: number\n req: PayloadRequest\n /** The currently selected node ID. When provided, ensures siblings are loaded to include this node. */\n selectedNodeId?: null | number | string\n /** The parent ID of the selected node. Required when selectedNodeId is provided. */\n selectedNodeParentId?: null | number | string\n}\n\nexport type InitialTreeData = {\n docs: TypeWithID[]\n // Metadata about what was loaded - keyed by parent ID ('null' for root)\n loadedParents: Record<string, { hasMore: boolean; loadedCount?: number; totalDocs: number }>\n}\n\nexport const getInitialTreeData = async ({\n baseFilter,\n collectionSlug,\n expandedNodeIds = [],\n filterByCollections,\n limit,\n req,\n selectedNodeId,\n selectedNodeParentId,\n}: GetInitialTreeDataArgs): Promise<InitialTreeData> => {\n const collectionConfig = req.payload.collections[collectionSlug]?.config\n\n if (!collectionConfig || !collectionConfig.hierarchy) {\n throw new Error(`Collection ${collectionSlug} is not a hierarchy`)\n }\n\n const hierarchyConfig = collectionConfig.hierarchy\n const parentFieldName = hierarchyConfig.parentFieldName\n const useAsTitle = collectionConfig.admin?.useAsTitle ?? 'id'\n\n // Get typeFieldName for filtering\n const typeFieldName =\n hierarchyConfig.collectionSpecific && typeof hierarchyConfig.collectionSpecific === 'object'\n ? hierarchyConfig.collectionSpecific.fieldName\n : undefined\n\n // Build filter condition if filterByCollections is provided\n // Exclude the hierarchy collection itself (folders always show folders)\n const filteredTypes = filterByCollections?.filter((t) => t !== collectionSlug)\n\n // Get all possible type values from relatedCollections for detecting empty arrays\n const allPossibleTypes = hierarchyConfig.relatedCollections\n ? Object.keys(hierarchyConfig.relatedCollections)\n : []\n\n const filterCondition =\n filteredTypes?.length && typeFieldName\n ? {\n or: [\n { [typeFieldName]: { in: filteredTypes } },\n { [typeFieldName]: { exists: false } }, // Include unrestricted folders (field doesn't exist)\n // Include unrestricted folders with empty allowedTypes array\n // Using not_in with all possible values matches empty arrays in both MongoDB and Postgres\n ...(allPossibleTypes.length > 0\n ? [{ [typeFieldName]: { not_in: allPossibleTypes } }]\n : []),\n ],\n }\n : null\n\n // Use limit from config if not provided, fallback to config's treeLimit\n const effectiveLimit = limit ?? hierarchyConfig.admin?.treeLimit ?? DEFAULT_HIERARCHY_TREE_LIMIT\n\n const allDocs: TypeWithID[] = []\n const loadedParents: Record<\n string,\n { hasMore: boolean; loadedCount: number; totalDocs: number }\n > = {}\n\n // Normalize selectedNodeParentId: treat null/undefined as 'null' (root level)\n const normalizedSelectedParentId =\n selectedNodeParentId === null || selectedNodeParentId === undefined\n ? 'null'\n : String(selectedNodeParentId)\n\n // Helper to check if selectedNodeId is among siblings at a given parent level\n const needsSelectedNodeIncluded = (parentKey: string): boolean => {\n return !!selectedNodeId && normalizedSelectedParentId === parentKey\n }\n\n // Helper to fetch children with optional selectedNodeId inclusion\n const fetchChildrenForParent = async (\n parentKey: string,\n parentCondition: Where,\n ): Promise<void> => {\n const mustIncludeSelected = needsSelectedNodeIncluded(parentKey)\n let accumulatedDocs: TypeWithID[] = []\n let currentPage = 1\n let hasMore = true\n let totalDocs = 0\n let foundSelected = false\n\n // Combine parent condition with filter condition and baseFilter\n const conditions: Where[] = [parentCondition]\n if (filterCondition) {\n conditions.push(filterCondition)\n }\n if (baseFilter) {\n conditions.push(baseFilter)\n }\n const whereClause = conditions.length > 1 ? { and: conditions } : parentCondition\n\n while (hasMore) {\n const result = await req.payload.find({\n collection: collectionSlug,\n depth: 0,\n limit: effectiveLimit,\n overrideAccess: false,\n page: currentPage,\n req,\n sort: useAsTitle,\n user: req.user,\n where: whereClause,\n })\n\n accumulatedDocs = [...accumulatedDocs, ...result.docs]\n totalDocs = result.totalDocs\n hasMore = result.hasNextPage\n\n // Check if selectedNodeId is in this page's results\n if (mustIncludeSelected && !foundSelected) {\n foundSelected = result.docs.some(\n (doc: TypeWithID) => String(doc.id) === String(selectedNodeId),\n )\n }\n\n // Stop if we've found the selected node OR we only need first page\n if (!mustIncludeSelected || foundSelected || !hasMore) {\n break\n }\n\n currentPage++\n }\n\n allDocs.push(...accumulatedDocs)\n loadedParents[parentKey] = {\n hasMore,\n loadedCount: accumulatedDocs.length,\n totalDocs,\n }\n }\n\n // Query 1: Fetch root nodes\n await fetchChildrenForParent('null', {\n [parentFieldName]: { exists: false },\n })\n\n // Query 2: For each expanded node, fetch its children\n for (const parentId of expandedNodeIds) {\n await fetchChildrenForParent(String(parentId), {\n [parentFieldName]: { equals: parentId },\n })\n }\n\n return {\n docs: allDocs,\n loadedParents,\n }\n}\n"],"names":["DEFAULT_HIERARCHY_TREE_LIMIT","getInitialTreeData","baseFilter","collectionSlug","expandedNodeIds","filterByCollections","limit","req","selectedNodeId","selectedNodeParentId","collectionConfig","payload","collections","config","hierarchy","Error","hierarchyConfig","parentFieldName","useAsTitle","admin","typeFieldName","collectionSpecific","fieldName","undefined","filteredTypes","filter","t","allPossibleTypes","relatedCollections","Object","keys","filterCondition","length","or","in","exists","not_in","effectiveLimit","treeLimit","allDocs","loadedParents","normalizedSelectedParentId","String","needsSelectedNodeIncluded","parentKey","fetchChildrenForParent","parentCondition","mustIncludeSelected","accumulatedDocs","currentPage","hasMore","totalDocs","foundSelected","conditions","push","whereClause","and","result","find","collection","depth","overrideAccess","page","sort","user","where","docs","hasNextPage","some","doc","id","loadedCount","parentId","equals"],"mappings":"AAGA,SAASA,4BAA4B,QAAQ,4BAA2B;AAuBxE,OAAO,MAAMC,qBAAqB,OAAO,EACvCC,UAAU,EACVC,cAAc,EACdC,kBAAkB,EAAE,EACpBC,mBAAmB,EACnBC,KAAK,EACLC,GAAG,EACHC,cAAc,EACdC,oBAAoB,EACG;IACvB,MAAMC,mBAAmBH,IAAII,OAAO,CAACC,WAAW,CAACT,eAAe,EAAEU;IAElE,IAAI,CAACH,oBAAoB,CAACA,iBAAiBI,SAAS,EAAE;QACpD,MAAM,IAAIC,MAAM,CAAC,WAAW,EAAEZ,eAAe,mBAAmB,CAAC;IACnE;IAEA,MAAMa,kBA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AAEwD,QAAQD;YAAS;QACxC;IACF;IAEA,OAAO;QACLN,MAAM3B;QACNC;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/hierarchy/getInitialTreeData.ts"],"sourcesContent":["import type { TypeWithID } from '../collections/config/types.js'\nimport type { PayloadRequest, Where } from '../types/index.js'\n\nimport { DEFAULT_HIERARCHY_TREE_LIMIT } from '../hierarchy/constants.js'\n\nexport type GetInitialTreeDataArgs = {\n /** Base filter to apply to all queries (e.g., tenant filter) */\n baseFilter?: null | Where\n collectionSlug: string\n expandedNodeIds?: (number | string)[]\n /** Filter tree to only show folders that allow these collection types (or are unrestricted) */\n filterByCollections?: string[]\n limit?: number\n payload: PayloadRequest['payload']\n /** The currently selected node ID. When provided, ensures siblings are loaded to include this node. */\n selectedNodeId?: null | number | string\n /** The parent ID of the selected node. Required when selectedNodeId is provided. */\n selectedNodeParentId?: null | number | string\n user: PayloadRequest['user']\n}\n\nexport type InitialTreeData = {\n docs: TypeWithID[]\n // Metadata about what was loaded - keyed by parent ID ('null' for root)\n loadedParents: Record<string, { hasMore: boolean; loadedCount?: number; totalDocs: number }>\n}\n\nexport const getInitialTreeData = async ({\n baseFilter,\n collectionSlug,\n expandedNodeIds = [],\n filterByCollections,\n limit,\n payload,\n selectedNodeId,\n selectedNodeParentId,\n user,\n}: GetInitialTreeDataArgs): Promise<InitialTreeData> => {\n const collectionConfig = payload.collections[collectionSlug]?.config\n\n if (!collectionConfig || !collectionConfig.hierarchy) {\n throw new Error(`Collection ${collectionSlug} is not a hierarchy`)\n }\n\n const hierarchyConfig = collectionConfig.hierarchy\n const parentFieldName = hierarchyConfig.parentFieldName\n const useAsTitle = collectionConfig.admin?.useAsTitle ?? 'id'\n\n // Get typeFieldName for filtering\n const typeFieldName =\n hierarchyConfig.collectionSpecific && typeof hierarchyConfig.collectionSpecific === 'object'\n ? hierarchyConfig.collectionSpecific.fieldName\n : undefined\n\n // Build filter condition if filterByCollections is provided\n // Exclude the hierarchy collection itself (folders always show folders)\n const filteredTypes = filterByCollections?.filter((t) => t !== collectionSlug)\n\n // Get all possible type values from relatedCollections for detecting empty arrays\n const allPossibleTypes = hierarchyConfig.relatedCollections\n ? Object.keys(hierarchyConfig.relatedCollections)\n : []\n\n const filterCondition =\n filteredTypes?.length && typeFieldName\n ? {\n or: [\n { [typeFieldName]: { in: filteredTypes } },\n { [typeFieldName]: { exists: false } }, // Include unrestricted folders (field doesn't exist)\n // Include unrestricted folders with empty allowedTypes array\n // Using not_in with all possible values matches empty arrays in both MongoDB and Postgres\n ...(allPossibleTypes.length > 0\n ? [{ [typeFieldName]: { not_in: allPossibleTypes } }]\n : []),\n ],\n }\n : null\n\n // Use limit from config if not provided, fallback to config's treeLimit\n const effectiveLimit = limit ?? hierarchyConfig.admin?.treeLimit ?? DEFAULT_HIERARCHY_TREE_LIMIT\n\n const allDocs: TypeWithID[] = []\n const loadedParents: Record<\n string,\n { hasMore: boolean; loadedCount: number; totalDocs: number }\n > = {}\n\n // Normalize selectedNodeParentId: treat null/undefined as 'null' (root level)\n const normalizedSelectedParentId =\n selectedNodeParentId === null || selectedNodeParentId === undefined\n ? 'null'\n : String(selectedNodeParentId)\n\n // Helper to check if selectedNodeId is among siblings at a given parent level\n const needsSelectedNodeIncluded = (parentKey: string): boolean => {\n return !!selectedNodeId && normalizedSelectedParentId === parentKey\n }\n\n // Helper to fetch children with optional selectedNodeId inclusion\n const fetchChildrenForParent = async (\n parentKey: string,\n parentCondition: Where,\n ): Promise<void> => {\n const mustIncludeSelected = needsSelectedNodeIncluded(parentKey)\n let accumulatedDocs: TypeWithID[] = []\n let currentPage = 1\n let hasMore = true\n let totalDocs = 0\n let foundSelected = false\n\n // Combine parent condition with filter condition and baseFilter\n const conditions: Where[] = [parentCondition]\n if (filterCondition) {\n conditions.push(filterCondition)\n }\n if (baseFilter) {\n conditions.push(baseFilter)\n }\n const whereClause = conditions.length > 1 ? { and: conditions } : parentCondition\n\n while (hasMore) {\n const result = await payload.find({\n collection: collectionSlug,\n depth: 0,\n limit: effectiveLimit,\n overrideAccess: false,\n page: currentPage,\n sort: useAsTitle,\n user,\n where: whereClause,\n })\n\n accumulatedDocs = [...accumulatedDocs, ...result.docs]\n totalDocs = result.totalDocs\n hasMore = result.hasNextPage\n\n // Check if selectedNodeId is in this page's results\n if (mustIncludeSelected && !foundSelected) {\n foundSelected = result.docs.some(\n (doc: TypeWithID) => String(doc.id) === String(selectedNodeId),\n )\n }\n\n // Stop if we've found the selected node OR we only need first page\n if (!mustIncludeSelected || foundSelected || !hasMore) {\n break\n }\n\n currentPage++\n }\n\n allDocs.push(...accumulatedDocs)\n loadedParents[parentKey] = {\n hasMore,\n loadedCount: accumulatedDocs.length,\n totalDocs,\n }\n }\n\n // Query 1: Fetch root nodes\n await fetchChildrenForParent('null', {\n [parentFieldName]: { exists: false },\n })\n\n // Query 2: For each expanded node, fetch its children\n for (const parentId of expandedNodeIds) {\n await fetchChildrenForParent(String(parentId), {\n [parentFieldName]: { equals: parentId },\n })\n }\n\n return {\n docs: allDocs,\n loadedParents,\n }\n}\n"],"names":["DEFAULT_HIERARCHY_TREE_LIMIT","getInitialTreeData","baseFilter","collectionSlug","expandedNodeIds","filterByCollections","limit","payload","selectedNodeId","selectedNodeParentId","user","collectionConfig","collections","config","hierarchy","Error","hierarchyConfig","parentFieldName","useAsTitle","admin","typeFieldName","collectionSpecific","fieldName","undefined","filteredTypes","filter","t","allPossibleTypes","relatedCollections","Object","keys","filterCondition","length","or","in","exists","not_in","effectiveLimit","treeLimit","allDocs","loadedParents","normalizedSelectedParentId","String","needsSelectedNodeIncluded","parentKey","fetchChildrenForParent","parentCondition","mustIncludeSelected","accumulatedDocs","currentPage","hasMore","totalDocs","foundSelected","conditions","push","whereClause","and","result","find","collection","depth","overrideAccess","page","sort","where","docs","hasNextPage","some","doc","id","loadedCount","parentId","equals"],"mappings":"AAGA,SAASA,4BAA4B,QAAQ,4BAA2B;AAwBxE,OAAO,MAAMC,qBAAqB,OAAO,EACvCC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GAAG;YACzBM;YACAoB,aAAatB,gBAAgBhB,MAAM;YACnCmB;QACF;IACF;IAEA,4BAA4B;IAC5B,MAAMN,uBAAuB,QAAQ;QACnC,CAAC5B,gBAAgB,EAAE;YAAEkB,QAAQ;QAAM;IACrC;IAEA,sDAAsD;IACtD,KAAK,MAAMoC,YAAYnE,gBAAiB;QACtC,MAAMyC,uBAAuBH,OAAO6B,WAAW;YAC7C,CAACtD,gBAAgB,EAAE;gBAAEuD,QAAQD;YAAS;QACxC;IACF;IAEA,OAAO;QACLN,MAAM1B;QACNC;IACF;AACF,EAAC"}
@@ -12729,11 +12729,12 @@ type GetInitialTreeDataArgs = {
12729
12729
  /** Filter tree to only show folders that allow these collection types (or are unrestricted) */
12730
12730
  filterByCollections?: string[];
12731
12731
  limit?: number;
12732
- req: PayloadRequest;
12732
+ payload: PayloadRequest['payload'];
12733
12733
  /** The currently selected node ID. When provided, ensures siblings are loaded to include this node. */
12734
12734
  selectedNodeId?: null | number | string;
12735
12735
  /** The parent ID of the selected node. Required when selectedNodeId is provided. */
12736
12736
  selectedNodeParentId?: null | number | string;
12737
+ user: PayloadRequest['user'];
12737
12738
  };
12738
12739
  type InitialTreeData = {
12739
12740
  docs: TypeWithID[];
@@ -12743,7 +12744,7 @@ type InitialTreeData = {
12743
12744
  totalDocs: number;
12744
12745
  }>;
12745
12746
  };
12746
- declare const getInitialTreeData: ({ baseFilter, collectionSlug, expandedNodeIds, filterByCollections, limit, req, selectedNodeId, selectedNodeParentId, }: GetInitialTreeDataArgs) => Promise<InitialTreeData>;
12747
+ declare const getInitialTreeData: ({ baseFilter, collectionSlug, expandedNodeIds, filterByCollections, limit, payload, selectedNodeId, selectedNodeParentId, user, }: GetInitialTreeDataArgs) => Promise<InitialTreeData>;
12747
12748
 
12748
12749
  /**
12749
12750
  * Injects the HierarchyButton component into a collection's BeforeDocumentMeta slot.
@@ -1,4 +1,9 @@
1
1
  export declare const validOperators: readonly ["equals", "contains", "not_equals", "in", "all", "not_in", "exists", "greater_than", "greater_than_equal", "less_than", "less_than_equal", "like", "not_like", "within", "intersects", "near"];
2
2
  export type Operator = (typeof validOperators)[number];
3
3
  export declare const validOperatorSet: Set<"all" | "contains" | "equals" | "exists" | "intersects" | "near" | "within" | "not_equals" | "in" | "not_in" | "greater_than" | "greater_than_equal" | "less_than" | "less_than_equal" | "like" | "not_like">;
4
+ /**
5
+ * Matches a dot-separated path where each segment is a word character (a-zA-Z0-9_).
6
+ * Used to validate field paths before they are processed by query builders.
7
+ */
8
+ export declare const SAFE_FIELD_PATH_REGEX: RegExp;
4
9
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/types/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,0MAiBjB,CAAA;AAEV,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAA;AAEtD,eAAO,MAAM,gBAAgB,mNAAoC,CAAA"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/types/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,0MAiBjB,CAAA;AAEV,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAA;AAEtD,eAAO,MAAM,gBAAgB,mNAAoC,CAAA;AAEjE;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QAAoB,CAAA"}
@@ -17,5 +17,9 @@ export const validOperators = [
17
17
  'near'
18
18
  ];
19
19
  export const validOperatorSet = new Set(validOperators);
20
+ /**
21
+ * Matches a dot-separated path where each segment is a word character (a-zA-Z0-9_).
22
+ * Used to validate field paths before they are processed by query builders.
23
+ */ export const SAFE_FIELD_PATH_REGEX = /^\w+(?:\.\w+)*$/;
20
24
 
21
25
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/types/constants.ts"],"sourcesContent":["export const validOperators = [\n 'equals',\n 'contains',\n 'not_equals',\n 'in',\n 'all',\n 'not_in',\n 'exists',\n 'greater_than',\n 'greater_than_equal',\n 'less_than',\n 'less_than_equal',\n 'like',\n 'not_like',\n 'within',\n 'intersects',\n 'near',\n] as const\n\nexport type Operator = (typeof validOperators)[number]\n\nexport const validOperatorSet = new Set<Operator>(validOperators)\n"],"names":["validOperators","validOperatorSet","Set"],"mappings":"AAAA,OAAO,MAAMA,iBAAiB;IAC5B;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAS;AAIV,OAAO,MAAMC,mBAAmB,IAAIC,IAAcF,gBAAe"}
1
+ {"version":3,"sources":["../../src/types/constants.ts"],"sourcesContent":["export const validOperators = [\n 'equals',\n 'contains',\n 'not_equals',\n 'in',\n 'all',\n 'not_in',\n 'exists',\n 'greater_than',\n 'greater_than_equal',\n 'less_than',\n 'less_than_equal',\n 'like',\n 'not_like',\n 'within',\n 'intersects',\n 'near',\n] as const\n\nexport type Operator = (typeof validOperators)[number]\n\nexport const validOperatorSet = new Set<Operator>(validOperators)\n\n/**\n * Matches a dot-separated path where each segment is a word character (a-zA-Z0-9_).\n * Used to validate field paths before they are processed by query builders.\n */\nexport const SAFE_FIELD_PATH_REGEX = /^\\w+(?:\\.\\w+)*$/\n"],"names":["validOperators","validOperatorSet","Set","SAFE_FIELD_PATH_REGEX"],"mappings":"AAAA,OAAO,MAAMA,iBAAiB;IAC5B;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAS;AAIV,OAAO,MAAMC,mBAAmB,IAAIC,IAAcF,gBAAe;AAEjE;;;CAGC,GACD,OAAO,MAAMG,wBAAwB,kBAAiB"}
@@ -1 +1 @@
1
- {"version":3,"file":"getFile.d.ts","sourceRoot":"","sources":["../../../src/uploads/endpoints/getFile.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAU3D,eAAO,MAAM,cAAc,EAAE,cAkJ5B,CAAA"}
1
+ {"version":3,"file":"getFile.d.ts","sourceRoot":"","sources":["../../../src/uploads/endpoints/getFile.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAU3D,eAAO,MAAM,cAAc,EAAE,cA0J5B,CAAA"}
@@ -43,8 +43,14 @@ export const getFileHandler = async (req)=>{
43
43
  return customResponse;
44
44
  }
45
45
  }
46
+ // Local filesystem fallback — cloud storage handlers return a Response above
47
+ // and have their own filename validation via sanitizeFilename.
46
48
  const fileDir = collection.config.upload?.staticDir || collection.config.slug;
47
- const filePath = path.resolve(`${fileDir}/${filename}`);
49
+ const resolvedDir = path.resolve(fileDir);
50
+ const filePath = path.resolve(resolvedDir, filename);
51
+ if (!filePath.startsWith(resolvedDir + path.sep)) {
52
+ throw new APIError('Invalid filename.', httpStatus.BAD_REQUEST);
53
+ }
48
54
  let stats;
49
55
  try {
50
56
  stats = await fsPromises.stat(filePath);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/uploads/endpoints/getFile.ts"],"sourcesContent":["import type { Stats } from 'fs'\n\nimport { fileTypeFromFile } from 'file-type'\nimport fsPromises from 'fs/promises'\nimport { status as httpStatus } from 'http-status'\nimport path from 'path'\n\nimport type { PayloadHandler } from '../../config/types.js'\n\nimport { APIError } from '../../errors/APIError.js'\nimport { checkFileAccess } from '../../uploads/checkFileAccess.js'\nimport { streamFile } from '../../uploads/fetchAPI-stream-file/index.js'\nimport { getFileTypeFallback } from '../../uploads/getFileTypeFallback.js'\nimport { parseRangeHeader } from '../../uploads/parseRangeHeader.js'\nimport { getRequestCollection } from '../../utilities/getRequestEntity.js'\nimport { headersWithCors } from '../../utilities/headersWithCors.js'\n\nexport const getFileHandler: PayloadHandler = async (req) => {\n const collection = getRequestCollection(req)\n\n const filename = req.routeParams?.filename as string\n\n if (!collection.config.upload) {\n throw new APIError(\n `This collection is not an upload collection: ${collection.config.slug}`,\n httpStatus.BAD_REQUEST,\n )\n }\n\n const accessResult = (await checkFileAccess({\n collection,\n filename,\n req,\n }))!\n\n if (accessResult instanceof Response) {\n return accessResult\n }\n\n if (collection.config.upload.handlers?.length) {\n let customResponse: null | Response | void = null\n const headers = new Headers()\n\n for (const handler of collection.config.upload.handlers) {\n customResponse = await handler(req, {\n doc: accessResult,\n headers,\n params: {\n collection: collection.config.slug,\n filename,\n },\n })\n if (customResponse && customResponse instanceof Response) {\n break\n }\n }\n\n if (customResponse instanceof Response) {\n return customResponse\n }\n }\n\n const fileDir = collection.config.upload?.staticDir || collection.config.slug\n const filePath = path.resolve(`${fileDir}/${filename}`)\n let stats: Stats\n\n try {\n stats = await fsPromises.stat(filePath)\n } catch (err) {\n if ((err as { code?: string }).code === 'ENOENT') {\n req.payload.logger.error(\n `File ${filename} for collection ${collection.config.slug} is missing on the disk. Expected path: ${filePath}`,\n )\n\n // Omit going to the routeError handler by returning response instead of\n // throwing an error to cut down log noise. The response still matches what you get with APIError to not leak details to the user.\n return Response.json(\n {\n errors: [\n {\n message: 'Something went wrong.',\n },\n ],\n },\n {\n headers: headersWithCors({\n headers: new Headers(),\n req,\n }),\n status: 500,\n },\n )\n }\n\n throw err\n }\n\n const fileTypeResult = (await fileTypeFromFile(filePath)) || getFileTypeFallback(filePath)\n let mimeType = fileTypeResult.mime\n\n if (filePath.endsWith('.svg') && fileTypeResult.mime === 'application/xml') {\n mimeType = 'image/svg+xml'\n }\n\n // Parse Range header for byte range requests\n const rangeHeader = req.headers.get('range')\n const rangeResult = parseRangeHeader({\n fileSize: stats.size,\n rangeHeader,\n })\n\n if (rangeResult.type === 'invalid') {\n let headers = new Headers()\n headers.set('Content-Range', `bytes */${stats.size}`)\n headers = collection.config.upload?.modifyResponseHeaders\n ? collection.config.upload.modifyResponseHeaders({ headers }) || headers\n : headers\n\n return new Response(null, {\n headers: headersWithCors({\n headers,\n req,\n }),\n status: httpStatus.REQUESTED_RANGE_NOT_SATISFIABLE,\n })\n }\n\n let headers = new Headers()\n headers.set('Content-Type', mimeType)\n headers.set('Accept-Ranges', 'bytes')\n\n if (mimeType === 'image/svg+xml') {\n headers.set('Content-Security-Policy', \"script-src 'none'\")\n }\n\n let data: ReadableStream\n let status: number\n const isPartial = rangeResult.type === 'partial'\n const range = rangeResult.range\n\n if (isPartial && range) {\n const contentLength = range.end - range.start + 1\n headers.set('Content-Length', String(contentLength))\n headers.set('Content-Range', `bytes ${range.start}-${range.end}/${stats.size}`)\n data = streamFile({ filePath, options: { end: range.end, start: range.start } })\n status = httpStatus.PARTIAL_CONTENT\n } else {\n headers.set('Content-Length', String(stats.size))\n data = streamFile({ filePath })\n status = httpStatus.OK\n }\n\n headers = collection.config.upload?.modifyResponseHeaders\n ? collection.config.upload.modifyResponseHeaders({ headers }) || headers\n : headers\n\n return new Response(data, {\n headers: headersWithCors({\n headers,\n req,\n }),\n status,\n })\n}\n"],"names":["fileTypeFromFile","fsPromises","status","httpStatus","path","APIError","checkFileAccess","streamFile","getFileTypeFallback","parseRangeHeader","getRequestCollection","headersWithCors","getFileHandler","req","collection","filename","routeParams","config","upload","slug","BAD_REQUEST","accessResult","Response","handlers","length","customResponse","headers","Headers","handler","doc","params","fileDir","staticDir","filePath","resolve","stats","stat","err","code","payload","logger","error","json","errors","message","fileTypeResult","mimeType","mime","endsWith","rangeHeader","get","rangeResult","fileSize","size","type","set","modifyResponseHeaders","REQUESTED_RANGE_NOT_SATISFIABLE","data","isPartial","range","contentLength","end","start","String","options","PARTIAL_CONTENT","OK"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,YAAW;AAC5C,OAAOC,gBAAgB,cAAa;AACpC,SAASC,UAAUC,UAAU,QAAQ,cAAa;AAClD,OAAOC,UAAU,OAAM;AAIvB,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,eAAe,QAAQ,mCAAkC;AAClE,SAASC,UAAU,QAAQ,8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E7B,IAAIT,aAAa,iBAAiB;QAChCpB,QAAQ6B,GAAG,CAAC,2BAA2B;IACzC;IAEA,IAAIG;IACJ,IAAIxD;IACJ,MAAMyD,YAAYR,YAAYG,IAAI,KAAK;IACvC,MAAMM,QAAQT,YAAYS,KAAK;IAE/B,IAAID,aAAaC,OAAO;QACtB,MAAMC,gBAAgBD,MAAME,GAAG,GAAGF,MAAMG,KAAK,GAAG;QAChDrC,QAAQ6B,GAAG,CAAC,kBAAkBS,OAAOH;QACrCnC,QAAQ6B,GAAG,CAAC,iBAAiB,CAAC,MAAM,EAAEK,MAAMG,KAAK,CAAC,CAAC,EAAEH,MAAME,GAAG,CAAC,CAAC,EAAE3B,MAAMkB,IAAI,EAAE;QAC9EK,OAAOnD,WAAW;YAAE0B;YAAUgC,SAAS;gBAAEH,KAAKF,MAAME,GAAG;gBAAEC,OAAOH,MAAMG,KAAK;YAAC;QAAE;QAC9E7D,SAASC,WAAW+D,eAAe;IACrC,OAAO;QACLxC,QAAQ6B,GAAG,CAAC,kBAAkBS,OAAO7B,MAAMkB,IAAI;QAC/CK,OAAOnD,WAAW;YAAE0B;QAAS;QAC7B/B,SAASC,WAAWgE,EAAE;IACxB;IAEAzC,UAAUZ,WAAWG,MAAM,CAACC,MAAM,EAAEsC,wBAChC1C,WAAWG,MAAM,CAACC,MAAM,CAACsC,qBAAqB,CAAC;QAAE9B;IAAQ,MAAMA,UAC/DA;IAEJ,OAAO,IAAIJ,SAASoC,MAAM;QACxBhC,SAASf,gBAAgB;YACvBe;YACAb;QACF;QACAX;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../src/uploads/endpoints/getFile.ts"],"sourcesContent":["import type { Stats } from 'fs'\n\nimport { fileTypeFromFile } from 'file-type'\nimport fsPromises from 'fs/promises'\nimport { status as httpStatus } from 'http-status'\nimport path from 'path'\n\nimport type { PayloadHandler } from '../../config/types.js'\n\nimport { APIError } from '../../errors/APIError.js'\nimport { checkFileAccess } from '../../uploads/checkFileAccess.js'\nimport { streamFile } from '../../uploads/fetchAPI-stream-file/index.js'\nimport { getFileTypeFallback } from '../../uploads/getFileTypeFallback.js'\nimport { parseRangeHeader } from '../../uploads/parseRangeHeader.js'\nimport { getRequestCollection } from '../../utilities/getRequestEntity.js'\nimport { headersWithCors } from '../../utilities/headersWithCors.js'\n\nexport const getFileHandler: PayloadHandler = async (req) => {\n const collection = getRequestCollection(req)\n\n const filename = req.routeParams?.filename as string\n\n if (!collection.config.upload) {\n throw new APIError(\n `This collection is not an upload collection: ${collection.config.slug}`,\n httpStatus.BAD_REQUEST,\n )\n }\n\n const accessResult = (await checkFileAccess({\n collection,\n filename,\n req,\n }))!\n\n if (accessResult instanceof Response) {\n return accessResult\n }\n\n if (collection.config.upload.handlers?.length) {\n let customResponse: null | Response | void = null\n const headers = new Headers()\n\n for (const handler of collection.config.upload.handlers) {\n customResponse = await handler(req, {\n doc: accessResult,\n headers,\n params: {\n collection: collection.config.slug,\n filename,\n },\n })\n if (customResponse && customResponse instanceof Response) {\n break\n }\n }\n\n if (customResponse instanceof Response) {\n return customResponse\n }\n }\n\n // Local filesystem fallback — cloud storage handlers return a Response above\n // and have their own filename validation via sanitizeFilename.\n const fileDir = collection.config.upload?.staticDir || collection.config.slug\n const resolvedDir = path.resolve(fileDir)\n const filePath = path.resolve(resolvedDir, filename)\n\n if (!filePath.startsWith(resolvedDir + path.sep)) {\n throw new APIError('Invalid filename.', httpStatus.BAD_REQUEST)\n }\n\n let stats: Stats\n\n try {\n stats = await fsPromises.stat(filePath)\n } catch (err) {\n if ((err as { code?: string }).code === 'ENOENT') {\n req.payload.logger.error(\n `File ${filename} for collection ${collection.config.slug} is missing on the disk. Expected path: ${filePath}`,\n )\n\n // Omit going to the routeError handler by returning response instead of\n // throwing an error to cut down log noise. The response still matches what you get with APIError to not leak details to the user.\n return Response.json(\n {\n errors: [\n {\n message: 'Something went wrong.',\n },\n ],\n },\n {\n headers: headersWithCors({\n headers: new Headers(),\n req,\n }),\n status: 500,\n },\n )\n }\n\n throw err\n }\n\n const fileTypeResult = (await fileTypeFromFile(filePath)) || getFileTypeFallback(filePath)\n let mimeType = fileTypeResult.mime\n\n if (filePath.endsWith('.svg') && fileTypeResult.mime === 'application/xml') {\n mimeType = 'image/svg+xml'\n }\n\n // Parse Range header for byte range requests\n const rangeHeader = req.headers.get('range')\n const rangeResult = parseRangeHeader({\n fileSize: stats.size,\n rangeHeader,\n })\n\n if (rangeResult.type === 'invalid') {\n let headers = new Headers()\n headers.set('Content-Range', `bytes */${stats.size}`)\n headers = collection.config.upload?.modifyResponseHeaders\n ? collection.config.upload.modifyResponseHeaders({ headers }) || headers\n : headers\n\n return new Response(null, {\n headers: headersWithCors({\n headers,\n req,\n }),\n status: httpStatus.REQUESTED_RANGE_NOT_SATISFIABLE,\n })\n }\n\n let headers = new Headers()\n headers.set('Content-Type', mimeType)\n headers.set('Accept-Ranges', 'bytes')\n\n if (mimeType === 'image/svg+xml') {\n headers.set('Content-Security-Policy', \"script-src 'none'\")\n }\n\n let data: ReadableStream\n let status: number\n const isPartial = rangeResult.type === 'partial'\n const range = rangeResult.range\n\n if (isPartial && range) {\n const contentLength = range.end - range.start + 1\n headers.set('Content-Length', String(contentLength))\n headers.set('Content-Range', `bytes ${range.start}-${range.end}/${stats.size}`)\n data = streamFile({ filePath, options: { end: range.end, start: range.start } })\n status = httpStatus.PARTIAL_CONTENT\n } else {\n headers.set('Content-Length', String(stats.size))\n data = streamFile({ filePath })\n status = httpStatus.OK\n }\n\n headers = collection.config.upload?.modifyResponseHeaders\n ? collection.config.upload.modifyResponseHeaders({ headers }) || headers\n : headers\n\n return new Response(data, {\n headers: headersWithCors({\n headers,\n req,\n }),\n status,\n })\n}\n"],"names":["fileTypeFromFile","fsPromises","status","httpStatus","path","APIError","checkFileAccess","streamFile","getFileTypeFallback","parseRangeHeader","getRequestCollection","headersWithCors","getFileHandler","req","collection","filename","routeParams","config","upload","slug","BAD_REQUEST","accessResult","Response","handlers","length","customResponse","headers","Headers","handler","doc","params","fileDir","staticDir","resolvedDir","resolve","filePath","startsWith","sep","stats","stat","err","code","payload","logger","error","json","errors","message","fileTypeResult","mimeType","mime","endsWith","rangeHeader","get","rangeResult","fileSize","size","type","set","modifyResponseHeaders","REQUESTED_RANGE_NOT_SATISFIABLE","data","isPartial","range","contentLength","end","start","String","options","PARTIAL_CONTENT","OK"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,YAAW;AAC5C,OAAOC,gBAAgB,cAAa;AACpC,SAASC,UAAUC,UAAU,QAAQ,cAAa;AAClD,OAAOC,UAAU,OAAM;AAIvB,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,eAAe,QAAQ,mCAAkC;AAClE,SAASC,UAAU,QAAQ,8CAA6C;AACxE,SAASC,mBAAmB,QAAQ,uCAAsC;AAC1E,SAASC,gBAAgB,QAAQ,oCAAmC;AACpE,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,eAAe,QAAQ,qCAAoC;AAEpE,OAAO,MAAMC,iBAAiC,OAAOC;IACnD,MAAMC,aAAaJ,qBAAqBG;IAExC,MAAME,WAAWF,IAAIG,WAAW,EAAED;IAElC,IAAI,CAACD,WAAWG,MAAM,CAACC,MAAM,EAAE;QAC7B,MAAM,IAAIb,SACR,CAAC,6CAA6C,EAAES,WAAWG,MAAM,CAACE,IAAI,EAAE,EACxEhB,WAAWiB,WAAW;IAE1B;IAEA,MAAMC,eAAgB,MAAMf,gBAAgB;QAC1CQ;QACAC;QACAF;IACF;IAEA,IAAIQ,wBAAwBC,UAAU;QACpC,OAAOD;IACT;IAEA,IAAIP,WAAWG,MAAM,CAACC,MAAM,CAACK,QAAQ,EAAEC,QAAQ;QAC7C,IAAIC,iBAAyC;QAC7C,MAAMC,UAAU,IAAIC;QAEpB,KAAK,MAAMC,WAAWd,WAAWG,MAAM,CAACC,MAAM,CAACK,QAAQ,CAAE;YACvDE,iBAAiB,MAAMG,QAAQf,KAAK;gBAClCgB,KAAKR;gBACLK;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AAO;QACL3C,QAAQgC,GAAG,CAAC,kBAAkBS,OAAO7B,MAAMkB,IAAI;QAC/CK,OAAOtD,WAAW;YAAE4B;QAAS;QAC7BjC,SAASC,WAAWmE,EAAE;IACxB;IAEA5C,UAAUZ,WAAWG,MAAM,CAACC,MAAM,EAAEyC,wBAChC7C,WAAWG,MAAM,CAACC,MAAM,CAACyC,qBAAqB,CAAC;QAAEjC;IAAQ,MAAMA,UAC/DA;IAEJ,OAAO,IAAIJ,SAASuC,MAAM;QACxBnC,SAASf,gBAAgB;YACvBe;YACAb;QACF;QACAX;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getFileFromURL.d.ts","sourceRoot":"","sources":["../../../src/uploads/endpoints/getFileFromURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAc3D,eAAO,MAAM,qBAAqB,EAAE,cAsEnC,CAAA"}
1
+ {"version":3,"file":"getFileFromURL.d.ts","sourceRoot":"","sources":["../../../src/uploads/endpoints/getFileFromURL.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAgB3D,eAAO,MAAM,qBAAqB,EAAE,cA+GnC,CAAA"}
@@ -3,6 +3,8 @@ import { APIError } from '../../errors/APIError.js';
3
3
  import { Forbidden } from '../../errors/Forbidden.js';
4
4
  import { getRequestCollectionWithID } from '../../utilities/getRequestEntity.js';
5
5
  import { isURLAllowed } from '../../utilities/isURLAllowed.js';
6
+ import { sanitizeFilename } from '../../utilities/sanitizeFilename.js';
7
+ import { safeFetch } from '../safeFetch.js';
6
8
  // If doc id is provided, it means we are updating the doc
7
9
  // /:collectionSlug/paste-url/:doc-id?src=:fileUrl
8
10
  // If doc id is not provided, it means we are creating a new doc
@@ -35,39 +37,76 @@ export const getFileFromURLHandler = async (req)=>{
35
37
  throw new Forbidden(req.t);
36
38
  }
37
39
  }
40
+ if (!req.url) {
41
+ throw new APIError('Request URL is missing.', 400);
42
+ }
43
+ const { searchParams } = new URL(req.url);
44
+ const src = searchParams.get('src');
45
+ if (!src || typeof src !== 'string') {
46
+ throw new APIError('A valid URL string is required.', 400);
47
+ }
48
+ const hasAllowList = typeof config.upload.pasteURL === 'object' && Array.isArray(config.upload.pasteURL.allowList);
49
+ let fileURL;
38
50
  try {
39
- if (!req.url) {
40
- throw new APIError('Request URL is missing.', 400);
41
- }
42
- const { searchParams } = new URL(req.url);
43
- const src = searchParams.get('src');
44
- if (!src || typeof src !== 'string') {
45
- throw new APIError('A valid URL string is required.', 400);
46
- }
47
- const validatedUrl = new URL(src);
48
- if (!isURLAllowed(validatedUrl.href, config.upload.pasteURL.allowList)) {
49
- throw new APIError(`The provided URL (${validatedUrl.href}) is not allowed.`, 400);
51
+ fileURL = new URL(src).href;
52
+ } catch {
53
+ throw new APIError('A valid URL string is required.', 400);
54
+ }
55
+ if (hasAllowList && !isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {
56
+ throw new APIError('The provided URL is not allowed.', 400);
57
+ }
58
+ let redirectCount = 0;
59
+ const maxRedirects = 3;
60
+ let response;
61
+ while(true){
62
+ if (hasAllowList && isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {
63
+ // Allow-listed URLs bypass SSRF filtering (e.g. internal/localhost CDNs)
64
+ response = await fetch(fileURL, {
65
+ headers: {
66
+ 'Accept-Encoding': 'identity'
67
+ },
68
+ redirect: 'manual',
69
+ signal: AbortSignal.timeout(30_000)
70
+ });
71
+ } else {
72
+ response = await safeFetch(fileURL, {
73
+ headers: {
74
+ 'Accept-Encoding': 'identity'
75
+ },
76
+ signal: AbortSignal.timeout(30_000)
77
+ });
50
78
  }
51
- // Fetch the file with no compression
52
- const response = await fetch(validatedUrl.href, {
53
- headers: {
54
- 'Accept-Encoding': 'identity'
79
+ if (response.status >= 300 && response.status < 400) {
80
+ redirectCount++;
81
+ if (redirectCount > maxRedirects) {
82
+ throw new APIError('Too many redirects.', 403);
55
83
  }
56
- });
57
- if (!response.ok) {
58
- throw new APIError(`Failed to fetch file from ${validatedUrl.href}`, response.status);
59
- }
60
- const decodedFileName = decodeURIComponent(validatedUrl.pathname.split('/').pop() || '');
61
- return new Response(response.body, {
62
- headers: {
63
- 'Content-Disposition': `attachment; filename="${decodedFileName}"`,
64
- 'Content-Length': response.headers.get('content-length') || '',
65
- 'Content-Type': response.headers.get('content-type') || 'application/octet-stream'
84
+ const location = response.headers.get('location');
85
+ if (location) {
86
+ fileURL = new URL(location, fileURL).href;
87
+ if (hasAllowList && !isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {
88
+ throw new APIError('The provided URL is not allowed.', 400);
89
+ }
90
+ continue;
66
91
  }
67
- });
68
- } catch (err) {
69
- throw new APIError(`Error fetching file: ${err instanceof Error ? err.message : 'Unknown error'}`, 500);
92
+ }
93
+ break;
94
+ }
95
+ if (!response.ok) {
96
+ throw new APIError('Failed to fetch the file from the provided URL.', response.status);
70
97
  }
98
+ const rawFileName = decodeURIComponent(new URL(fileURL).pathname.split('/').pop() || '');
99
+ const safeFileName = sanitizeFilename(rawFileName);
100
+ const encodedFileName = encodeURIComponent(safeFileName).replace(/['()]/g, (c)=>`%${c.charCodeAt(0).toString(16).toUpperCase()}`);
101
+ // Strip quotes, backslashes, and control chars from the ASCII fallback
102
+ const asciiFileName = safeFileName.replace(/["\\\r\n]/g, '_');
103
+ return new Response(response.body, {
104
+ headers: {
105
+ 'Content-Disposition': `attachment; filename="${asciiFileName}"; filename*=UTF-8''${encodedFileName}`,
106
+ 'Content-Length': response.headers.get('content-length') || '',
107
+ 'Content-Type': response.headers.get('content-type') || 'application/octet-stream'
108
+ }
109
+ });
71
110
  };
72
111
 
73
112
  //# sourceMappingURL=getFileFromURL.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/uploads/endpoints/getFileFromURL.ts"],"sourcesContent":["import type { PayloadHandler } from '../../config/types.js'\n\nimport { executeAccess } from '../../auth/executeAccess.js'\nimport { APIError } from '../../errors/APIError.js'\nimport { Forbidden } from '../../errors/Forbidden.js'\nimport { getRequestCollectionWithID } from '../../utilities/getRequestEntity.js'\nimport { isURLAllowed } from '../../utilities/isURLAllowed.js'\n\n// If doc id is provided, it means we are updating the doc\n// /:collectionSlug/paste-url/:doc-id?src=:fileUrl\n\n// If doc id is not provided, it means we are creating a new doc\n// /:collectionSlug/paste-url?src=:fileUrl\n\nexport const getFileFromURLHandler: PayloadHandler = async (req) => {\n const { id, collection } = getRequestCollectionWithID(req, { optionalID: true })\n\n if (!req.user) {\n throw new Forbidden(req.t)\n }\n\n const config = collection?.config\n\n if (!config.upload?.pasteURL) {\n throw new APIError('Pasting from URL is not enabled for this collection.', 400)\n }\n\n if (id) {\n // updating doc\n const accessResult = await executeAccess({ req }, config.access.update)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n } else {\n // creating doc\n const accessResult = await executeAccess({ req }, config.access?.create)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n }\n try {\n if (!req.url) {\n throw new APIError('Request URL is missing.', 400)\n }\n\n const { searchParams } = new URL(req.url)\n const src = searchParams.get('src')\n\n if (!src || typeof src !== 'string') {\n throw new APIError('A valid URL string is required.', 400)\n }\n\n const validatedUrl = new URL(src)\n\n if (!isURLAllowed(validatedUrl.href, config.upload.pasteURL.allowList)) {\n throw new APIError(`The provided URL (${validatedUrl.href}) is not allowed.`, 400)\n }\n\n // Fetch the file with no compression\n const response = await fetch(validatedUrl.href, {\n headers: {\n 'Accept-Encoding': 'identity',\n },\n })\n\n if (!response.ok) {\n throw new APIError(`Failed to fetch file from ${validatedUrl.href}`, response.status)\n }\n\n const decodedFileName = decodeURIComponent(validatedUrl.pathname.split('/').pop() || '')\n\n return new Response(response.body, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${decodedFileName}\"`,\n 'Content-Length': response.headers.get('content-length') || '',\n 'Content-Type': response.headers.get('content-type') || 'application/octet-stream',\n },\n })\n } catch (err) {\n throw new APIError(\n `Error fetching file: ${err instanceof Error ? err.message : 'Unknown error'}`,\n 500,\n )\n }\n}\n"],"names":["executeAccess","APIError","Forbidden","getRequestCollectionWithID","isURLAllowed","getFileFromURLHandler","req","id","collection","optionalID","user","t","config","upload","pasteURL","accessResult","access","update","create","url","searchParams","URL","src","get","validatedUrl","href","allowList","response","fetch","headers","ok","status","decodedFileName","decodeURIComponent","pathname","split","pop","Response","body","err","Error","message"],"mappings":"AAEA,SAASA,aAAa,QAAQ,8BAA6B;AAC3D,SAASC,QAAQ,QAAQ,2BAA0B;AACnD,SAASC,SAAS,QAAQ,4BAA2B;AACrD,SAASC,0BAA0B,QAAQ,sCAAqC;AAChF,SAASC,YAAY,QAAQ,kCAAiC;AAE9D,0DAA0D;AAC1D,kDAAkD;AAElD,gEAAgE;AAChE,0CAA0C;AAE1C,OAAO,MAAMC,wBAAwC,OAAOC;IAC1D,MAAM,EAAEC,EAAE,EAAEC,UAAU,EAAE,GAAGL,2BAA2BG,KAAK;QAAEG,YAAY;IAAK;IAE9E,IAAI,CAACH,IAAII,IAAI,EAAE;QACb,MAAM,IAAIR,UAAUI,IAAIK,CAAC;IAC3B;IAEA,MAAMC,SAASJ,YAAYI;IAE3B,IAAI,CAACA,OAAOC,MAAM,EAAEC,UAAU;QAC5B,MAAM,IAAIb,SAAS,wDAAwD;IAC7E;IAEA,IAAIM,IAAI;QACN,eAAe;QACf,MAAMQ,eAAe,MAAMf,cAAc;YAAEM;QAAI,GAAGM,OAAOI,MAAM,CAACC,MAAM;QACtE,IAAI,CAACF,cAAc;YACjB,MAAM,IAAIb,UAAUI,IAAIK,CAAC;QAC3B;IACF,OAAO;QACL,eAAe;QACf,MAAMI,eAAe,MAAMf,cAAc;YAAEM;QAAI,GAAGM,OAAOI,MAAM,EAAEE;QACjE,IAAI,CAACH,cAAc;YACjB,MAAM,IAAIb,UAAUI,IAAIK,CAAC;QAC3B;IACF;IACA,IAAI;QACF,IAAI,CAACL,IAAIa,GAAG,EAAE;YACZ,MAAM,IAAIlB,SAAS,2BAA2B;QAChD;QAEA,MAAM,EAAEmB,YAAY,EAAE,GAAG,IAAIC,IAAIf,IAAIa,GAAG;QACxC,MAAMG,MAAMF,aAAaG,GAAG,CAAC;QAE7B,IAAI,CAACD,OAAO,OAAOA,QAAQ,UAAU;YACnC,MAAM,IAAIrB,SAAS,mCAAmC;QACxD;QAEA,MAAMuB,eAAe,IAAIH,IAAIC;QAE7B,IAAI,CAAClB,aAAaoB,aAAaC,IAAI,EAAEb,OAAOC,MAAM,CAACC,QAAQ,CAACY,SAAS,GAAG;YACtE,MAAM,IAAIzB,SAAS,CAAC,kBAAkB,EAAEuB,aAAaC,IAAI,CAAC,iBAAiB,CAAC,EAAE;QAChF;QAEA,qCAAqC;QACrC,MAAME,WAAW,MAAMC,MAAMJ,aAAaC,IAAI,EAAE;YAC9CI,SAAS;gBACP,mBAAmB;YACrB;QACF;QAEA,IAAI,CAACF,SAASG,EAAE,EAAE;YAChB,MAAM,IAAI7B,SAAS,CAAC,0BAA0B,EAAEuB,aAAaC,IAAI,EAAE,EAAEE,SAASI,MAAM;QACtF;QAEA,MAAMC,kBAAkBC,mBAAmBT,aAAaU,QAAQ,CAACC,KAAK,CAAC,KAAKC,GAAG,MAAM;QAErF,OAAO,IAAIC,SAASV,SAASW,IAAI,EAAE;YACjCT,SAAS;gBACP,uBAAuB,CAAC,sBAAsB,EAAEG,gBAAgB,CAAC,CAAC;gBAClE,kBAAkBL,SAASE,OAAO,CAACN,GAAG,CAAC,qBAAqB;gBAC5D,gBAAgBI,SAASE,OAAO,CAACN,GAAG,CAAC,mBAAmB;YAC1D;QACF;IACF,EAAE,OAAOgB,KAAK;QACZ,MAAM,IAAItC,SACR,CAAC,qBAAqB,EAAEsC,eAAeC,QAAQD,IAAIE,OAAO,GAAG,iBAAiB,EAC9E;IAEJ;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../src/uploads/endpoints/getFileFromURL.ts"],"sourcesContent":["import type { PayloadHandler } from '../../config/types.js'\n\nimport { executeAccess } from '../../auth/executeAccess.js'\nimport { APIError } from '../../errors/APIError.js'\nimport { Forbidden } from '../../errors/Forbidden.js'\nimport { getRequestCollectionWithID } from '../../utilities/getRequestEntity.js'\nimport { isURLAllowed } from '../../utilities/isURLAllowed.js'\nimport { sanitizeFilename } from '../../utilities/sanitizeFilename.js'\nimport { safeFetch } from '../safeFetch.js'\n\n// If doc id is provided, it means we are updating the doc\n// /:collectionSlug/paste-url/:doc-id?src=:fileUrl\n\n// If doc id is not provided, it means we are creating a new doc\n// /:collectionSlug/paste-url?src=:fileUrl\n\nexport const getFileFromURLHandler: PayloadHandler = async (req) => {\n const { id, collection } = getRequestCollectionWithID(req, { optionalID: true })\n\n if (!req.user) {\n throw new Forbidden(req.t)\n }\n\n const config = collection?.config\n\n if (!config.upload?.pasteURL) {\n throw new APIError('Pasting from URL is not enabled for this collection.', 400)\n }\n\n if (id) {\n // updating doc\n const accessResult = await executeAccess({ req }, config.access.update)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n } else {\n // creating doc\n const accessResult = await executeAccess({ req }, config.access?.create)\n if (!accessResult) {\n throw new Forbidden(req.t)\n }\n }\n\n if (!req.url) {\n throw new APIError('Request URL is missing.', 400)\n }\n\n const { searchParams } = new URL(req.url)\n const src = searchParams.get('src')\n\n if (!src || typeof src !== 'string') {\n throw new APIError('A valid URL string is required.', 400)\n }\n\n const hasAllowList =\n typeof config.upload.pasteURL === 'object' && Array.isArray(config.upload.pasteURL.allowList)\n\n let fileURL: string\n try {\n fileURL = new URL(src).href\n } catch {\n throw new APIError('A valid URL string is required.', 400)\n }\n\n if (hasAllowList && !isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {\n throw new APIError('The provided URL is not allowed.', 400)\n }\n\n let redirectCount = 0\n const maxRedirects = 3\n let response!: Response\n\n while (true) {\n if (hasAllowList && isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {\n // Allow-listed URLs bypass SSRF filtering (e.g. internal/localhost CDNs)\n response = await fetch(fileURL, {\n headers: { 'Accept-Encoding': 'identity' },\n redirect: 'manual',\n signal: AbortSignal.timeout(30_000),\n })\n } else {\n response = await safeFetch(fileURL, {\n headers: {\n 'Accept-Encoding': 'identity',\n },\n signal: AbortSignal.timeout(30_000),\n })\n }\n\n if (response.status >= 300 && response.status < 400) {\n redirectCount++\n if (redirectCount > maxRedirects) {\n throw new APIError('Too many redirects.', 403)\n }\n const location = response.headers.get('location')\n if (location) {\n fileURL = new URL(location, fileURL).href\n if (hasAllowList && !isURLAllowed(fileURL, config.upload.pasteURL.allowList)) {\n throw new APIError('The provided URL is not allowed.', 400)\n }\n continue\n }\n }\n\n break\n }\n\n if (!response.ok) {\n throw new APIError('Failed to fetch the file from the provided URL.', response.status)\n }\n\n const rawFileName = decodeURIComponent(new URL(fileURL).pathname.split('/').pop() || '')\n const safeFileName = sanitizeFilename(rawFileName)\n const encodedFileName = encodeURIComponent(safeFileName).replace(\n /['()]/g,\n (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,\n )\n // Strip quotes, backslashes, and control chars from the ASCII fallback\n const asciiFileName = safeFileName.replace(/[\"\\\\\\r\\n]/g, '_')\n\n return new Response(response.body, {\n headers: {\n 'Content-Disposition': `attachment; filename=\"${asciiFileName}\"; filename*=UTF-8''${encodedFileName}`,\n 'Content-Length': response.headers.get('content-length') || '',\n 'Content-Type': response.headers.get('content-type') || 'application/octet-stream',\n },\n })\n}\n"],"names":["executeAccess","APIError","Forbidden","getRequestCollectionWithID","isURLAllowed","sanitizeFilename","safeFetch","getFileFromURLHandler","req","id","collection","optionalID","user","t","config","upload","pasteURL","accessResult","access","update","create","url","searchParams","URL","src","get","hasAllowList","Array","isArray","allowList","fileURL","href","redirectCount","maxRedirects","response","fetch","headers","redirect","signal","AbortSignal","timeout","status","location","ok","rawFileName","decodeURIComponent","pathname","split","pop","safeFileName","encodedFileName","encodeURIComponent","replace","c","charCodeAt","toString","toUpperCase","asciiFileName","Response","body"],"mappings":"AAEA,SA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F;QAEA;IACF;IAEA,IAAI,CAACiC,SAASS,EAAE,EAAE;QAChB,MAAM,IAAI1C,SAAS,mDAAmDiC,SAASO,MAAM;IACvF;IAEA,MAAMG,cAAcC,mBAAmB,IAAItB,IAAIO,SAASgB,QAAQ,CAACC,KAAK,CAAC,KAAKC,GAAG,MAAM;IACrF,MAAMC,eAAe5C,iBAAiBuC;IACtC,MAAMM,kBAAkBC,mBAAmBF,cAAcG,OAAO,CAC9D,UACA,CAACC,IAAM,CAAC,CAAC,EAAEA,EAAEC,UAAU,CAAC,GAAGC,QAAQ,CAAC,IAAIC,WAAW,IAAI;IAEzD,uEAAuE;IACvE,MAAMC,gBAAgBR,aAAaG,OAAO,CAAC,cAAc;IAEzD,OAAO,IAAIM,SAASxB,SAASyB,IAAI,EAAE;QACjCvB,SAAS;YACP,uBAAuB,CAAC,sBAAsB,EAAEqB,cAAc,oBAAoB,EAAEP,iBAAiB;YACrG,kBAAkBhB,SAASE,OAAO,CAACX,GAAG,CAAC,qBAAqB;YAC5D,gBAAgBS,SAASE,OAAO,CAACX,GAAG,CAAC,mBAAmB;QAC1D;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getExternalFile.d.ts","sourceRoot":"","sources":["../../src/uploads/getExternalFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAM9D,KAAK,IAAI,GAAG;IACV,IAAI,EAAE,QAAQ,CAAA;IACd,GAAG,EAAE,cAAc,CAAA;IACnB,YAAY,EAAE,YAAY,CAAA;CAC3B,CAAA;AACD,eAAO,MAAM,eAAe,gCAAuC,IAAI,KAAG,OAAO,CAAC,IAAI,CAyFrF,CAAA"}
1
+ {"version":3,"file":"getExternalFile.d.ts","sourceRoot":"","sources":["../../src/uploads/getExternalFile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAM9D,KAAK,IAAI,GAAG;IACV,IAAI,EAAE,QAAQ,CAAA;IACd,GAAG,EAAE,cAAc,CAAA;IACnB,YAAY,EAAE,YAAY,CAAA;CAC3B,CAAA;AACD,eAAO,MAAM,eAAe,gCAAuC,IAAI,KAAG,OAAO,CAAC,IAAI,CAgGrF,CAAA"}
@@ -48,6 +48,9 @@ export const getExternalFile = async ({ data, req, uploadConfig })=>{
48
48
  const location = res.headers.get('location');
49
49
  if (location) {
50
50
  fileURL = new URL(location, fileURL).toString();
51
+ if (uploadConfig.pasteURL && uploadConfig.pasteURL.allowList && !isURLAllowed(fileURL, uploadConfig.pasteURL.allowList)) {
52
+ throw new APIError('Redirect target is not allowed.', 400);
53
+ }
51
54
  continue;
52
55
  }
53
56
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/uploads/getExternalFile.ts"],"sourcesContent":["import type { PayloadRequest } from '../types/index.js'\nimport type { File, FileData, UploadConfig } from './types.js'\n\nimport { APIError } from '../errors/index.js'\nimport { isURLAllowed } from '../utilities/isURLAllowed.js'\nimport { safeFetch } from './safeFetch.js'\n\ntype Args = {\n data: FileData\n req: PayloadRequest\n uploadConfig: UploadConfig\n}\nexport const getExternalFile = async ({ data, req, uploadConfig }: Args): Promise<File> => {\n const { filename, url } = data\n\n let trimAuthCookies = true\n if (typeof url === 'string') {\n let fileURL = url\n if (!url.startsWith('http')) {\n // URL points to the same server - we can send any cookies safely to our server.\n trimAuthCookies = false\n const baseUrl = req.headers.get('origin') || `${req.protocol}://${req.headers.get('host')}`\n fileURL = `${baseUrl}${url}`\n }\n\n let cookies = (req.headers.get('cookie') ?? '').split(';')\n\n if (trimAuthCookies) {\n cookies = cookies.filter(\n (cookie) => !cookie.trim().startsWith(req.payload.config.cookiePrefix),\n )\n }\n\n const headers = uploadConfig.externalFileHeaderFilter\n ? uploadConfig.externalFileHeaderFilter(Object.fromEntries(new Headers(req.headers)))\n : {\n cookie: cookies.join(';'),\n }\n\n let res\n let redirectCount = 0\n const maxRedirects = 3\n\n while (redirectCount <= maxRedirects) {\n const skipSafeFetch: boolean =\n uploadConfig.skipSafeFetch === true\n ? uploadConfig.skipSafeFetch\n : Array.isArray(uploadConfig.skipSafeFetch) &&\n isURLAllowed(fileURL, uploadConfig.skipSafeFetch)\n\n const isAllowedPasteUrl: boolean | undefined =\n uploadConfig.pasteURL &&\n uploadConfig.pasteURL.allowList &&\n isURLAllowed(fileURL, uploadConfig.pasteURL.allowList)\n\n if (skipSafeFetch || isAllowedPasteUrl) {\n res = await fetch(fileURL, {\n credentials: 'include',\n headers,\n method: 'GET',\n redirect: 'manual',\n })\n } else {\n // Default\n res = await safeFetch(fileURL, {\n credentials: 'include',\n headers,\n method: 'GET',\n })\n }\n\n if (res.status >= 300 && res.status < 400) {\n redirectCount++\n if (redirectCount > maxRedirects) {\n throw new APIError(`Too many redirects (max ${maxRedirects})`, 403)\n }\n const location = res.headers.get('location')\n if (location) {\n fileURL = new URL(location, fileURL).toString()\n continue\n }\n }\n\n break\n }\n\n if (!res || !res.ok) {\n throw new APIError(`Failed to fetch file from ${fileURL}`, res?.status)\n }\n\n const data = await res.arrayBuffer()\n\n return {\n name: filename,\n data: Buffer.from(data),\n mimetype: res.headers.get('content-type') || undefined!,\n size: Number(res.headers.get('content-length')) || 0,\n }\n }\n\n throw new APIError('Invalid file url', 400)\n}\n"],"names":["APIError","isURLAllowed","safeFetch","getExternalFile","data","req","uploadConfig","filename","url","trimAuthCookies","fileURL","startsWith","baseUrl","headers","get","protocol","cookies","split","filter","cookie","trim","payload","config","cookiePrefix","externalFileHeaderFilter","Object","fromEntries","Headers","join","res","redirectCount","maxRedirects","skipSafeFetch","Array","isArray","isAllowedPasteUrl","pasteURL","allowList","fetch","credentials","method","redirect","status","location","URL","toString","ok","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B,aAAaS,SAASJ,aAAa0B,aAAa;YAEtD,MAAMG,oBACJ7B,aAAa8B,QAAQ,IACrB9B,aAAa8B,QAAQ,CAACC,SAAS,IAC/BpC,aAAaS,SAASJ,aAAa8B,QAAQ,CAACC,SAAS;YAEvD,IAAIL,iBAAiBG,mBAAmB;gBACtCN,MAAM,MAAMS,MAAM5B,SAAS;oBACzB6B,aAAa;oBACb1B;oBACA2B,QAAQ;oBACRC,UAAU;gBACZ;YACF,OAAO;gBACL,UAAU;gBACVZ,MAAM,MAAM3B,UAAUQ,SAAS;oBAC7B6B,aAAa;oBACb1B;oBACA2B,QAAQ;gBACV;YACF;YAEA,IAAIX,IAAIa,MAAM,IAAI,OAAOb,IAAIa,MAAM,GAAG,KAAK;gBACzCZ;gBACA,IAAIA,gBAAgBC,cAAc;oBAChC,MAAM,IAAI/B,SAAS,CAAC,wBAAwB,EAAE+B,aAAa,CAAC,CAAC,EAAE;gBACjE;gBACA,MAAMY,WAAWd,IAAIhB,OAAO,CAACC,GAAG,CAAC;gBACjC,IAAI6B,UAAU;oBACZjC,UAAU,IAAIkC,IAAID,UAAUjC,SAASmC,QAAQ;oBAC7C;gBACF;YACF;YAEA;QACF;QAEA,IAAI,CAAChB,OAAO,CAACA,IAAIiB,EAAE,EAAE;YACnB,MAAM,IAAI9C,SAAS,CAAC,0BAA0B,EAAEU,SAAS,EAAEmB,KAAKa;QAClE;QAEA,MAAMtC,OAAO,MAAMyB,IAAIkB,WAAW;QAElC,OAAO;YACLC,MAAMzC;YACNH,MAAM6C,OAAOC,IAAI,CAAC9C;YAClB+C,UAAUtB,IAAIhB,OAAO,CAACC,GAAG,CAAC,mBAAmBsC;YAC7CC,MAAMC,OAAOzB,IAAIhB,OAAO,CAACC,GAAG,CAAC,sBAAsB;QACrD;IACF;IAEA,MAAM,IAAId,SAAS,oBAAoB;AACzC,EAAC"}
1
+ {"version":3,"sources":["../../src/uploads/getExternalFile.ts"],"sourcesContent":["import type { PayloadRequest } from '../types/index.js'\nimport type { File, FileData, UploadConfig } from './types.js'\n\nimport { APIError } from '../errors/index.js'\nimport { isURLAllowed } from '../utilities/isURLAllowed.js'\nimport { safeFetch } from './safeFetch.js'\n\ntype Args = {\n data: FileData\n req: PayloadRequest\n uploadConfig: UploadConfig\n}\nexport const getExternalFile = async ({ data, req, uploadConfig }: Args): Promise<File> => {\n const { filename, url } = data\n\n let trimAuthCookies = true\n if (typeof url === 'string') {\n let fileURL = url\n if (!url.startsWith('http')) {\n // URL points to the same server - we can send any cookies safely to our server.\n trimAuthCookies = false\n const baseUrl = req.headers.get('origin') || `${req.protocol}://${req.headers.get('host')}`\n fileURL = `${baseUrl}${url}`\n }\n\n let cookies = (req.headers.get('cookie') ?? '').split(';')\n\n if (trimAuthCookies) {\n cookies = cookies.filter(\n (cookie) => !cookie.trim().startsWith(req.payload.config.cookiePrefix),\n )\n }\n\n const headers = uploadConfig.externalFileHeaderFilter\n ? uploadConfig.externalFileHeaderFilter(Object.fromEntries(new Headers(req.headers)))\n : {\n cookie: cookies.join(';'),\n }\n\n let res\n let redirectCount = 0\n const maxRedirects = 3\n\n while (redirectCount <= maxRedirects) {\n const skipSafeFetch: boolean =\n uploadConfig.skipSafeFetch === true\n ? uploadConfig.skipSafeFetch\n : Array.isArray(uploadConfig.skipSafeFetch) &&\n isURLAllowed(fileURL, uploadConfig.skipSafeFetch)\n\n const isAllowedPasteUrl: boolean | undefined =\n uploadConfig.pasteURL &&\n uploadConfig.pasteURL.allowList &&\n isURLAllowed(fileURL, uploadConfig.pasteURL.allowList)\n\n if (skipSafeFetch || isAllowedPasteUrl) {\n res = await fetch(fileURL, {\n credentials: 'include',\n headers,\n method: 'GET',\n redirect: 'manual',\n })\n } else {\n // Default\n res = await safeFetch(fileURL, {\n credentials: 'include',\n headers,\n method: 'GET',\n })\n }\n\n if (res.status >= 300 && res.status < 400) {\n redirectCount++\n if (redirectCount > maxRedirects) {\n throw new APIError(`Too many redirects (max ${maxRedirects})`, 403)\n }\n const location = res.headers.get('location')\n if (location) {\n fileURL = new URL(location, fileURL).toString()\n if (\n uploadConfig.pasteURL &&\n uploadConfig.pasteURL.allowList &&\n !isURLAllowed(fileURL, uploadConfig.pasteURL.allowList)\n ) {\n throw new APIError('Redirect target is not allowed.', 400)\n }\n continue\n }\n }\n\n break\n }\n\n if (!res || !res.ok) {\n throw new APIError(`Failed to fetch file from ${fileURL}`, res?.status)\n }\n\n const data = await res.arrayBuffer()\n\n return {\n name: filename,\n data: Buffer.from(data),\n mimetype: res.headers.get('content-type') || undefined!,\n size: Number(res.headers.get('content-length')) || 0,\n }\n }\n\n throw new APIError('Invalid file url', 400)\n}\n"],"names":["APIError","isURLAllowed","safeFetch","getExternalFile","data","req","uploadConfig","filename","url","trimAuthCookies","fileURL","startsWith","baseUrl","headers","get","protocol","cookies","split","filter","cookie","trim","payload","config","cookiePrefix","externalFileHeaderFilter","Object","fromEntries","Headers","join","res","redirectCount","maxRedirects","skipSafeFetch","Array","isArray","isAllowedPasteUrl","pasteURL","allowList","fetch","credentials","method","redirect","status","location","URL","toString","ok","arrayBuffer","name","Buffer","from","mimetype","undefined","size","Number"],"mappings":"AAGA,SAASA,QAAQ,QAAQ,qBAAoB;AAC7C,SAASC,YAAY,QAAQ,+BAA8B;AAC3D,SAASC,SAAS,QAAQ,iBAAgB;AAO1C,OAAO,MAAMC,kBAAkB,OAAO,EAAEC,IAAI,EAAEC,GAAG,EAAEC,YAAY,EAAQ;IACrE,MAAM,EAAEC,QAAQ,EAAEC,GAAG,EAAE,GAAGJ;IAE1B,IAAIK,kBAAkB;IACtB,IAAI,OAAOD,QAAQ,UAAU;QAC3B,IAAIE,UAAUF;QACd,IAAI,CAACA,IAAIG,UAAU,CAAC,SAAS;YAC3B,gFAAgF;YAChFF,kBAAkB;YAClB,MAAMG,UAAUP,IAAIQ,OAAO,CAACC,GAAG,CAAC,aAAa,GAAGT,IAAIU,QAAQ,CAAC,GAAG,EAAEV,IAAIQ,OAAO,CAACC,GAAG,CAAC,S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,EAAE;gBACjE;gBACA,MAAMY,WAAWd,IAAIhB,OAAO,CAACC,GAAG,CAAC;gBACjC,IAAI6B,UAAU;oBACZjC,UAAU,IAAIkC,IAAID,UAAUjC,SAASmC,QAAQ;oBAC7C,IACEvC,aAAa8B,QAAQ,IACrB9B,aAAa8B,QAAQ,CAACC,SAAS,IAC/B,CAACpC,aAAaS,SAASJ,aAAa8B,QAAQ,CAACC,SAAS,GACtD;wBACA,MAAM,IAAIrC,SAAS,mCAAmC;oBACxD;oBACA;gBACF;YACF;YAEA;QACF;QAEA,IAAI,CAAC6B,OAAO,CAACA,IAAIiB,EAAE,EAAE;YACnB,MAAM,IAAI9C,SAAS,CAAC,0BAA0B,EAAEU,SAAS,EAAEmB,KAAKa;QAClE;QAEA,MAAMtC,OAAO,MAAMyB,IAAIkB,WAAW;QAElC,OAAO;YACLC,MAAMzC;YACNH,MAAM6C,OAAOC,IAAI,CAAC9C;YAClB+C,UAAUtB,IAAIhB,OAAO,CAACC,GAAG,CAAC,mBAAmBsC;YAC7CC,MAAMC,OAAOzB,IAAIhB,OAAO,CAACC,GAAG,CAAC,sBAAsB;QACrD;IACF;IAEA,MAAM,IAAId,SAAS,oBAAoB;AACzC,EAAC"}
@@ -13,5 +13,5 @@ export declare const _internal_safeFetchGlobal: {
13
13
  * - Validates domain names by resolving them to IP addresses and checking if they're safe.
14
14
  * - Undici was used because it supported interceptors as well as "credentials: include". Native fetch
15
15
  */
16
- export declare const safeFetch: (input: import("undici").RequestInfo, init?: import("undici").RequestInit | undefined) => Promise<import("undici").Response>;
16
+ export declare const safeFetch: (input: import("undici").RequestInfo, init?: import("undici").RequestInit | undefined) => Promise<Response>;
17
17
  //# sourceMappingURL=safeFetch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"safeFetch.d.ts","sourceRoot":"","sources":["../../src/uploads/safeFetch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,EAAS,KAAK,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,yBAAyB;;CAErC,CAAA;AAgDD;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,8HA4CrB,CAAA"}
1
+ {"version":3,"file":"safeFetch.d.ts","sourceRoot":"","sources":["../../src/uploads/safeFetch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,EAAS,KAAK,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,yBAAyB;;CAErC,CAAA;AAgDD;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,4FAAoD,OAAO,CAAC,QAAQ,CA4CzF,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/uploads/safeFetch.ts"],"sourcesContent":["import type { LookupFunction } from 'net'\n\nimport { lookup } from 'dns'\nimport ipaddr from 'ipaddr.js'\nimport { Agent, fetch as undiciFetch } from 'undici'\n\n/**\n * @internal this is used to mock the IP `lookup` function in integration tests\n */\nexport const _internal_safeFetchGlobal = {\n lookup,\n}\n\nconst isSafeIp = (ip: string) => {\n try {\n if (!ip) {\n return false\n }\n\n if (!ipaddr.isValid(ip)) {\n return false\n }\n\n const parsedIpAddress = ipaddr.parse(ip)\n const range = parsedIpAddress.range()\n if (range !== 'unicast') {\n return false // Private IP Range\n }\n } catch (ignore) {\n return false\n }\n return true\n}\n\nconst ssrfFilterInterceptor: LookupFunction = (hostname, options, callback) => {\n _internal_safeFetchGlobal.lookup(hostname, options, (err, address, family) => {\n if (err) {\n callback(err, address, family)\n } else {\n let ips = [] as string[]\n if (Array.isArray(address)) {\n ips = address.map((a) => a.address)\n } else {\n ips = [address]\n }\n\n if (ips.some((ip) => !isSafeIp(ip))) {\n callback(new Error(`Blocked unsafe attempt to ${hostname}`), address, family)\n return\n }\n\n callback(null, address, family)\n }\n })\n}\n\nconst safeDispatcher = new Agent({\n connect: { lookup: ssrfFilterInterceptor },\n})\n/**\n * A \"safe\" version of undici's fetch that prevents SSRF attacks.\n *\n * - Utilizes a custom dispatcher that filters out requests to unsafe IP addresses.\n * - Validates domain names by resolving them to IP addresses and checking if they're safe.\n * - Undici was used because it supported interceptors as well as \"credentials: include\". Native fetch\n */\nexport const safeFetch = async (...args: Parameters<typeof undiciFetch>) => {\n const [unverifiedUrl, options] = args\n\n try {\n const url = new URL(unverifiedUrl)\n\n let hostname = url.hostname\n\n // Strip brackets from IPv6 addresses (e.g., \"[::1]\" => \"::1\")\n if (hostname.startsWith('[') && hostname.endsWith(']')) {\n hostname = hostname.slice(1, -1)\n }\n\n if (ipaddr.isValid(hostname)) {\n if (!isSafeIp(hostname)) {\n throw new Error(`Blocked unsafe attempt to ${hostname}`)\n }\n }\n return await undiciFetch(url, {\n ...options,\n dispatcher: safeDispatcher,\n redirect: 'manual', // Prevent automatic redirects\n })\n } catch (error) {\n if (error instanceof Error) {\n if (error.cause instanceof Error && error.cause.message.includes('unsafe')) {\n // Errors thrown from within interceptors always have 'fetch error' as the message\n // The desired message we want to bubble up is in the cause\n throw new Error(error.cause.message)\n } else {\n let stringifiedUrl: string | undefined = undefined\n if (typeof unverifiedUrl === 'string') {\n stringifiedUrl = unverifiedUrl\n } else if (unverifiedUrl instanceof URL) {\n stringifiedUrl = unverifiedUrl.toString()\n } else if (unverifiedUrl instanceof Request) {\n stringifiedUrl = unverifiedUrl.url\n }\n\n throw new Error(`Failed to fetch from ${stringifiedUrl}, ${error.message}`)\n }\n }\n throw error\n }\n}\n"],"names":["lookup","ipaddr","Agent","fetch","undiciFetch","_internal_safeFetchGlobal","isSafeIp","ip","isValid","parsedIpAddress","parse","range","ignore","ssrfFilterInterceptor","hostname","options","callback","err","address","family","ips","Array","isArray","map","a","some","Error","safeDispatcher","connect","safeFetch","args","unverifiedUrl","url","URL","startsWith","endsWith","slice","dispatcher","redirect","error","cause","message","includes","stringifiedUrl","undefined","toString","Request"],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IAAIC,IAAIF;QAEpB,IAAIjB,WAAWkB,IAAIlB,QAAQ;QAE3B,8DAA8D;QAC9D,IAAIA,SAASoB,UAAU,CAAC,QAAQpB,SAASqB,QAAQ,CAAC,MAAM;YACtDrB,WAAWA,SAASsB,KAAK,CAAC,GAAG,CAAC;QAChC;QAEA,IAAInC,OAAOO,OAAO,CAACM,WAAW;YAC5B,IAAI,CAACR,SAASQ,WAAW;gBACvB,MAAM,IAAIY,MAAM,CAAC,0BAA0B,EAAEZ,UAAU;YACzD;QACF;QACA,OAAO,MAAMV,YAAY4B,KAAK;YAC5B,GAAGjB,OAAO;YACVsB,YAAYV;YACZW,UAAU;QACZ;IACF,EAAE,OAAOC,OAAO;QACd,IAAIA,iBAAiBb,OAAO;YAC1B,IAAIa,MAAMC,KAAK,YAAYd,SAASa,MAAMC,KAAK,CAACC,OAAO,CAACC,QAAQ,CAAC,WAAW;gBAC1E,kFAAkF;gBAClF,2DAA2D;gBAC3D,MAAM,IAAIhB,MAAMa,MAAMC,KAAK,CAACC,OAAO;YACrC,OAAO;gBACL,IAAIE,iBAAqCC;gBACzC,IAAI,OAAOb,kBAAkB,UAAU;oBACrCY,iBAAiBZ;gBACnB,OAAO,IAAIA,yBAAyBE,KAAK;oBACvCU,iBAAiBZ,cAAcc,QAAQ;gBACzC,OAAO,IAAId,yBAAyBe,SAAS;oBAC3CH,iBAAiBZ,cAAcC,GAAG;gBACpC;gBAEA,MAAM,IAAIN,MAAM,CAAC,qBAAqB,EAAEiB,eAAe,EAAE,EAAEJ,MAAME,OAAO,EAAE;YAC5E;QACF;QACA,MAAMF;IACR;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/uploads/safeFetch.ts"],"sourcesContent":["import type { LookupFunction } from 'net'\n\nimport { lookup } from 'dns'\nimport ipaddr from 'ipaddr.js'\nimport { Agent, fetch as undiciFetch } from 'undici'\n\n/**\n * @internal this is used to mock the IP `lookup` function in integration tests\n */\nexport const _internal_safeFetchGlobal = {\n lookup,\n}\n\nconst isSafeIp = (ip: string) => {\n try {\n if (!ip) {\n return false\n }\n\n if (!ipaddr.isValid(ip)) {\n return false\n }\n\n const parsedIpAddress = ipaddr.parse(ip)\n const range = parsedIpAddress.range()\n if (range !== 'unicast') {\n return false // Private IP Range\n }\n } catch (ignore) {\n return false\n }\n return true\n}\n\nconst ssrfFilterInterceptor: LookupFunction = (hostname, options, callback) => {\n _internal_safeFetchGlobal.lookup(hostname, options, (err, address, family) => {\n if (err) {\n callback(err, address, family)\n } else {\n let ips = [] as string[]\n if (Array.isArray(address)) {\n ips = address.map((a) => a.address)\n } else {\n ips = [address]\n }\n\n if (ips.some((ip) => !isSafeIp(ip))) {\n callback(new Error(`Blocked unsafe attempt to ${hostname}`), address, family)\n return\n }\n\n callback(null, address, family)\n }\n })\n}\n\nconst safeDispatcher = new Agent({\n connect: { lookup: ssrfFilterInterceptor },\n})\n/**\n * A \"safe\" version of undici's fetch that prevents SSRF attacks.\n *\n * - Utilizes a custom dispatcher that filters out requests to unsafe IP addresses.\n * - Validates domain names by resolving them to IP addresses and checking if they're safe.\n * - Undici was used because it supported interceptors as well as \"credentials: include\". Native fetch\n */\nexport const safeFetch = async (...args: Parameters<typeof undiciFetch>): Promise<Response> => {\n const [unverifiedUrl, options] = args\n\n try {\n const url = new URL(unverifiedUrl)\n\n let hostname = url.hostname\n\n // Strip brackets from IPv6 addresses (e.g., \"[::1]\" => \"::1\")\n if (hostname.startsWith('[') && hostname.endsWith(']')) {\n hostname = hostname.slice(1, -1)\n }\n\n if (ipaddr.isValid(hostname)) {\n if (!isSafeIp(hostname)) {\n throw new Error(`Blocked unsafe attempt to ${hostname}`)\n }\n }\n return (await undiciFetch(url, {\n ...options,\n dispatcher: safeDispatcher,\n redirect: 'manual', // Prevent automatic redirects\n })) as unknown as Response\n } catch (error) {\n if (error instanceof Error) {\n if (error.cause instanceof Error && error.cause.message.includes('unsafe')) {\n // Errors thrown from within interceptors always have 'fetch error' as the message\n // The desired message we want to bubble up is in the cause\n throw new Error(error.cause.message)\n } else {\n let stringifiedUrl: string | undefined = undefined\n if (typeof unverifiedUrl === 'string') {\n stringifiedUrl = unverifiedUrl\n } else if (unverifiedUrl instanceof URL) {\n stringifiedUrl = unverifiedUrl.toString()\n } else if (unverifiedUrl instanceof Request) {\n stringifiedUrl = unverifiedUrl.url\n }\n\n throw new Error(`Failed to fetch from ${stringifiedUrl}, ${error.message}`)\n }\n }\n throw error\n }\n}\n"],"names":["lookup","ipaddr","Agent","fetch","undiciFetch","_internal_safeFetchGlobal","isSafeIp","ip","isValid","parsedIpAddress","parse","range","ignore","ssrfFilterInterceptor","hostname","options","callback","err","address","family","ips","Array","isArray","map","a","some","Error","safeDispatcher","connect","safeFetch","args","unverifiedUrl","url","URL","startsWith","endsWith","slice","dispatcher","redirect","error","cause","message","includes","stringifiedUrl","undefined","toString","Request"],"mappings":"AAEA,SAASA,MAAM,QAAQ,MAAK;AAC5B,OAAOC,YAAY,YAAW;AAC9B,SAASC,KAAK,EAAEC,SAASC,WAAW,QAAQ,SAAQ;AAEpD;;CAEC,GACD,OAAO,MAAMC,4BAA4B;IACvCL;AACF,EAAC;AAED,MAAMM,WAAW,CAACC;IAChB,IAAI;QACF,IAAI,CAACA,IAAI;YACP,OAAO;QACT;QAEA,IAAI,CAACN,OAAOO,OAAO,CAACD,KAAK;YACvB,OAAO;QACT;QAEA,MAAME,kBAAkBR,OAAOS,KAAK,CAACH;QACrC,MAAMI,QAAQF,gBAAgBE,KAAK;QACnC,IAAIA,UAAU,WAAW;YACvB,OAAO,MAAM,mBAAmB;;QAClC;IACF,EAAE,OAAOC,QAAQ;QACf,OAAO;IACT;IACA,OAAO;AACT;AAEA,MAAMC,wBAAwC,CAACC,UAAUC,SAASC;IAChEX,0BAA0BL,MAAM,CAACc,UAAUC,SAAS,CAACE,KAAKC,SAASC;QACjE,IAAIF,KAAK;YACPD,SAASC,KAAKC,SAASC;QACzB,OAAO;YACL,IAAIC,MAAM,EAAE;YACZ,IAAIC,MA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kBAAkB,UAAU;oBACrCY,iBAAiBZ;gBACnB,OAAO,IAAIA,yBAAyBE,KAAK;oBACvCU,iBAAiBZ,cAAcc,QAAQ;gBACzC,OAAO,IAAId,yBAAyBe,SAAS;oBAC3CH,iBAAiBZ,cAAcC,GAAG;gBACpC;gBAEA,MAAM,IAAIN,MAAM,CAAC,qBAAqB,EAAEiB,eAAe,EAAE,EAAEJ,MAAME,OAAO,EAAE;YAC5E;QACF;QACA,MAAMF;IACR;AACF,EAAC"}
@@ -0,0 +1,10 @@
1
+ import type { SanitizedConfig } from '../config/types.js';
2
+ import type { PayloadRequest } from '../types/index.js';
3
+ /**
4
+ * Returns a trusted request origin
5
+ */
6
+ export declare const getRequestOrigin: ({ config, req, }: {
7
+ config: Pick<SanitizedConfig, "cors" | "csrf" | "serverURL">;
8
+ req: Pick<PayloadRequest, "headers" | "payload" | "url">;
9
+ }) => string;
10
+ //# sourceMappingURL=getRequestOrigin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRequestOrigin.d.ts","sourceRoot":"","sources":["../../src/utilities/getRequestOrigin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AA8BvD;;GAEG;AACH,eAAO,MAAM,gBAAgB,qBAG1B;IACD,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,GAAG,WAAW,CAAC,CAAA;IAC5D,GAAG,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,SAAS,GAAG,KAAK,CAAC,CAAA;CACzD,KAAG,MA4BH,CAAA"}
@@ -0,0 +1,50 @@
1
+ const getTrustedOrigins = (config)=>{
2
+ const origins = new Set();
3
+ const { cors, csrf } = config;
4
+ if (cors === '*') {
5
+ return null;
6
+ }
7
+ if (Array.isArray(cors)) {
8
+ cors.forEach((o)=>origins.add(o));
9
+ } else if (cors && typeof cors === 'object') {
10
+ const corsOrigins = cors.origins;
11
+ if (corsOrigins === '*') {
12
+ return null;
13
+ }
14
+ if (Array.isArray(corsOrigins)) {
15
+ corsOrigins.forEach((o)=>origins.add(o));
16
+ }
17
+ }
18
+ if (Array.isArray(csrf)) {
19
+ csrf.forEach((o)=>origins.add(o));
20
+ }
21
+ return [
22
+ ...origins
23
+ ];
24
+ };
25
+ /**
26
+ * Returns a trusted request origin
27
+ */ export const getRequestOrigin = ({ config, req })=>{
28
+ if (config.serverURL !== null && config.serverURL !== '') {
29
+ return config.serverURL;
30
+ }
31
+ let origin = '';
32
+ try {
33
+ const protocol = new URL(req.url).protocol;
34
+ const host = req.headers?.get('host');
35
+ if (host) {
36
+ origin = `${protocol}//${host}`;
37
+ }
38
+ } catch {
39
+ // req.url is malformed; origin stays empty
40
+ }
41
+ const trustedOrigins = getTrustedOrigins(config);
42
+ if (trustedOrigins !== null && origin && trustedOrigins.includes(origin)) {
43
+ // Host header value is explicitly listed in the CORS/CSRF allowlist — safe to use.
44
+ return origin;
45
+ }
46
+ req.payload.logger.warn(`Request origin "${origin}" is not in the CORS/CSRF allowlist. Falling back to empty string as request origin. It is recommended to explicitly set the serverURL in the config to avoid this warning and ensure correct request origin is used.`);
47
+ return '';
48
+ };
49
+
50
+ //# sourceMappingURL=getRequestOrigin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utilities/getRequestOrigin.ts"],"sourcesContent":["import type { CORSConfig, SanitizedConfig } from '../config/types.js'\nimport type { PayloadRequest } from '../types/index.js'\n\nconst getTrustedOrigins = (config: Pick<SanitizedConfig, 'cors' | 'csrf'>): null | string[] => {\n const origins = new Set<string>()\n\n const { cors, csrf } = config\n\n if (cors === '*') {\n return null\n }\n\n if (Array.isArray(cors)) {\n cors.forEach((o) => origins.add(o))\n } else if (cors && typeof cors === 'object') {\n const corsOrigins = (cors as CORSConfig).origins\n if (corsOrigins === '*') {\n return null\n }\n if (Array.isArray(corsOrigins)) {\n corsOrigins.forEach((o) => origins.add(o))\n }\n }\n\n if (Array.isArray(csrf)) {\n csrf.forEach((o) => origins.add(o))\n }\n\n return [...origins]\n}\n\n/**\n * Returns a trusted request origin\n */\nexport const getRequestOrigin = ({\n config,\n req,\n}: {\n config: Pick<SanitizedConfig, 'cors' | 'csrf' | 'serverURL'>\n req: Pick<PayloadRequest, 'headers' | 'payload' | 'url'>\n}): string => {\n if (config.serverURL !== null && config.serverURL !== '') {\n return config.serverURL\n }\n\n let origin = ''\n try {\n const protocol = new URL(req.url!).protocol\n const host = req.headers?.get('host')\n if (host) {\n origin = `${protocol}//${host}`\n }\n } catch {\n // req.url is malformed; origin stays empty\n }\n\n const trustedOrigins = getTrustedOrigins(config)\n\n if (trustedOrigins !== null && origin && trustedOrigins.includes(origin)) {\n // Host header value is explicitly listed in the CORS/CSRF allowlist — safe to use.\n return origin\n }\n\n req.payload.logger.warn(\n `Request origin \"${origin}\" is not in the CORS/CSRF allowlist. Falling back to empty string as request origin. It is recommended to explicitly set the serverURL in the config to avoid this warning and ensure correct request origin is used.`,\n )\n\n return ''\n}\n"],"names":["getTrustedOrigins","config","origins","Set","cors","csrf","Array","isArray","forEach","o","add","corsOrigins","getRequestOrigin","req","serverURL","origin","protocol","URL","url","host","headers","get","trustedOrigins","includes","payload","logger","warn"],"mappings":"AAGA,MAAMA,oBAAoB,CAACC;IACzB,MAAMC,UAAU,IAAIC;IAEpB,MAAM,EAAEC,IAAI,EAAEC,IAAI,EAAE,GAAGJ;IAEvB,IAAIG,SAAS,KAAK;QAChB,OAAO;IACT;IAEA,IAAIE,MAAMC,OAAO,CAACH,OAAO;QACvBA,KAAKI,OAAO,CAAC,CAACC,IAAMP,QAAQQ,GAAG,CAACD;IAClC,OAAO,IAAIL,QAAQ,OAAOA,SAAS,UAAU;QAC3C,MAAMO,cAAc,AAACP,KAAoBF,OAAO;QAChD,IAAIS,gBAAgB,KAAK;YACvB,OAAO;QACT;QACA,IAAIL,MAAMC,OAAO,CA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