Bug logic in whitelisted pubkey check
Closed this issue · 0 comments
atrifat commented
Kirino-san, there is bug logic in whitelisted pubkey check feature that i have proposed before in #2
The following test will show that despite the pubkey (pseudo pubkey) is true but the old implementation will give false result. It happens because the second and the third loop check change the shouldRelay=false
const event = ["", { pubkey: "alice" }];
const whitelistedPubkeys = ["alice", "bob", "charlie"];
let shouldRelay = true;
let because = "";
for (const allowed of whitelistedPubkeys) {
if (event[1].pubkey !== allowed) {
shouldRelay = false;
because = "Only whitelisted pubkey can write events";
// break;
} else {
shouldRelay = true;
because = "";
}
}
console.log("old implementation", "shouldRelay: ", shouldRelay);
I have fixed that in new implementation as follows
const event = ["", { pubkey: "alice" }];
const whitelistedPubkeys = ["alice", "bob", "charlie"];
let shouldRelay = true;
let because = "";
if (shouldRelay && whitelistedPubkeys.length > 0) {
const isWhitelistPubkey = whitelistedPubkeys.includes(event[1].pubkey);
if (!isWhitelistPubkey) {
shouldRelay = false;
because = "Only whitelisted pubkey can write events";
}
}
console.log("new implementation", "shouldRelay: ", shouldRelay);
I will send the PR shortly.
Thank you.