Skip to content

Commit

Permalink
Brain: avoid retaining dangerous robot/brain references
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Mar 5, 2019
1 parent d2b57bd commit 58c70c0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ let reconstructUserIfNecessary = function (user, robot) {
// populating the new user with its values.
// Also add the `robot` field so it gets a reference.
user.robot = robot
let newUser = new User(id, user)
delete user.robot

return new User(id, user)
return newUser
} else {
return user
}
Expand All @@ -39,7 +41,9 @@ class Brain extends EventEmitter {
users: {},
_private: {}
}
this.robot = robot
this.getRobot = function () {
return robot
}

this.autoSave = true

Expand Down Expand Up @@ -146,7 +150,7 @@ class Brain extends EventEmitter {
if (data && data.users) {
for (let k in data.users) {
let user = this.data.users[k]
this.data.users[k] = reconstructUserIfNecessary(user, this.robot)
this.data.users[k] = reconstructUserIfNecessary(user, this.getRobot())
}
}

Expand All @@ -168,7 +172,7 @@ class Brain extends EventEmitter {
if (!options) {
options = {}
}
options.robot = this.robot
options.robot = this.getRobot()

if (!user) {
user = new User(id, options)
Expand All @@ -179,6 +183,7 @@ class Brain extends EventEmitter {
user = new User(id, options)
this.data.users[id] = user
}
delete options.robot

return user
}
Expand Down

0 comments on commit 58c70c0

Please sign in to comment.