Skip to content

Commit

Permalink
peer checks replicator state before any db reads
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Sep 27, 2024
1 parent f946075 commit 1f1aef8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ class Peer {
}

onopen ({ seeks, capability }) {
if (this.replicator.destroyed) return

const expected = caps.replicate(this.stream.isInitiator === false, this.replicator.key, this.stream.handshakeHash)

if (b4a.equals(capability, expected) !== true) { // TODO: change this to a rejection instead, less leakage
Expand Down Expand Up @@ -605,6 +607,8 @@ class Peer {
}

async onsync ({ fork, length, remoteLength, canUpgrade, uploading, downloading, hasManifest }) {
if (this.replicator.destroyed) return

const lengthChanged = length !== this.remoteLength
const sameFork = fork === this.core.tree.fork

Expand Down Expand Up @@ -669,6 +673,8 @@ class Peer {

// Safe to call in the background - never fails
async _canUpgrade (remoteLength, remoteFork) {
if (this.replicator.destroyed) return false

if (remoteFork !== this.core.tree.fork) return false

if (remoteLength === 0) return true
Expand Down Expand Up @@ -708,6 +714,8 @@ class Peer {
async onrequest (msg) {
this.tracer.trace('onrequest', msg)

if (this.replicator.destroyed) return

const size = this.remoteRequests.size
this.remoteRequests.set(msg.id, msg)

Expand Down Expand Up @@ -848,6 +856,8 @@ class Peer {
async ondata (data) {
this.tracer.trace('ondata', data)

if (this.replicator.destroyed) return

// always allow a fork conflict proof to be sent
if (data.request === 0 && data.upgrade && data.upgrade.start === 0) {
if (await this.core.checkConflict(data, this)) return
Expand Down

0 comments on commit 1f1aef8

Please sign in to comment.