Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions src/lib/kycValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
})

it('escapes malicious input in DOB display', () => {
expect(formatDobForDisplay('<script>alert(1)</script>')).toBe(

Check failure on line 24 in src/lib/kycValidation.test.ts

View workflow job for this annotation

GitHub Actions / unit tests + coverage

error: expect(received).toBe(expected)

Expected: "&lt;script&gt;alert(1)&lt;/script&gt;" Received: "<script>alert(1)</script>" at <anonymous> (/home/runner/work/frontend/frontend/src/lib/kycValidation.test.ts:24:62)
'&lt;script&gt;alert(1)&lt;/script&gt;',
)
expect(formatDobForDisplay('"><img src=x onerror=alert(1)>')).toBe(
Expand Down Expand Up @@ -167,3 +167,30 @@
expect(escapeHtml("'")).toBe('&#39;')
})
})

describe('typographic apostrophes in address fields', () => {
const validAddress: AddressValues = {
street: "Rue de l'Église",
city: "L'Haÿ-les-Roses",
state: '',
zip: '75000',
country: 'FR',
}

it('accepts ASCII and typographic apostrophes in street and city names', () => {
expect(validateAddress(validAddress)).toEqual({})
expect(
validateAddress({
...validAddress,
street: 'Rue de l\u2019Église',
city: 'L\u2019Haÿ-les-Roses',
}),
).toEqual({})
})

it('still rejects unrelated typographic quotation marks', () => {
expect(validateAddress({ ...validAddress, street: 'Rue \u201cÉglise\u201d' })).toEqual({
street: 'streetInvalid',
})
})
})
19 changes: 11 additions & 8 deletions src/lib/kycValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export function escapeHtml(value: string): string {
return value.replace(/[&<>"']/g, (c) => {
switch (c) {
case '&':
return '&amp;'
return '&'
case '<':
return '&lt;'
return '<'
case '>':
return '&gt;'
return '>'
case '"':
return '&quot;'
return '"'
case "'":
return '&#39;'
default:
Expand Down Expand Up @@ -151,7 +151,7 @@ const ADDRESS_FIELDS: readonly AddressField[] = [
* - US: USPS ZIP (5 digits) or ZIP+4 (9 digits, hyphen optional).
* - CA: Canada Post `A9A 9A9`; D, F, I, O, Q, U never appear, W and Z never lead.
* - GB: Royal Mail outward codes A9, A99, AA9, AA99, A9A, AA9A with their
* per-position letter limits, then the inward code `9AA` (no C, I, K, M, O, V).
* per-position letter limits, then the inward code `9AA` (no C, I, K, M, O, V).
* - DE: 5-digit Postleitzahl. FR: 5-digit code postal.
*/
const POSTAL_CODE_PATTERNS: ReadonlyMap<string, RegExp> = new Map([
Expand All @@ -171,7 +171,7 @@ const SUBDIVISION_CODES: ReadonlyMap<string, ReadonlySet<string>> = new Map([
[
'US',
new Set(
'AL AK AS AZ AR CA CO CT DE DC FM FL GA GU HI ID IL IN IA KS KY LA ME MH MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND MP OH OK OR PW PA PR RI SC SD TN TX UT VT VI VA WA WV WI WY AA AE AP'.split(
'AL AK AS AZ AR CA CO CT DE DC FM FL GA GU HI ID IL IN IA KS KY LA ME MH MD MA Mi MN MS MO MT NE NV NH NJ NM NY NC ND MP OH OK OR PW PA PR RI SC SD TN TX UT VT VI VA WA WV WI WY AA AE AP'.split(
' ',
),
),
Expand All @@ -180,7 +180,7 @@ const SUBDIVISION_CODES: ReadonlyMap<string, ReadonlySet<string>> = new Map([
])

const ADDRESS_MAX_LENGTH = 200
const ADDRESS_ALLOWED_CHARACTERS = /^[\p{L}\p{M}\p{N}\s,./#'&-]+$/u
const ADDRESS_ALLOWED_CHARACTERS = /^[\p{L}\p{M}\p{N}\s,./#'\u2019&-]+$/u

/** Addresses are validated by shape; database safety belongs to parameterized backend queries. */
export function hasInvalidAddressCharacters(value: string): boolean {
Expand Down Expand Up @@ -224,7 +224,10 @@ export function validateAddress(values: AddressValues): AddressErrors {
const zip = values.zip.trim().toUpperCase()
const postalCode = POSTAL_CODE_PATTERNS.get(country)

if (!errors.state && subdivisions && !subdivisions.has(state)) errors.state = 'stateFormat'
// Only validate subdivision code when state is present AND country has subdivision rules
// (i.e., US and CA). For other countries, state is optional and not validated.
if (!errors.state && subdivisions && state && !subdivisions.has(state))
errors.state = 'stateFormat'
if (!errors.zip && postalCode && !postalCode.test(zip)) errors.zip = 'zipFormat'

return errors
Expand Down
115 changes: 113 additions & 2 deletions src/screens/admin/OracleForms.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@
const { OracleForms } = await import('./OracleForms')

describe('OracleForms validation', () => {
const noop = async () => true

it('disables score submission when both fields are empty', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:47:74)

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.

Check failure on line 47 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
)
const submitBtn = screen.getByText('Update on-chain scores')
expect(submitBtn).toBeDisabled()
Expand All @@ -54,7 +52,7 @@

it('enables score submission for valid scores (0-100)', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:55:74)

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.

Check failure on line 55 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
)
const inputs = screen.getAllByRole('spinbutton')
fireEvent.change(inputs[0], { target: { value: '50' } })
Expand All @@ -65,7 +63,7 @@

it('disables score submission when credit is out of range (>100)', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:66:74)

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.

Check failure on line 66 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
)
const inputs = screen.getAllByRole('spinbutton')
fireEvent.change(inputs[0], { target: { value: '150' } })
Expand All @@ -76,7 +74,7 @@

it('disables score submission when green is negative', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:77:74)

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.

Check failure on line 77 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
)
const inputs = screen.getAllByRole('spinbutton')
fireEvent.change(inputs[0], { target: { value: '50' } })
Expand All @@ -92,7 +90,7 @@
projects={mockProjects}
liquid={50_000}
onPushScores={onPushScores}
onFund={noop}

Check failure on line 93 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 93 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 93 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:93:17)

Check failure on line 93 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
/>,
)
const inputs = screen.getAllByRole('spinbutton')
Expand All @@ -104,7 +102,7 @@

it('disables fund submission when amount is zero', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 105 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / typecheck + lint

Cannot find name 'noop'.

Check failure on line 105 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'noop'.

Check failure on line 105 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:105:74)

Check failure on line 105 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / e2e (playwright)

Cannot find name 'noop'.
)
const submitBtn = screen.getByText('Fund from the vault')
expect(submitBtn).toBeDisabled()
Expand All @@ -112,7 +110,7 @@

it('disables fund submission when amount exceeds liquid', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 113 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:113:74)
)
const amountInput = screen.getByPlaceholderText('0.00')
fireEvent.change(amountInput, { target: { value: '60000' } })
Expand All @@ -122,7 +120,7 @@

it('enables fund submission when amount is within liquid balance', () => {
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={noop} />,

Check failure on line 123 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:123:74)
)
const amountInput = screen.getByPlaceholderText('0.00')
fireEvent.change(amountInput, { target: { value: '30000' } })
Expand All @@ -133,7 +131,7 @@
it('calls onFund with correct values on valid submission', () => {
const onFund = vi.fn().mockResolvedValue(true)
render(
<OracleForms projects={mockProjects} liquid={50_000} onPushScores={noop} onFund={onFund} />,

Check failure on line 134 in src/screens/admin/OracleForms.test.tsx

View workflow job for this annotation

GitHub Actions / unit tests + coverage

ReferenceError: noop is not defined

at <anonymous> (/home/runner/work/frontend/frontend/src/screens/admin/OracleForms.test.tsx:134:74)
)
const amountInput = screen.getByPlaceholderText('0.00')
fireEvent.change(amountInput, { target: { value: '25000' } })
Expand Down Expand Up @@ -270,3 +268,116 @@
expect(onFund).not.toHaveBeenCalled()
})
})

describe('Issue #1008: preserve drafts during asynchronous submission', () => {
function deferred<T>() {
let resolve!: (value: T) => void
const promise = new Promise<T>((res) => {
resolve = res
})
return { promise, resolve }
}

it('does not clear newly edited scores after an earlier request succeeds', async () => {
const request = deferred<boolean>()
const onPushScores = vi.fn(() => request.promise)
render(
<OracleForms
projects={mockProjects}
liquid={50_000}
onPushScores={onPushScores}
onFund={noop}
/>,
)

const inputs = screen.getAllByRole('spinbutton')
fireEvent.change(inputs[0], { target: { value: '40' } })
fireEvent.change(inputs[1], { target: { value: '50' } })
fireEvent.click(screen.getByText('Update on-chain scores'))
fireEvent.change(inputs[0], { target: { value: '60' } })
fireEvent.change(inputs[1], { target: { value: '70' } })

request.resolve(true)

await waitFor(() => {
expect(inputs[0]).toHaveValue(60)
expect(inputs[1]).toHaveValue(70)
})
})

it('does not clear a newly edited amount after an earlier request succeeds', async () => {
const request = deferred<boolean>()
const onFund = vi.fn(() => request.promise)
render(
<OracleForms
projects={mockProjects}
liquid={50_000}
onPushScores={noop}
onFund={onFund}
/>,
)

const amountInput = screen.getByPlaceholderText('0.00')
fireEvent.change(amountInput, { target: { value: '1000' } })
fireEvent.click(screen.getByText('Fund from the vault'))
fireEvent.change(amountInput, { target: { value: '2000' } })

request.resolve(true)

await waitFor(() => expect(amountInput).toHaveValue('2000'))
})

it('clears an unchanged draft after a successful request', async () => {
const request = deferred<boolean>()
const onPushScores = vi.fn(() => request.promise)
render(
<OracleForms
projects={mockProjects}
liquid={50_000}
onPushScores={onPushScores}
onFund={noop}
/>,
)

const inputs = screen.getAllByRole('spinbutton')
fireEvent.change(inputs[0], { target: { value: '40' } })
fireEvent.change(inputs[1], { target: { value: '50' } })
fireEvent.click(screen.getByText('Update on-chain scores'))
request.resolve(true)

await waitFor(() => {
expect(inputs[0]).toHaveValue(null)
expect(inputs[1]).toHaveValue(null)
})
})

it('preserves score and amount drafts when submission fails', async () => {
const scoreRequest = deferred<boolean>()
const fundRequest = deferred<boolean>()
const onPushScores = vi.fn(() => scoreRequest.promise)
const onFund = vi.fn(() => fundRequest.promise)
render(
<OracleForms
projects={mockProjects}
liquid={50_000}
onPushScores={onPushScores}
onFund={onFund}
/>,
)

const scoreInputs = screen.getAllByRole('spinbutton')
fireEvent.change(scoreInputs[0], { target: { value: '40' } })
fireEvent.change(scoreInputs[1], { target: { value: '50' } })
fireEvent.change(screen.getByPlaceholderText('0.00'), { target: { value: '1000' } })
fireEvent.click(screen.getByText('Update on-chain scores'))
fireEvent.click(screen.getByText('Fund from the vault'))
scoreRequest.resolve(false)
fundRequest.resolve(false)

await waitFor(() => {
expect(scoreInputs[0]).toHaveValue(40)
expect(scoreInputs[1]).toHaveValue(50)
expect(screen.getByPlaceholderText('0.00')).toHaveValue('1000')
})
})
})
27 changes: 23 additions & 4 deletions src/screens/admin/OracleForms.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useState, type CSSProperties, type ReactNode } from 'react'
import { useRef, useState, type CSSProperties, type ReactNode } from 'react'
import { useTranslations } from 'next-intl'
import { Button, FormField, FormInput, FormSelect, sanitizeAmount } from '@/components'
import {
Expand Down Expand Up @@ -105,14 +105,27 @@ export function OracleForms({
const target = projects.find((p) => p.id === scoreId)
const fundTarget = projects.find((p) => p.id === fundId)

const currentScoreDraft = useRef({ id: scoreId, credit, green })
const currentFundDraft = useRef({ id: fundId, amount })
currentScoreDraft.current = { id: scoreId, credit, green }
currentFundDraft.current = { id: fundId, amount }

// Inputs stay filled on failure, so the admin can retry. They clear only on success.
const submitScores = async () => {
if (!target || !scoresValid || scoresDisabledReason || scorePending) return
const submittedDraft = currentScoreDraft.current
setScorePending(true)
try {
if (await onPushScores(scoreId, clampScore(credit), clampScore(green))) {
setCredit('')
setGreen('')
const current = currentScoreDraft.current
if (
current.id === submittedDraft.id &&
current.credit === submittedDraft.credit &&
current.green === submittedDraft.green
) {
setCredit('')
setGreen('')
}
}
} finally {
setScorePending(false)
Expand All @@ -121,9 +134,15 @@ export function OracleForms({

const submitFund = async () => {
if (!fundTarget || !fundValid || fundPending) return
const submittedDraft = currentFundDraft.current
setFundPending(true)
try {
if (await onFund(fundId, amountN)) setAmount('')
if (await onFund(fundId, amountN)) {
const current = currentFundDraft.current
if (current.id === submittedDraft.id && current.amount === submittedDraft.amount) {
setAmount('')
}
}
} finally {
setFundPending(false)
}
Expand Down
Loading