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

[Major] Add server-side input validation #182

Conversation

dchege711
Copy link
Owner

@dchege711 dchege711 commented Jun 16, 2024

Install zod 1 for use in input parsing/validation. Bump validator,
which we use in conjunction with zod. We remove the custom
sanitizeQuery function.

Some functional fixes discovered while validating/parsing input:

  • The requireLogIn middleware handles the case where
    req.session.user is not set (due to express session requiring
    HTTPS). It falls back to re-logging in the user through the session
    token.
  • The requireLogIn middleware now passes req instead of
    req.cookies.session_token to the logInBySessionToken. Previously,
    this would have been a runtime error.
  • Make express-session use the secure option in production but not
    in dev. [ES] Run local/test app in HTTPS mode to mirror production #185 tracks being able to use HTTPS in dev and tests.
  • We forego calling nodemailer's sendMail function in tests and dev
    environments to avoid incurring costs from our email provider,
    Mailgun.
  • When requesting search results, the front end would send Infinity,
    which serializes to undefined over the wire. We now send 50
    instead.
  • Add a VS Code launch command for debugging server-side tests without
    timing out.

Copy link

render bot commented Jun 16, 2024

Looking for library-provided sanitization instead.
@dchege711 dchege711 force-pushed the user/dchege711/database/library-backed-no-sql-injection-protection branch from 2848ed9 to 3009e3c Compare June 24, 2024 01:32
dchege711 added 27 commits June 23, 2024 19:49
The NoSQL injection test fails with a 5XX error.
Depending on how we implement NoSQL injection protection, a tRPC-based test might
not catch the fix.
I can't prove that this works though.
No new packages added, just moved around. zod was already a grand-dep of the
@web/test-runner dev dependency.
Upgraded the package.
* Fetch a public card.
* Return null for a private card.
* Reject an empty body.
* Reject a body without required params.
* Resilient against extra parameters.
* Return null for non-existent cards.
Seems like chaiAsPromised ships as an ESM, which I'm unable to use on
the server-side.

```
TypeError: Unknown file extension ".ts" for /Users/dchege711/study_buddy/src/routes/InAppRouter.test.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at ModuleLoader.load (node:internal/modules/esm/loader:396:7)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:45)
    at link (node:internal/modules/esm/module_job:78:21)
```
dchege711 added 24 commits June 29, 2024 14:08
…esence of a validator

Not all endpoints are surfaces for NoSQL injection.
`req.cookies.session_token` is typed as `any` and that's why TS did not
catch this. The tests caught it though.
The account page using the browser's form POST action to request
updates. We don't use the tRPC endpoint.
…umber

Search results were previously not showing because of:

```log
Uncaught (in promise) TRPCClientError: [
  {
    "code": "invalid_type",
    "expected": "number",
    "received": "null",
    "path": [
      "limit"
    ],
    "message": "Expected number, received null"
  }
]
    at TRPCClientError.from (TRPCClientError.mjs:26:1)
    at createHTTPBatchLink.mjs:59:59
```
For local development, the app is served through HTTP and the cookie
ends up being not set. This makes logins not persist.
I've already sent 3,049 emails this month, which surpasses the 1K free
emails quota. I'll be charged at least $1.00 + $2.05.

A lot of these emails came from auto-initializing an account on dev
server launch, and initializing an account for each test runs. These
rack up pretty fast.
These clutter up the test run logs making it harder to identify real
issues.
I can't yet verify that it helps. FWIW, it doesn't catch injection
attacks on tRPC endpoints.

Will re-install after validating its effectiveness.
@dchege711 dchege711 changed the title [DB] Delete sanitizeQuery function from SanitizationAndValidation.ts [Major] Add server-side input validation Jun 30, 2024
@dchege711 dchege711 merged commit 464e421 into main Jun 30, 2024
3 of 4 checks passed
@dchege711 dchege711 deleted the user/dchege711/database/library-backed-no-sql-injection-protection branch June 30, 2024 18:59
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.

1 participant