Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt to validate incoming data before proxy is executed #312

Merged
merged 6 commits into from
Jul 31, 2023

Conversation

kovalenp
Copy link
Contributor

Provide an option to preValidate incoming data (check out: #311)

Checklist

Copy link

@evahteev evahteev left a comment

Choose a reason for hiding this comment

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

LGTM

index.js Outdated
Comment on lines 263 to 269
if (opts.preValidation === undefined && opts.proxyPayloads !== false) {
fastify.addContentTypeParser('application/json', bodyParser)
fastify.addContentTypeParser('*', bodyParser)
}

if (opts.preValidation) {
fastify.addHook('preValidation', opts.preValidation)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we not proxy Payloads if preValidation is set?

Copy link
Contributor Author

@kovalenp kovalenp Jul 20, 2023

Choose a reason for hiding this comment

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

req.body is not parsed and comes as aIncomingMessage stream if:

fastify.addContentTypeParser('application/json', bodyParser)
fastify.addContentTypeParser('*', bodyParser)

condition is executed, as described here: #86 (comment)

actually, the payload is passed in this case, see:

https://github.com/fastify/fastify-http-proxy/pull/312/files#diff-a561630bb56b82342bc66697aee2ad96efddcbc9d150665abd6fb7ecb7c0ab2fR269

index.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

I'm -1 to add this option.

This can be achieved with the following:

app.register(async function (app) {
  app.addHook('preValidation', ...)
  app.register(httpProxy, {
    proxyPayloads: false
  })
})

Could you add that to the documentation instead?

@kovalenp
Copy link
Contributor Author

thanks @mcollina did you check #311 ? It is on the TODO list.

what if I don't want to run preValidation on every route just on the proxied one?

@mcollina
Copy link
Member

thanks @mcollina did you check #311 ?

I did now, I still think what you want to achieve can be done with the approach I described in #312 (review). Note that I set proxyPayloads to `false, which does the trick and it enables body validation.

It is on the TODO list.

I assembled that a long time ago, I might have been mistaken in adding this.

what if I don't want to run preValidation on every route just on the proxied one?

The snippet in #312 (review) adds it only to the proxied routes.

@kovalenp
Copy link
Contributor Author

I still find adding preValidator option a more convenient way (and it's consistent to preHandler option), I'd rather remove the proxyPayloads instead.

But it's indeed up to the team. Feel free to close the PR if you decide that it's not the preferable way.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

Can you fix the failing tests?

@kovalenp
Copy link
Contributor Author

kovalenp commented Jul 21, 2023

Can you fix the failing tests?

I wish!

I can see that tests are failing for node 18 on ubuntu agent and are hardly related to the change:

# Subtest: Proxy websocket with custom upstream url
        1..5
        ok 1 - should be equal
        ok 2 - should be equal
        ok 3 - should be equal
        ok 4 - should be equal
        ok 5 - should be equivalent strictly
    # test count(8) != plan(null)
    # failed 1 of 8 tests
not ok 5 - test/websocket.js # time=30007.488ms
  ---
  env: {}
  file: test/websocket.js
  timeout: 30000
  command: /opt/hostedtoolcache/node/18.16.1/x64/bin/node
  args:
    - test/websocket.js
  stdio:
    - 0
    - pipe
    - 2
  cwd: /home/runner/work/fastify-http-proxy/fastify-http-proxy
  failures:
    - tapError: no plan
  exitCode: null
  signal: SIGTERM
  ...

But the same is happening for me locally on the main branch. 😢 when use 18.17 node

could you please advise how it could be fixed, @mcollina ?

@mcollina mcollina mentioned this pull request Jul 28, 2023
@mcollina
Copy link
Member

For whatever reason this is actually breaking websocket support as this test PR is passing: #315

@kovalenp
Copy link
Contributor Author

For whatever reason this is actually breaking websocket support as this test PR is passing: #315

This PR didn't trigger unit tests though, there are only two checks. I'm positive main branch have the issue

@mcollina
Copy link
Member

Verified, it's a problem.

@mcollina
Copy link
Member

please rebase, I've fixed it

@kovalenp kovalenp force-pushed the feat/request-payload-validation branch from 4f2e199 to 8ab3717 Compare July 31, 2023 09:54
@kovalenp
Copy link
Contributor Author

please rebase, I've fixed it

Nice! Respect Sir. Rebase is done

@@ -31,6 +31,7 @@ declare namespace fastifyHttpProxy {
proxyPayloads?: boolean;
preHandler?: preHandlerHookHandler;
beforeHandler?: preHandlerHookHandler;
preValidation?: preValidationHookHandler;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed here: 0c0e505

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 0f5bc42 into fastify:master Jul 31, 2023
15 checks passed
@cb1kenobi
Copy link

Excellent work! I can't wait to use it. Mind pushing a new release? Thanks!

@denchen
Copy link

denchen commented Nov 1, 2023

Hi. Can we get a release for this? I saw preValidation in the docs and assumed that I could use it, but the latest release does not have this feature.

@Eomm
Copy link
Member

Eomm commented Nov 1, 2023

Released as 9.3.0 👍🏼

renovate bot added a commit to redwoodjs/redwood that referenced this pull request Nov 15, 2023
[![Mend Renovate logo
banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@fastify/http-proxy](https://togithub.com/fastify/fastify-http-proxy)
| [`9.2.1` ->
`9.3.0`](https://renovatebot.com/diffs/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@fastify%2fhttp-proxy/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@fastify%2fhttp-proxy/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify-http-proxy
(@&#8203;fastify/http-proxy)</summary>

###
[`v9.3.0`](https://togithub.com/fastify/fastify-http-proxy/releases/tag/v9.3.0)

[Compare
Source](https://togithub.com/fastify/fastify-http-proxy/compare/v9.2.1...v9.3.0)

#### What's Changed

- Listen only to IPv4 network for websockets tests by
[@&#8203;mcollina](https://togithub.com/mcollina) in
[fastify/fastify-http-proxy#316
- opt to validate incoming data before proxy is executed by
[@&#8203;kovalenp](https://togithub.com/kovalenp) in
[fastify/fastify-http-proxy#312
- build(deps-dev): bump
[@&#8203;typescript-eslint/eslint-plugin](https://togithub.com/typescript-eslint/eslint-plugin)
from 5.62.0 to 6.2.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#318
- build(deps-dev): bump tsd from 0.28.1 to 0.29.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#320
- perf: use `node:` prefix to bypass require.cache call for builtins by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#321
- build(deps-dev): bump express-http-proxy from 1.6.3 to 2.0.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#323
- test(websocket): optimize split param by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#326
- keep query params on proxy by
[@&#8203;dancastillo](https://togithub.com/dancastillo) in
[fastify/fastify-http-proxy#325
- chore(package): explicitly declare js module type by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#327

#### New Contributors

- [@&#8203;kovalenp](https://togithub.com/kovalenp) made their first
contribution in
[fastify/fastify-http-proxy#312
- [@&#8203;dancastillo](https://togithub.com/dancastillo) made their
first contribution in
[fastify/fastify-http-proxy#325

**Full Changelog**:
fastify/fastify-http-proxy@v9.2.1...v9.3.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/redwoodjs/redwood).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40Ni4wIiwidXBkYXRlZEluVmVyIjoiMzcuNDYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
jtoar pushed a commit to redwoodjs/redwood that referenced this pull request Nov 16, 2023
[![Mend Renovate logo
banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@fastify/http-proxy](https://togithub.com/fastify/fastify-http-proxy)
| [`9.2.1` ->
`9.3.0`](https://renovatebot.com/diffs/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@fastify%2fhttp-proxy/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@fastify%2fhttp-proxy/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@fastify%2fhttp-proxy/9.2.1/9.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify-http-proxy
(@&#8203;fastify/http-proxy)</summary>

###
[`v9.3.0`](https://togithub.com/fastify/fastify-http-proxy/releases/tag/v9.3.0)

[Compare
Source](https://togithub.com/fastify/fastify-http-proxy/compare/v9.2.1...v9.3.0)

#### What's Changed

- Listen only to IPv4 network for websockets tests by
[@&#8203;mcollina](https://togithub.com/mcollina) in
[fastify/fastify-http-proxy#316
- opt to validate incoming data before proxy is executed by
[@&#8203;kovalenp](https://togithub.com/kovalenp) in
[fastify/fastify-http-proxy#312
- build(deps-dev): bump
[@&#8203;typescript-eslint/eslint-plugin](https://togithub.com/typescript-eslint/eslint-plugin)
from 5.62.0 to 6.2.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#318
- build(deps-dev): bump tsd from 0.28.1 to 0.29.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#320
- perf: use `node:` prefix to bypass require.cache call for builtins by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#321
- build(deps-dev): bump express-http-proxy from 1.6.3 to 2.0.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[fastify/fastify-http-proxy#323
- test(websocket): optimize split param by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#326
- keep query params on proxy by
[@&#8203;dancastillo](https://togithub.com/dancastillo) in
[fastify/fastify-http-proxy#325
- chore(package): explicitly declare js module type by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify-http-proxy#327

#### New Contributors

- [@&#8203;kovalenp](https://togithub.com/kovalenp) made their first
contribution in
[fastify/fastify-http-proxy#312
- [@&#8203;dancastillo](https://togithub.com/dancastillo) made their
first contribution in
[fastify/fastify-http-proxy#325

**Full Changelog**:
fastify/fastify-http-proxy@v9.2.1...v9.3.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/redwoodjs/redwood).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40Ni4wIiwidXBkYXRlZEluVmVyIjoiMzcuNDYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants