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

fix: limit origin length #2334

Merged
merged 3 commits into from
Aug 7, 2023
Merged
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
55 changes: 53 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,49 @@ describe('transactions', () => {
expect(getQueuedTransactionCount(txPage)).toBe('1')
})
})

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

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

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

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

const result = getTxOrigin(app)

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

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

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

const result = getTxOrigin(app)

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

expect(result).toBe(
'{"url":"https://test.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","name":""}',
)
})
})
})
usame-algan marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 10 additions & 1 deletion src/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,23 @@ 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 })
// Must include empty string to avoid including the length of `undefined`
const maxUrlLength = MAX_ORIGIN_LENGTH - JSON.stringify({ url: '', name: '' }).length
const trimmedUrl = app.url.slice(0, maxUrlLength)

const maxNameLength = Math.max(0, maxUrlLength - trimmedUrl.length)
const trimmedName = app.name.slice(0, maxNameLength)

origin = JSON.stringify({ url: trimmedUrl, name: trimmedName })
} catch (e) {
logError(Errors._808, e)
}
Expand Down
Loading