Skip to content

Commit

Permalink
Add a couple of checks that would have prevented the sev1 on Saturday…
Browse files Browse the repository at this point in the history
…. If the response is not in the format we are expecting, we will succeed but log a warning
  • Loading branch information
wrandall22 committed Dec 2, 2024
1 parent 18e3bdc commit f416958
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
52 changes: 48 additions & 4 deletions src/common/components/Recaptcha/Recaptcha.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('Recaptcha component', () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({ success: true, action: 'read' })
json: () => Promise.resolve({ success: true, action: 'read', score: 0.9 })
})
})

Expand Down Expand Up @@ -220,11 +220,11 @@ describe('Recaptcha component', () => {
})
})

it('should not block gifts if something weird happens', async () => {
it('should not block gifts if data is empty', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve()
json: () => Promise.resolve({})
})
})

Expand All @@ -238,7 +238,51 @@ describe('Recaptcha component', () => {
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Data was missing!')
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', {})
})
})

it('should not block gifts if action is undefined', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({ success: true, score: 0.9 })
})
})

onSuccess.mockImplementation(() => console.log('success after weird'))

const { getByRole } = render(
buildRecaptcha()
)

await userEvent.click(getByRole('button'))
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', { success: true, score: 0.9 })
})
})

it('should not block gifts if score is undefined', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({ success: true, action: 'submit_gift' })
})
})

onSuccess.mockImplementation(() => console.log('success after weird'))

const { getByRole } = render(
buildRecaptcha()
)

await userEvent.click(getByRole('button'))
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', { success: true, action: 'submit_gift' })
})
})

Expand Down
11 changes: 6 additions & 5 deletions src/common/components/Recaptcha/Recaptcha.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export const Recaptcha = ({
})
const data = await serverResponse.json()

if (!data || !data.score || !data.action) {
$log.warn('Recaptcha returned an unusual response:', data)
onSuccess(componentInstance)
return
}

if (data?.success === true && isValidAction(data?.action)) {
if (data.score < 0.5) {
$log.warn(`Captcha score was below the threshold: ${data.score}`)
Expand All @@ -100,11 +106,6 @@ export const Recaptcha = ({
onSuccess(componentInstance)
return
}
if (!data) {
$log.warn('Data was missing!')
onSuccess(componentInstance)
return
}
if (!isValidAction(data?.action)) {
$log.warn(`Invalid action: ${data?.action}`)
onFailure(componentInstance)
Expand Down

0 comments on commit f416958

Please sign in to comment.