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(connect): HAWNG-474 make sure only http(s) is used for connection scheme #761

Merged
merged 1 commit into from
Feb 2, 2024
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
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
Loading