Skip to content

feat(DHT): NET-1436: Cut off connections to all application versions before v102.0.0 #3053

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/dht/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"lodash": "^4.17.21",
"lru-cache": "^11.0.2",
"node-datachannel": "^0.26.0",
"semver": "^7.7.1",
"uuid": "^11.1.0",
"websocket": "^1.0.34",
"ws": "^8.18.1"
Expand All @@ -62,6 +63,7 @@
"@types/heap": "^0.2.34",
"@types/k-bucket": "^5.0.1",
"@types/lodash": "^4.17.16",
"@types/semver": "^7.5.8",
"@types/websocket": "^1.0.10",
"@types/ws": "^8.18.0",
"jest-leak-detector": "^27.3.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/dht/protos/DhtRpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ message HandshakeResponse {
enum HandshakeError {
DUPLICATE_CONNECTION = 0;
INVALID_TARGET_PEER_DESCRIPTOR = 1;
UNSUPPORTED_PROTOCOL_VERSION = 2;
UNSUPPORTED_VERSION = 2;
}

// Wraps all messages
Expand Down
7 changes: 5 additions & 2 deletions packages/dht/src/connection/Handshaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IConnection } from './IConnection'
import { LOCAL_PROTOCOL_VERSION, isMaybeSupportedProtocolVersion } from '../helpers/version'
import { toNodeId } from '../identifiers'
import { PendingConnection } from './PendingConnection'
import semver from 'semver'
import { version as applicationVersion } from '../../package.json'

const logger = new Logger(module)
Expand Down Expand Up @@ -36,7 +37,7 @@ export const createOutgoingHandshaker = (
pendingConnection.off('disconnected', managedConnectionDisconnectedListener)
}
const handshakeFailedListener = (error?: HandshakeError) => {
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR || error === HandshakeError.UNSUPPORTED_PROTOCOL_VERSION) {
if (error === HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR || error === HandshakeError.UNSUPPORTED_VERSION) {
pendingConnection.close(false)
stopHandshaker()
} else {
Expand Down Expand Up @@ -177,7 +178,9 @@ export class Handshaker extends EventEmitter<HandshakerEvents> {
logger.trace('handshake response received')
const handshake = message.body.handshakeResponse
const error = !isMaybeSupportedProtocolVersion(handshake.protocolVersion)
? HandshakeError.UNSUPPORTED_PROTOCOL_VERSION : handshake.error
// All versions before 102.0.0 are not supported
|| semver.satisfies(semver.coerce(handshake.applicationVersion)!, '< 102.0.0')
? HandshakeError.UNSUPPORTED_VERSION : handshake.error
if (error !== undefined) {
this.emit('handshakeFailed', error)
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/dht/src/connection/webrtc/WebrtcConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class WebrtcConnector {
})
handshaker.on('handshakeRequest', (_sourceDescriptor: PeerDescriptor, remoteVersion: string) => {
if (!isMaybeSupportedProtocolVersion(remoteVersion)) {
rejectHandshake(pendingConnection!, connection, handshaker, HandshakeError.UNSUPPORTED_PROTOCOL_VERSION)
rejectHandshake(pendingConnection!, connection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
} else {
acceptHandshake(handshaker, pendingConnection, connection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class WebsocketServerConnector {
if (this.ongoingConnectRequests.has(nodeId)) {
const { pendingConnection, delFunc } = this.ongoingConnectRequests.get(nodeId)!
if (!isMaybeSupportedProtocolVersion(remoteProtocolVersion)) {
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_PROTOCOL_VERSION)
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
delFunc()
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
Expand All @@ -145,7 +145,7 @@ export class WebsocketServerConnector {
const pendingConnection = new PendingConnection(remotePeerDescriptor)

if (!isMaybeSupportedProtocolVersion(remoteProtocolVersion)) {
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_PROTOCOL_VERSION)
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
} else if (this.options.onNewConnection(pendingConnection)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dht/src/helpers/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const LOCAL_PROTOCOL_VERSION = '1.1'
*
* The older version assumes optimistically that it may be supported by the newer
* version. It can't know for sure, but the other node will tell if it is not
* supported (e.g. rejecting the handshake with UNSUPPORTED_PROTOCOL_VERSION error).
* supported (e.g. rejecting the handshake with UNSUPPORTED_VERSION error).
*/
export const isMaybeSupportedProtocolVersion = (remoteVersion: string): boolean => {
const localMajor = parseVersion(LOCAL_PROTOCOL_VERSION)!.major
Expand Down
2 changes: 1 addition & 1 deletion packages/dht/test/unit/Handshaker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('Handshaker', () => {
})

it('onHandshakeFailed unsupported version', () => {
handshaker.emit('handshakeFailed', HandshakeError.UNSUPPORTED_PROTOCOL_VERSION)
handshaker.emit('handshakeFailed', HandshakeError.UNSUPPORTED_VERSION)
expect(mockOnHandshakeCompleted).not.toHaveBeenCalled()
expect(mockPendingConnectionClose).toHaveBeenCalledTimes(1)
})
Expand Down