Skip to content

chore: adds filters - #12622

Merged
denolfe merged 34 commits into
mainfrom
chore/url-filters
Jun 12, 2025
Merged

denolfe merged 34 commits into
mainfrom
chore/url-filters

Conversation

@kendelljoseph

@kendelljoseph kendelljoseph commented May 30, 2025 •

Copy link
Copy Markdown
Contributor

Filters URLs to avoid issues with SSRF

Had to use undici instead of native fetch because it was the only viable alternative that supported both overriding agent/dispatch and also implemented credentials: include.

More info here.

@denolfe

denolfe commented May 30, 2025 •

Copy link
Copy Markdown
Member

A few questions/notes:

  • Can we validate that this function call is actually doing anything? Feels like we should have a test. The library doesn't appear to have any types, which makes me a little more uneasy.
  • From reading, looks like this lib throws an error in the scenario where the URL is blocked. We should catch and rethrow an APIError, so it is handled nicely.

@kendelljoseph kendelljoseph self-assigned this Jun 3, 2025
@denolfe
denolfe merged commit c04c257 into main Jun 12, 2025
@denolfe
denolfe deleted the chore/url-filters branch June 12, 2025 13:46
@github-actions

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.43.0

@sam-gab

sam-gab commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

After upgrading to v3.43.0, I can't execute any pnpm payload xxx command.
And it seems this PR may have led to it. I may be wrong...

This is the issue:

> payload

/Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/undici/lib/web/fetch/webidl.js:27
  return new TypeError(`${message.header}: ${message.message}`)
         ^

TypeError: TypeError: Illegal constructor
    at webidl.errors.exception (/Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/undici/lib/web/fetch/webidl.js:27:10)
    at webidl.illegalConstructor (/Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/undici/lib/web/fetch/webidl.js:81:23)
    at new CacheStorage (/Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/undici/lib/web/cache/cachestorage.js:17:14)
    at Object.<anonymous> (/Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/undici/index.js:144:25)
    at Module._compile (node:internal/modules/cjs/loader:1529:14)
    at Object.transformer (file:///Users/xxxxxx/Documents/Projects/cnc-backend-cms/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/register-C3TE0KFF.mjs:2:1115)
    at Module.load (node:internal/modules/cjs/loader:1275:32)
    at Module._load (node:internal/modules/cjs/loader:1096:12)
    at Module.require (node:internal/modules/cjs/loader:1298:19)
    at require (node:internal/modules/helpers:182:18)

Node.js v20.19.0

@denolfe

@denolfe

denolfe commented Jun 17, 2025

Copy link
Copy Markdown
Member

After upgrading to v3.43.0, I can't execute any pnpm payload xxx command.
And it seems this PR may have led to it. I may be wrong...

Thanks @sam-gab . Let us take a look.

@denolfe denolfe added the keep Prevents from being marked stale or auto-closed. label Jun 17, 2025
@khnn

khnn commented Jun 19, 2025

Copy link
Copy Markdown

Facing the same issue like @sam-gab.

@bertrandkhe

Copy link
Copy Markdown

On version 3.43.0, when I try to run payload.jobs.run() from a custom CLI command, I get this error message:

26 |             throw new Error(`Blocked unsafe attempt to ${url}`);
27 |         }
28 |         return dispatch(opts, handler);
29 |     };
30 | };
31 | const safeDispatcher = new Agent().compose(ssrfFilterInterceptor);
                                        ^
TypeError: new Agent().compose is not a function. (In 'new Agent().compose(ssrfFilterInterceptor)', 'new Agent().compose' is undefined)
      at /home/node/app/node_modules/payload/dist/uploads/safeFetch.js:31:36

It's working fine 3.42.0.

@tsemachh

tsemachh commented Jun 19, 2025 •

Copy link
Copy Markdown
Contributor

I also face similar issue when I try to edit Document Upload from using Drawer I get:

fetch("http://localhost:3000/api/research-documents/6853e79ec74ff5e6453aa6b7?depth=0&fallback-locale=null", {
  ---,
  "referrer": "http://localhost:3000/admin/collections/meetings/6853cd951fb29a5e4f860490",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "------WebKitFormBoundaryOBc6sCM7NuGdHyLh\r\nContent-Disposition: form-data; name=\"_payload\"\r\n\r\n{\"url\":\"http://localhost:3000/api/research-documents/file/Can_you_translate_this_document_to_hebre-1.pdf\",\"creator\":\"tsemachh\",\"filename\":\"Can_you_translate_this_document_to_hebre-1.pdf\",\"mimeType\":\"application/pdf\",\"filesize\":139204,\"companies\":[\"6848050ca0c94d84bb5555f5\",\"6848050ca0c94d84bb5555fd\"],\"createdAt\":\"2025-06-19T10:34:06.077Z\",\"updatedAt\":\"2025-06-19T10:34:06.077Z\",\"uploadedAt\":\"2025-06-19T10:33:41.865Z\",\"thumbnailURL\":null}\r\n------WebKitFormBoundaryOBc6sCM7NuGdHyLh--\r\n",
  "method": "PATCH",
  "mode": "cors",
  "credentials": "include"
});

I get next error in console:

{
  "level": 50,
  "time": 1750333310047,
  "user": "tsemachhadad",
  "hostname": "Tsemachs-MacBook-Pro.local",
  "env": "production",
  "err": {
    "type": "z",
    "message": "A problem occurred while uploading the file. Blocked unsafe attempt to http://localhost:3000/api/research-documents/file/Can_you_translate_this_document_to_hebre-1.pdf",
    "stack": [
      "z: A problem occurred while uploading the file. Blocked unsafe attempt to http://localhost:3000/api/research-documents/file/Can_you_translate_this_document_to_hebre-1.pdf",
      "    at rg (/Users/tsemachhadad/devemada/emda-dev/.next/server/chunks/940.js:188:79825)",
      "    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)",
      "    at async B (/Users/tsemachhadad/devemada/emda-dev/.next/server/chunks/940.js:190:27905)",
      "    at async handler (/Users/tsemachhadad/devemada/emda-dev/.next/server/chunks/940.js:413:604780)",
      "    at async g (/Users/tsemachhadad/devemada/emda-dev/.next/server/chunks/6109.js:1:4473)",
      "    at async /Users/tsemachhadad/devemada/emda-dev/.next/server/chunks/6109.js:1:7150",
      "    at async to.do (/Users/tsemachhadad/devemada/emda-dev/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:18:18605)",
      "    at async to.handle (/Users/tsemachhadad/devemada/emda-dev/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:18:23632)",
      "    at async doRender (/Users/tsemachhadad/devemada/emda-dev/node_modules/next/dist/server/base-server.js:1513:42)",
      "    at async NextNodeServer.renderToResponseWithComponentsImpl (/Users/tsemachhadad/devemada/emda-dev/node_modules/next/dist/server/base-server.js:1915:28)"
    ],
    "data": null,
    "isOperational": true,
    "isPublic": false,
    "status": 500,
    "name": "z"
  },
  "msg": "A problem occurred while uploading the file. Blocked unsafe attempt to http://localhost:3000/api/research-documents/file/Can_you_translate_this_document_to_hebre-1.pdf"
}

This happens also on previous payload versions at leat 3.39 , didn't check backwards though
@denolfe - see the mentioned issue it's related as well , it's actually blocking both localhost and central env on this flow

const ssrfFilterInterceptor: Dispatcher.DispatcherComposeInterceptor = (dispatch) => {
return (opts, handler) => {
const url = new URL(opts.origin?.toString() + opts.path)
if (!isSafeIp(url.hostname)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hostname is not always a valid IP address, as per MDN it can be also a domain. Right now, checked on localhost and vercel deployment when editing a previously uploaded file, the hostname is being a domain name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix: #12927

@geminigeek

geminigeek commented Jun 21, 2025 •

Copy link
Copy Markdown
Contributor

On version 3.43.0, when I try to run payload.jobs.run() from a custom CLI command, I get this error message:

26 |             throw new Error(`Blocked unsafe attempt to ${url}`);
27 |         }
28 |         return dispatch(opts, handler);
29 |     };
30 | };
31 | const safeDispatcher = new Agent().compose(ssrfFilterInterceptor);
                                        ^
TypeError: new Agent().compose is not a function. (In 'new Agent().compose(ssrfFilterInterceptor)', 'new Agent().compose' is undefined)
      at /home/node/app/node_modules/payload/dist/uploads/safeFetch.js:31:36

It's working fine 3.42.0.

same issue using next custom server, working fine in 3.42.0 for me

denolfe pushed a commit that referenced this pull request Jun 26, 2025
## Fix
- Use `[Config].upload.skipSafeFetch` to allow specific external urls
- Use `[Config].upload.pasteURL.allowList` to allow specific external
urls

Documentation: [Uploading files from remote
urls](https://payloadcms.com/docs/upload/overview#uploading-files-from-remote-urls)

Fixes: #12876
Mentioned: #7037,
#12934
Source PR: #12622
Issue Trace:
1. [`allowList`
Added](8b7f2dd#diff-92acf7b8d30e447a791e37820136bcbf23c42f0358daca0fdea4e7b77f7d4bc9
)

2. [`allowList`
Removed](648c168#diff-92acf7b8d30e447a791e37820136bcbf23c42f0358daca0fdea4e7b77f7d4bc9)
@sam-gab

sam-gab commented Jun 27, 2025 •

Copy link
Copy Markdown
Contributor

Facing the same issue like @sam-gab.

Hi @khnn, noticed you were the only one who seemed to be facing the same issue I was.

Just an update, I realized switching from commonJs to ES Modules by adding "type": "module", to the package.json solved the issue.
I was sticking with commonjs prior to v3.43.0 because I was using some packages that don't fully support ES Modules.
cc: @denolfe

@khnn

khnn commented Jun 27, 2025

Copy link
Copy Markdown

Facing the same issue like @sam-gab.

Hi @khnn, noticed you were the only one who seemed to be facing the same issue I was.

Just an update, I realized switching from commonJs to ES Modules by adding "type": "module", to the package.json solved the issue.

I was sticking with commonjs prior to v3.43.0 because I was using some packages that don't fully support ES Modules.

cc: @denolfe

@sam-gab Thanks for pointing that out. I'll try that.

@AlTavares

Copy link
Copy Markdown

I'm having the same issue as @geminigeek.
Are you using Bun?

@geminigeek

geminigeek commented Jul 1, 2025 •

Copy link
Copy Markdown
Contributor

I'm having the same issue as @geminigeek. Are you using Bun?

yes, what version of payload you have? i am stuck on 3.38 , waiting for a PR, you can try with latest payload!

@AlTavares

Copy link
Copy Markdown

3.42 is the latest one I got working.
This seems related.

geminigeek added a commit to geminigeek/payload that referenced this pull request Jul 3, 2025
… false

hi, 
currently we are initializing safeDispatcher outside of the safeFetch function, which is causing edge case issue with bun and undici polyfill

like this 
https://github.com/payloadcms/payload/blob/30fc7e3012dae81b3f727b35ff6917e75f271df1/packages/payload/src/uploads/safeFetch.ts#L52

if we move this to the function safeFetch  , we can get rid of edge case like these , if user set `skipSafeFetch: true`.

i have implemented this like that in this PR, till bun fixes undici polyfill issue this is a temporary fix.


related issues payload
payloadcms#12622 (comment)
payloadcms#12622 (comment)


related issues Bun
oven-sh/bun#17799
oven-sh/bun#20022
@geminigeek

Copy link
Copy Markdown
Contributor

3.42 is the latest one I got working. This seems related.

i created a PR to temporarily fix this issue #13032

@cameronrdecker

cameronrdecker commented Jul 30, 2025 •

Copy link
Copy Markdown

As of version 3.49.1, I still can't run any payload command via npm. Getting the same "TypeError: Illegal constructor" message

UPDATE: I was able to get it working by adding "type": "module" to my package.json and converting a config file to module syntax.

@BoavidaGuerra

BoavidaGuerra commented Sep 3, 2025 •

Copy link
Copy Markdown

Same situation here. Any plans for a fix, besides switching from 'commonjs' to 'module'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

created-by: Payload team keep Prevents from being marked stale or auto-closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.