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

feat: don't non-null assert CSPs #8918

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

gomesalexandre
Copy link
Contributor

Description

Continues on the work started in #8912 - only ~150 to go down from 300 when starting!

Issue (if applicable)

relates to #8909

Risk

High Risk PRs Require 2 approvals

What protocols, transaction types, wallets or contract interactions might be affected by this PR?

Medium - this adds runtime checks for CSPs, ensure that both the dev build and actual build are not throwing on undefined CSPs

Testing

Engineering

  • Test both dev build and dist build and ensure things are looking gucci
  • You may see some CORS errors which are not related to this PR i.e those are actual upstream failures reflected as CORS, pretty easy to spot as the same URI works fine on other requests

Operations

  • 🏁 My feature is behind a flag and doesn't require operations testing (yet)
  • Do a smoke test of the app and ensure nothing breaks

Screenshots (if applicable)

@gomesalexandre gomesalexandre requested a review from a team as a code owner February 21, 2025 12:25
Base automatically changed from feat_non_null_assert_1 to develop February 24, 2025 05:02
Copy link
Member

@0xApotheosis 0xApotheosis left a comment

Choose a reason for hiding this comment

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

Runtime test looking good, tested app and dev env vars whilst serving the build locally.

These guys spooked me a bit, but they were both ephemeral and reproducible on develop.

Screenshot 2025-02-24 at 17 03 17

Requesting changes to the type definitions as we have the opportunity to keep them strict and ensure the pattern you've implemented throughout this PR is enforced.

@@ -4,6 +4,6 @@ export const csp: Csp = {
'connect-src': [
// [email protected]: https://github.com/FriendlyCaptcha/friendly-challenge/blob/f110634f17f316b90a9bd59b190291abe3c639bb/src/captcha.ts#L20
'https://api.friendlycaptcha.com/api/v1/puzzle',
process.env.REACT_APP_WALLET_MIGRATION_URL!,
process.env.REACT_APP_WALLET_MIGRATION_URL,
Copy link
Member

Choose a reason for hiding this comment

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

We missed our throw logic on this guy.

Copy link
Member

Choose a reason for hiding this comment

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

diff --git a/react-app-rewired/headers/csps/wallets/walletMigration.ts b/react-app-rewired/headers/csps/wallets/walletMigration.ts
index 9dbdd615ff..ba6a53ddd4 100644
--- a/react-app-rewired/headers/csps/wallets/walletMigration.ts
+++ b/react-app-rewired/headers/csps/wallets/walletMigration.ts
@@ -1,9 +1,13 @@
 import type { Csp } from '../../types'

+const REACT_APP_WALLET_MIGRATION_URL = process.env.REACT_APP_WALLET_MIGRATION_URL
+
+if (!REACT_APP_WALLET_MIGRATION_URL) throw new Error('REACT_APP_WALLET_MIGRATION_URL is required')
+
 export const csp: Csp = {
   'connect-src': [
     // [email protected]: https://github.com/FriendlyCaptcha/friendly-challenge/blob/f110634f17f316b90a9bd59b190291abe3c639bb/src/captcha.ts#L20
     'https://api.friendlycaptcha.com/api/v1/puzzle',
-    process.env.REACT_APP_WALLET_MIGRATION_URL,
+    REACT_APP_WALLET_MIGRATION_URL,
   ],
 }

Copy link
Member

Choose a reason for hiding this comment

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

Confirming runtime is happy with the below type changes once the diff above is applied 🙏

diff --git a/react-app-rewired/headers/types.ts b/react-app-rewired/headers/types.ts
index e92c32b013..d6eecf75d9 100644
--- a/react-app-rewired/headers/types.ts
+++ b/react-app-rewired/headers/types.ts
@@ -1,3 +1,3 @@
 // process.env is not properly typed but could be if we were using vite imports.meta. wen vite?
-export type Csp = Record<string, (string | undefined)[]>
-export type CspEntry = (string | undefined)[]
+export type Csp = Record<string, string[]>
+export type CspEntry = string[]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All applied in b07311e @0xApotheosis, v. nice catch for even more safety!

Comment on lines 2 to 3
export type Csp = Record<string, (string | undefined)[]>
export type CspEntry = (string | undefined)[]
Copy link
Member

Choose a reason for hiding this comment

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

Wait, didn't we just do all the work to confirm that the values in the list are defined?

Suggested change
export type Csp = Record<string, (string | undefined)[]>
export type CspEntry = (string | undefined)[]
export type Csp = Record<string,(string[]>
export type CspEntry = string[]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 27 to 28
if (k1 === undefined || k2 === undefined) return 0
if (v1 === undefined || v2 === undefined) return 0
Copy link
Member

Choose a reason for hiding this comment

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

Then we don't need these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (k1 < k2) return -1
if (k1 > k2) return 1
if (v1 < v2) return -1
if (v1 > v2) return 1
return 0
})
.reduce((a, [k, v]) => {
if (!k) return a
Copy link
Member

Choose a reason for hiding this comment

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

or this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gomesalexandre
Copy link
Contributor Author

@0xApotheosis FYI moving to draft until #8924 gets in, see https://github.com/shapeshift/web/pull/8924/files#r1970831176, bigly mergefix incoming

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