Skip to content

Commit 84654ff

Browse files
committed
fix(elasticsearch): reject a malformed parent-domain port in a Cloud ID
1 parent 8643841 commit 84654ff

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎apps/sim/tools/elasticsearch/utils.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ describe('parseCloudId', () => {
6262
expect(() => parseCloudId(hostile)).toThrow(/Invalid Cloud ID/)
6363
})
6464

65+
it('rejects a malformed parent-domain port even when the service port is valid', () => {
66+
const hostile = cloudId(`${PARENT_DOMAIN}:abc$${ES_UUID}:9243$${KIBANA_UUID}`)
67+
expect(() => parseCloudId(hostile)).toThrow(/Invalid Cloud ID/)
68+
})
69+
70+
/**
71+
* `extractPortFromName` splits at the last colon, so a second colon survives
72+
* in the *name* half, where the `#@?/\` reject set does not catch it. Left
73+
* unchecked this builds `https://<uuid>.<host>:<port>:<port>`, which throws a
74+
* bare `TypeError: Invalid URL` inside the transport instead of naming the
75+
* real problem.
76+
*/
77+
it('rejects a second colon in the parent domain rather than building an invalid authority', () => {
78+
const hostile = cloudId(`${PARENT_DOMAIN}:9243:evil@x$${ES_UUID}:9243$${KIBANA_UUID}`)
79+
expect(() => parseCloudId(hostile)).toThrow(/Invalid Cloud ID/)
80+
})
81+
6582
it('rejects a payload with fewer than three $-separated components', () => {
6683
expect(() => parseCloudId(cloudId(`${PARENT_DOMAIN}$${ES_UUID}`))).toThrow(/Invalid Cloud ID/)
6784
})

‎apps/sim/tools/elasticsearch/utils.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const DEFAULT_CLOUD_PORT = '443'
1313
* Mirrors the `strings.IndexAny(component, "#@?/")` reject set in Beats, plus
1414
* `\`, which the WHATWG URL parser treats as a path separator for special
1515
* schemes and which therefore truncates the authority exactly as `/` does.
16+
*
17+
* A `:` is deliberately absent — `extractPortFromName` has already split each
18+
* component at its last colon, so a surviving colon means a second one, which
19+
* the all-digits port check below rejects.
1620
*/
1721
const CLOUD_ID_REJECTED_CHARACTERS = /[#@?/\\]/
1822

@@ -59,8 +63,10 @@ export function parseCloudId(cloudId: string): string {
5963
}
6064
}
6165

62-
if (!/^\d+$/.test(elasticsearch.port)) {
63-
throw new Error('Invalid Cloud ID format')
66+
for (const port of [parentDomain.port, elasticsearch.port]) {
67+
if (!/^\d+$/.test(port)) {
68+
throw new Error('Invalid Cloud ID format')
69+
}
6470
}
6571

6672
const host = `${elasticsearch.name}.${parentDomain.name}`

0 commit comments

Comments
 (0)