Skip to content

Commit

Permalink
fix(connect): HAWNG-474 make sure only http(s) is used for connection…
Browse files Browse the repository at this point in the history
… scheme
  • Loading branch information
tadayosi committed Feb 2, 2024
1 parent 8ecb827 commit 97500b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/hawtio/src/plugins/connect/discover/discover-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class DiscoverService {
}

const url = new URL(agent.url)
conn.scheme = url.protocol.substring(0, url.protocol.length - 1) // strip last ':'
conn.scheme = this.schemeFromUrl(url)
conn.host = url.hostname
conn.port = parseInt(url.port)
conn.path = url.pathname
Expand All @@ -105,14 +105,20 @@ class DiscoverService {
return conn
}

private schemeFromUrl(url: URL): 'http' | 'https' {
const scheme = url.protocol.substring(0, url.protocol.length - 1) // strip last ':'
// Scheme other than 'http' or 'https' is not valid in the context of Jolokia agent
return scheme === 'http' || scheme === 'https' ? scheme : 'http'
}

jvmToConnection(jvm: Jvm): Connection {
const conn = { ...INITIAL_CONNECTION, name: `local-${jvm.port}` }
if (!jvm.scheme || !jvm.hostname || jvm.port === 0 || !jvm.path) {
log.warn('Lack of information to connect to JVM:', jvm)
return conn
}

conn.scheme = jvm.scheme
conn.scheme = jvm.scheme === 'http' || jvm.scheme === 'https' ? jvm.scheme : 'http'
conn.host = jvm.hostname
conn.port = jvm.port
conn.path = jvm.path
Expand Down
20 changes: 17 additions & 3 deletions packages/hawtio/src/plugins/shared/connect-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Connections = {

export type Connection = {
name: string
scheme: string
scheme: 'http' | 'https'
host: string
port: number
path: string
Expand Down Expand Up @@ -146,8 +146,22 @@ class ConnectService implements IConnectService {
}

loadConnections(): Connections {
const conns = localStorage.getItem(STORAGE_KEY_CONNECTIONS)
return conns ? JSON.parse(conns) : {}
const item = localStorage.getItem(STORAGE_KEY_CONNECTIONS)
if (!item) {
return {}
}
const conns: Connections = JSON.parse(item)

// Make sure scheme is not compromised for each connection
Object.values(conns).forEach(conn => {
if (conn.scheme !== 'http' && conn.scheme !== 'https') {
log.warn('Invalid scheme for connection:', conn)
// Force resetting to 'http' for any invalid scheme
conn.scheme = 'http'
}
})

return conns
}

saveConnections(connections: Connections) {
Expand Down

0 comments on commit 97500b3

Please sign in to comment.