-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: all unverified inbound sessions #180
Merged
wemeetagain
merged 9 commits into
ChainSafe:master
from
acolytec3:all-unverified-sessions
May 25, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
413ce5c
feat: optionally allow inbound sessions with unverified ENRs
acolytec3 583d97c
Merge branch 'master' into all-unverified-sessions
acolytec3 2804e30
Revert nodeinfo changes
acolytec3 2377083
Address feedback on talkresp
acolytec3 198fe17
Merge branch 'all-unverified-sessions' of https://github.com/acolytec…
acolytec3 382a423
Optionally allow outbound sessions with invalid ENRs
acolytec3 7884905
Requested changes
acolytec3 77f79f7
Fix tests
acolytec3 2d72a07
make sessionService public
acolytec3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,7 +430,7 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte | |
this.send(nodeAddr, authPacket); | ||
|
||
// Notify the application that the session has been established | ||
this.emit("established", requestCall.contact.enr, connectionDirection); | ||
this.emit("established", nodeAddr, requestCall.contact.enr, connectionDirection, true); | ||
break; | ||
} | ||
case INodeContactType.Raw: { | ||
|
@@ -460,13 +460,10 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte | |
} | ||
|
||
/** | ||
* Verifies a Node ENR to its observed address. | ||
* If it fails, any associated session is also considered failed. | ||
* If it succeeds, we notify the application | ||
* Compares the ENR multiaddr to its observed address. | ||
* Returns true if they match | ||
*/ | ||
private verifyEnr(enr: ENR, nodeAddr: INodeAddress): boolean { | ||
// If the ENR does not match the observed IP addresses, | ||
// we consider the session failed. | ||
const enrMultiaddr = enr.getLocationMultiaddr("udp"); | ||
return enr.nodeId === nodeAddr.nodeId && (enrMultiaddr?.equals(nodeAddr.socketAddr) ?? true); | ||
} | ||
|
@@ -510,28 +507,34 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte | |
); | ||
|
||
// Receiving an AuthResponse must give us an up-to-date view of the node ENR. | ||
// Verify the ENR is valid | ||
if (this.verifyEnr(enr, nodeAddr)) { | ||
// Session is valid | ||
// Notify the application | ||
// The session established here are from WHOAREYOU packets that we sent. | ||
// This occurs when a node established a connection with us. | ||
this.emit("established", enr, ConnectionDirection.Incoming); | ||
|
||
this.newSession(nodeAddr, session); | ||
|
||
// decrypt the message | ||
this.handleMessage(src, { | ||
maskingIv: packet.maskingIv, | ||
header: createHeader( | ||
PacketType.Message, | ||
encodeMessageAuthdata({ srcId: nodeAddr.nodeId }), | ||
packet.header.nonce | ||
), | ||
message: packet.message, | ||
messageAd: encodeChallengeData(packet.maskingIv, packet.header), | ||
}); | ||
// Verify the ENR endpoint matches observed node address | ||
const verified = this.verifyEnr(enr, nodeAddr); | ||
|
||
// Drop session if invalid ENR and session service not configured to allow unverified sessions | ||
if (!verified && !this.config.allowUnverifiedSessions) { | ||
log("ENR contains invalid socket address. Dropping session with %o", nodeAddr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
this.failSession(nodeAddr, RequestErrorType.InvalidRemoteENR, true); | ||
return; | ||
} | ||
|
||
// Notify application | ||
// The session established here are from WHOAREYOU packets that we sent. | ||
// This occurs when a node established a connection with us. | ||
this.emit("established", nodeAddr, enr, ConnectionDirection.Outgoing, verified); | ||
|
||
this.newSession(nodeAddr, session); | ||
|
||
// decrypt the message | ||
this.handleMessage(src, { | ||
maskingIv: packet.maskingIv, | ||
header: createHeader( | ||
PacketType.Message, | ||
encodeMessageAuthdata({ srcId: nodeAddr.nodeId }), | ||
packet.header.nonce | ||
), | ||
message: packet.message, | ||
messageAd: encodeChallengeData(packet.maskingIv, packet.header), | ||
}); | ||
} catch (e) { | ||
if ((e as Error).name === ERR_INVALID_SIG) { | ||
log("Authentication header contained invalid signature. Ignoring packet from: %o", nodeAddr); | ||
|
@@ -619,15 +622,24 @@ export class SessionService extends (EventEmitter as { new (): StrictEventEmitte | |
if (message.type === MessageType.NODES) { | ||
// Received the requested ENR | ||
const enr = message.enrs.pop(); | ||
|
||
if (enr) { | ||
if (this.verifyEnr(enr, nodeAddr)) { | ||
// Notify the application | ||
// This can occur when we try to dial a node without an | ||
// ENR. In this case we have attempted to establish the | ||
// connection, so this is an outgoing connection. | ||
this.emit("established", enr, ConnectionDirection.Outgoing); | ||
// Verify the ENR endpoint matches observed node address | ||
const verified = this.verifyEnr(enr, nodeAddr); | ||
|
||
// Drop session if invalid ENR and session service not configured to allow unverified sessions | ||
if (!verified && !this.config.allowUnverifiedSessions) { | ||
log("ENR contains invalid socket address. Dropping session with %o", nodeAddr); | ||
this.failSession(nodeAddr, RequestErrorType.InvalidRemoteENR, true); | ||
return; | ||
} | ||
// Notify the application | ||
// This can occur when we try to dial a node without an | ||
// ENR. In this case we have attempted to establish the | ||
// connection, so this is an outgoing connection. | ||
this.emit("established", nodeAddr, enr, ConnectionDirection.Outgoing, verified); | ||
|
||
return; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏