Skip to content

Commit b64c425

Browse files
DF-21369 fix Utils.sanitize(url) to also handle partial urls (#3933)
* DF-21369 fix Utils.sanitize(url) to also handle partial urls i.e. those starting with '?' * review fix: run yarn format:fix --------- Co-authored-by: Matthew McAllister <matthew.mcallister@smartcontract.com>
1 parent ceb9ff5 commit b64c425

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

.changeset/weak-chairs-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/ice-adapter': patch
3+
---
4+
5+
fix url sanitize

packages/sources/ice/src/transport/netdania/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,20 @@ export class Utils {
105105
}
106106
}
107107

108-
static sanitize(url: string): string {
109-
const urlObj = new URL(url)
110-
if (urlObj.searchParams.has('h')) {
111-
urlObj.searchParams.set('h', 'redacted')
108+
static sanitize(urlStr: string): string {
109+
try {
110+
const url = new URL(urlStr)
111+
if (url.searchParams.has('h')) url.searchParams.set('h', 'redacted')
112+
return decodeURIComponent(url.toString())
113+
} catch {
114+
try {
115+
const url = new URL(`https://fake-but-irrelevant.com${urlStr}`)
116+
if (url.searchParams.has('h')) url.searchParams.set('h', 'redacted')
117+
return '?' + decodeURIComponent(url.searchParams.toString())
118+
} catch {
119+
return 'redacted'
120+
}
112121
}
113-
return urlObj.toString()
114122
}
115123
}
116124

packages/sources/ice/test/unit/transport-netdania-utils.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,26 @@ describe('PartialPriceUpdate', () => {
6060
)
6161
})
6262

63-
it('must sanitize a url by idempotently redacting the h parameter', () => {
64-
expect(Utils.sanitize('https://example.com/path?h=12345&otherParam=value')).toBe(
65-
'https://example.com/path?h=redacted&otherParam=value',
63+
it('must sanitize a full url by idempotently redacting the h parameter', () => {
64+
expect(Utils.sanitize('https://example.com/path?h=12345&otherParam=value&cb=?')).toBe(
65+
'https://example.com/path?h=redacted&otherParam=value&cb=?',
6666
)
6767

6868
const urlWithoutH = 'https://example.com/path?sessid=UP12345&otherParam=value'
6969
expect(Utils.sanitize(urlWithoutH)).toBe(urlWithoutH)
7070
})
71+
72+
it('must sanitize a partial url by idempotently redacting the h parameter', () => {
73+
expect(
74+
Utils.sanitize(
75+
'?xstream=1&v=5&dt=0&h=eyJnIjoiY2hhaW4ubGluayIsImFpIjoiTm9kZUpTQVBJdjEuNS4yIiwicHIiOjIsImF1IjoibG9jYWxob3N0OjgwODAiLCJxdXAiOjEsInAiOiJmYWtlLWFwaS1rZXkifQ..&xcmd=W3sidCI6MSwiaSI6MSwibSI6MSwicyI6IkVVUlVTRCIsInAiOiJpZGMifV0.&cb=?&ts=1752653143000',
76+
),
77+
).toBe(
78+
'?xstream=1&v=5&dt=0&h=redacted&xcmd=W3sidCI6MSwiaSI6MSwibSI6MSwicyI6IkVVUlVTRCIsInAiOiJpZGMifV0.&cb=?&ts=1752653143000',
79+
)
80+
81+
const urlWithoutH =
82+
'?xstream=1&v=5&dt=0&xcmd=W3sidCI6MSwiaSI6MSwibSI6MSwicyI6IkVVUlVTRCIsInAiOiJpZGMifV0.&cb=?&ts=1752653143000'
83+
expect(Utils.sanitize(urlWithoutH)).toBe(urlWithoutH)
84+
})
7185
})

0 commit comments

Comments
 (0)