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: validate bridge_url #232

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type IDKitConfig } from '@/types/config'
import { VerificationState } from '@/types/bridge'
import type { ISuccessResult } from '@/types/result'
import type { CredentialType } from '@/types/config'
import { validate_bridge_url } from './lib/validation'
import { encodeAction, generateSignal } from '@/lib/hashing'
import { AppErrorCodes, ResponseStatus } from '@/types/bridge'
import { decryptResponse, encryptRequest, exportKey, generateKey } from '@/lib/crypto'
Expand Down Expand Up @@ -58,7 +59,16 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((set, get) => ({
createClient: async ({ bridge_url, app_id, verification_level, action_description, action, signal }) => {
const { key, iv } = await generateKey()

const res = await fetch(`${bridge_url ?? DEFAULT_BRIDGE_URL}/request`, {
if (bridge_url) {
const validation = validate_bridge_url(bridge_url)
if (!validation.valid) {
console.error(validation.errors.join('\n'))
set({ verificationState: VerificationState.Failed })
throw new Error('Invalid bridge_url. Please check the console for more details.')
}
}

const res = await fetch(new URL('/request', bridge_url ?? DEFAULT_BRIDGE_URL), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(
Expand Down Expand Up @@ -102,7 +112,7 @@ export const useWorldBridgeStore = create<WorldBridgeStore>((set, get) => ({
const key = get().key
if (!key) throw new Error('No keypair found. Please call `createClient` first.')

const res = await fetch(`${get().bridge_url}/response/${get().requestId}`)
const res = await fetch(new URL(`/response/${get().requestId}`, get().bridge_url))

if (!res.ok) {
return set({
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export type ValidationResponse = { valid: true } | { valid: false; errors: string[] }

export function validate_bridge_url(bridge_url: string): ValidationResponse {
try {
new URL(bridge_url)
} catch (e) {
return { valid: false, errors: ['Failed to parse Bridge URL.'] }
}

const test_url = new URL(bridge_url)
const errors: string[] = []

if (test_url.protocol !== 'https:') {
errors.push('Bridge URL must use HTTPS.')
}
if (test_url.port) {
errors.push('Bridge URL must use the default port (443).')
}
if (test_url.pathname !== '/') {
errors.push('Bridge URL must not have a path.')
}
if (test_url.search) {
errors.push('Bridge URL must not have query parameters.')
}
if (test_url.hash) {
errors.push('Bridge URL must not have a fragment.')
}

// remove once restriction lifted in world app
if (!test_url.hostname.endsWith('worldcoin.org') && !test_url.hostname.endsWith('toolsforhumanity.com')) {
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
Fixed Show fixed Hide fixed
console.warn(
"Bridge URL should be a subdomain of worldcoin.org or toolsforhumanity.com. The user's identity wallet may refuse to connect. This is a temporary restriction and will be removed in the future."
0xPenryn marked this conversation as resolved.
Show resolved Hide resolved
)
}

if (errors.length) {
return { valid: false, errors }
}

return { valid: true }
}
Loading