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

chore: add test for CORS OPTIONS handler #1331

Merged
merged 3 commits into from
May 30, 2022
Merged

Conversation

adamalton
Copy link
Contributor

@adamalton adamalton commented May 17, 2022

This is a follow-up from #1329, adding a test for the functionality that I broke. But it warrants a bit more explanation…

  • The envAll middleware attaches a Logging instance to env.log. This should be a new instance for each request.
  • The corsOptions function is not a middleware in the eyes of the itty-router, because it returns a response, and so the router then skips all other middlwares and just returns that response. This means that, if corsOptions is registered before envAll then the Logging instance is never attached to env.log and so when our main fetch function tries to call env.log.end(response) it dies trying to access the non-existent env.log property.
  • BUT!... that problem only occurs if the OPTIONS request is the first request made to the server. If a GET/POST/DELETE/ request has already successfully completed then the env.log will already be set, and that will be reused for the next request thus causing:
    1. the problem to be silently hidden; and
    2. the old Logging instance to be reused, including all its metadata, which may include the User ID from the previous request 😱 . (This User ID is only used for logging purposes, so it's nothing disastrous, but it will cause us to log misleading information.)
  • This little quirk means that the test I've added here doesn't catch the problem that was fixed in fix: swap the order of corsOptions and envAll to avoid error #1329 without the additional thing that I've added, which is…

I've made the Logging.end method blow up if you call it twice, thereby preventing it being reused on a second request. This seems to me like a reasonable solution, and it makes the test that I've added catch the problem that #1329 fixed. But alternative solutions could be:

  1. Store the Logging instance on request.log rather than env.log, because the request is guaranteed to be new for each request.
  2. Move to a more "proper" middleware setup, where there's a clear distinction between middlewares and handler functions, and where each middleware is therefore guaranteed to be run.

I'm happy to implement idea 1, if we think that's better, or to stick with the solution I've done. Idea 2 is probably beyond the scope of this PR.

@adamalton adamalton marked this pull request as ready for review May 17, 2022 16:36
@adamalton adamalton requested a review from olizilla May 17, 2022 16:37
Copy link
Contributor

@olizilla olizilla left a comment

Choose a reason for hiding this comment

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

👍

cc @hugomrdias just so you know

packages/api/src/utils/logs.js Outdated Show resolved Hide resolved
@olizilla
Copy link
Contributor

olizilla commented May 30, 2022

I want us to do

  1. Move to a more "proper" middleware setup

at some point. but yes, we can land this fix and test before getting in to that.

@adamalton adamalton merged commit 63328b5 into main May 30, 2022
@adamalton adamalton deleted the chore/test-cors-requests branch May 30, 2022 14:53
@adamalton
Copy link
Contributor Author

@olizilla do you have any particular thoughts around a better setup for the middleware stuff? It currently does all the right things, but I feel like it could perhaps just be a bit more explicit in the way things are declared. I don't have a specific proposal though. Maybe it's fine? Thoughts?

orvn added a commit that referenced this pull request Jun 13, 2022
* fix: JS errors from docs changes (#1334)

* fix: remove React fragment which is causing an error

This was causing `Each child in a list should have a unique "key" prop`

* fix: indentation from my previous commit

* fix: js errors

* fix: revert package-lock changes

* fix: lint error

Co-authored-by: Adam Alton <adamalton@gmail.com>

* fix: Removed mistakenly generated link on CID header item in filemanager (#1336)

* fix!: psa pinning APIs - rename requestId to requestid

* chore(main): release website 2.4.0 (#1299)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* make tooltip accessible (#1340)

* chore: rename pinned to psaPinned (#1268)


Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>

* docs: update peers (#1344)

* feat: respond with unique error message when blocked API key is used (#1302)

* chore(main): release api 6.0.0 (#1325)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore(main): release website 2.4.1 (#1342)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: Set fetch date before changing isFetching state (#1341)

* feat: send email notifications for storage quota usage (#1273)

This includes:
- a general email component in cron package for sending emails
- notifications to web3.storage users when they get to specific thresholds
- notifications to web3.storage admins when users go over their quota


Co-authored-by: Gary Homewood <gary@potatolondon.com>
Co-authored-by: Paolo <paolo@potatolondon.com>
Co-authored-by: Oli Evans <oli@tableflip.io>
Co-authored-by: francois-potato <103063611+francois-potato@users.noreply.github.com>

* fix: inaccurate used_storage migrations (#1360)

* feat: add user blocking functionality to web3 (#1322)

* chore: do not convert bigint to number (#1366)

* chore: use mailchimp provider in crons (#1368)

* chore: send list of storage quota violators to support@web3.storage, not admin@ (#1369)

* chore: remove unnecessary migration for creating admin user (#1373)

Rename the subsequent migration to keep the numbers sequential.
Now that we're using support@ rather than admin@ for the admin email address, that user already exists on both staging and prod.

* feat: implement postgres optimization (#1305)

* Refactor backups
* Add db migration scripts
* Update db configuration

* fix: typo in Logging constructor (#1346)

I assume this is a typo?

@adamalton can you also look into this logger running during testing in CI?

https://github.com/web3-storage/web3.storage/runs/6553500624?check_suite_focus=true

* fix: db migrations versioning (#1375)

* chore(main): release api 6.1.0 (#1356)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore(main): release cron 1.1.0 (#1357)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* docs: note db schema required in api readme

...and tweak website README to note that you are using a mock API

* chore: add test for CORS OPTIONS handler (#1331)

* fix: show custom storage quota to user (#1338)

* chore: fix tags in api user info (#1379)

* chore(main): release api 6.1.1 (#1382)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore(main): release website 2.5.0 (#1348)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: change the storage cron job config to run in prod, and every 6 hours (#1371)

* chore: trigger crons workflow on conf change (#1394)

* feat: Adding HasDeleteRestriction user_tag (#1390)

* Adding the type and failing HTTP DELETE operations if this tag is set.
* See nftstorage/admin.storage#66

* fix: clone env so new each request (#1396)

* chore(main): release api 6.2.0 (#1397)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* feat: DB schema and API for user_tag_proposal. (#1006)

* Users create records in this table and admins manage it.

Co-authored-by: trigramdev9 <jsdevel@trigram.co>

* feat: Adding admin ability to search by github_id (#1403)

* See nftstorage/admin.storage#68

* chore: optimise getUserByStorage query (#1405)

* fix: optimise getUserByStorage query to avoid timeouts (#1412)

* chore: update SQL migration to drop the old `users_by_storage_used` function before replacing (#1414)

* fix: update incorrect dag sizes job (#1059) (#1196)

* fix: 404 API http reference links (#1358)

Fixes: #1359

* fix(http docs): incorrect endpoint in description (#1429)

* fix(http docs): incorrect endpoint in description

* test: trigger a rebuild via CI

Co-authored-by: orun <orun@agencyundone.com>

* chore: add Wrangler worker env for Josh (#1351)

* chore: Add Wrangler worker env for Josh.

* chore: Add GATEWAY_URL to josh wrangler config.

* chore: Add default `GATEWAY_URL` var to wrangler template.

* chore(main): release website 2.5.1 (#1417)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: docs toc highlight on click (#1392)

* fix: toc highlight on click - docs

* fix: Replace document query with inline conditional classes and tweak scroll magic scene settings

* test: force rebuild

Co-authored-by: svvimming <benmunozj@gmail.com>
Co-authored-by: orun <orun@agencyundone.com>

* fix: refactor accordion content (#1391)

* fix: refactor accordion content

* style: reduce mobile padding below faq accordion

Co-authored-by: orun <orun@agencyundone.com>

* fix: Adding user_tag_proposal schema to reset.sql for local dev (#1445)

* Introduced in #1006

* feat: Split file manager table into uploaded & pinned (#1363)

* feat: Filemanager header file type tabs and split table by type

* fix: Storage manager progress bar double border & height

* feat: File manager search results in title + margins and spacing

* test: Console log all files

* test: Console log files and user storage data

* feat: Mock data for files using pinning service + api request for pinned files

* test: Fetch pinning data

* feat: Add loading state to pins tab table

* feat: Add loading state to pins tab table

* feat: Add uploaded/pinned tab url param

* test: Pinned fetch request

* test: Pinned fetch request

* revert 'test: Pinned fetch request'

* revert 'test: Pinned fetch request'

* test: Pinning fetch request with generated token

* revert 'test: Pinning fetch request with generated token'

* test: Pass generated API token to /pins GET request

* test: console log API

* feat: Disable pins table if no files are present & revert pins status to pinned in request

* chore: fix linting warning

* style: Responsive file manager header layout at small breakpoints

* feat: Account page UI minor tweaks

* feat: reduce font size (#1411)

* feat: Message bar incident/maintenance name (#1335)

* feat: Docs automatically generated files

* refactor: Message banner incident/maintenance message displays name

* chore: Remove testing materials

* chore: Remove testing materials

* Revert "chore: Remove testing materials"

This reverts commit ab5f9e9.

* Revert "refactor: Message banner incident/maintenance message displays name"

This reverts commit 7359dbe.

* Revert "feat: Docs automatically generated files"

This reverts commit 26ad9ca.

* refactor: Message banner incident/maintenance message displays name

* chore: Remove testing materials

* chore(main): release cron 1.1.1 (#1413)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: use NODE_TLS_REJECT_UNAUTHORIZED=0 env var for storage cron job (#1418)

This allows the direct connection to the Postgres DB to work.

* chore: add package-lock change report to PRs (#1453)

Bot to add comment on PRs with a human readable report of changes to package-lock.json

see: https://github.com/marketplace/actions/npm-lockfile-changes

License: (Apache-2.0 AND MIT)
Signed-off-by: Oli Evans <oli@tableflip.io>

* docs: clarify payload size limit for /car endpoint (#1457)

* chore: get back integrity and resolved keys for deps (#1456)

* chore: only check package-lock diff on PRs (#1460)

only check package-lock diff on PRs

see: #1453
which produces nice package-lock reports for our PRs but errors when run from not-a-pr.

License: (Apache-2.0 AND MIT)
Signed-off-by: Oli Evans <oli@tableflip.io>

* fix: Adjust progress bar styles for pinned files minimum cases

Co-authored-by: Joanna Ong <ongjfu@gmail.com>
Co-authored-by: Adam Alton <adamalton@gmail.com>
Co-authored-by: Paolo Chillari <flea89@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
Co-authored-by: e-schneid <99349687+e-schneid@users.noreply.github.com>
Co-authored-by: Gary Homewood <gary@potatolondon.com>
Co-authored-by: Paolo <paolo@potatolondon.com>
Co-authored-by: Oli Evans <oli@tableflip.io>
Co-authored-by: francois-potato <103063611+francois-potato@users.noreply.github.com>
Co-authored-by: Vasco Santos <santos.vasco10@gmail.com>
Co-authored-by: Josh Jarvis <josh.jarvis@potatolondon.com>
Co-authored-by: Joe Spencer <js.developer.undefined@gmail.com>
Co-authored-by: Joe Spencer <jsdevel@trigram.co>
Co-authored-by: Jorropo <jorropo.pgm@gmail.com>
Co-authored-by: Yusef Napora <yusef@napora.org>
Co-authored-by: orun <orun@agencyundone.com>
Co-authored-by: Hugo Dias <hugomrdias@gmail.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.

2 participants