Skip to content

Commit

Permalink
fix: limit origin length
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Jul 31, 2023
1 parent c031735 commit a3987a7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
59 changes: 57 additions & 2 deletions src/utils/__tests__/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { ConflictHeader, DateLabel, Label, Transaction } from '@safe-global/safe-gateway-typescript-sdk'
import { getQueuedTransactionCount } from '../transactions'
import type {
ConflictHeader,
DateLabel,
Label,
SafeAppData,
Transaction,
} from '@safe-global/safe-gateway-typescript-sdk'
import { getQueuedTransactionCount, getTxOrigin } from '../transactions'

describe('transactions', () => {
describe('getQueuedTransactionCount', () => {
Expand Down Expand Up @@ -59,4 +65,53 @@ describe('transactions', () => {
expect(getQueuedTransactionCount(txPage)).toBe('1')
})
})

describe('getTxOrigin', () => {
it('should return undefined if no app is provided', () => {
expect(getTxOrigin(undefined)).toBe(undefined)
})

it('should return a stringified object with the app name and url', () => {
const app = {
name: 'Test',
url: 'https://test.com',
} as SafeAppData

expect(getTxOrigin(app)).toBe('{"name":"Test","url":"https://test.com"}')
})

it('should limit the URL to 200 characters', () => {
const app = {
name: 'Test',
url: 'https://test.com/' + 'a'.repeat(1337),
} as SafeAppData

expect(JSON.stringify(app).length).toBeGreaterThan(200)

const result = getTxOrigin(app)

expect(result?.length).toBe(200)

expect(result).toBe(
'{"name":"Test","url":"https://test.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}',
)
})

it('should preserve the name when limiting', () => {
const app = {
name: 'Test' + 'a'.repeat(1337),
url: '',
} as SafeAppData

expect(JSON.stringify(app).length).toBeGreaterThan(200)

const result = getTxOrigin(app)

expect(result?.length).toBe(200)

expect(result).toBe(
'{"name":"Testaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","url":""}',
)
})
})
})
15 changes: 14 additions & 1 deletion src/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,27 @@ export const getQueuedTransactionCount = (txPage?: TransactionListPage): string
}

export const getTxOrigin = (app?: SafeAppData): string | undefined => {
const MAX_ORIGIN_LENGTH = 200

if (!app) {
return
}

let origin: string | undefined

try {
origin = JSON.stringify({ name: app.name, url: app.url })
const maxNameLength =
MAX_ORIGIN_LENGTH -
JSON.stringify({
name: '', // Must include empty string to avoid including the length of `undefined`
url: '',
}).length
const maxUrlLength = maxNameLength - app.name.length

origin = JSON.stringify({
name: app.name.slice(0, maxNameLength),
url: app.url.slice(0, maxUrlLength),
})
} catch (e) {
logError(Errors._808, e)
}
Expand Down

0 comments on commit a3987a7

Please sign in to comment.