Skip to content

Commit

Permalink
feat: consistent safety
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Feb 25, 2025
1 parent fa2942a commit b07311e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
6 changes: 5 additions & 1 deletion react-app-rewired/headers/csps/wallets/walletMigration.ts
Original file line number Diff line number Diff line change
@@ -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': [
// friendly-challenge@0.9.1: 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,
],
}
4 changes: 2 additions & 2 deletions react-app-rewired/headers/types.ts
Original file line number Diff line number Diff line change
@@ -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[]
7 changes: 1 addition & 6 deletions react-app-rewired/headers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Csp, CspEntry } from './types'
export function cspToEntries(x: Csp): CspEntry[] {
return Object.entries(x).flatMap(([k, v]) => {
return v.map(entry => {
const out = [k, entry]
const out: [string, string] = [k, entry]
return out
})
})
Expand All @@ -24,18 +24,13 @@ export function entriesToCsp(x: CspEntry[]): Csp {
const acc: Csp = {}
return x
.sort(([k1, v1], [k2, v2]) => {
if (k1 === undefined || k2 === undefined) return 0
if (v1 === undefined || v2 === undefined) return 0

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

a[k] ??= []
if (v && (a[k].length === 0 || v !== "'none'") && !a[k].includes(v)) a[k].push(v)
if (a[k].length > 1) a[k] = a[k].filter(x => x !== "'none'")
Expand Down

0 comments on commit b07311e

Please sign in to comment.