bip150: stricter parsing for known-peers and auth-peers.
This commit is contained in:
parent
78dfe005c7
commit
15adc17dea
@ -685,46 +685,55 @@ AuthDB.prototype.readKnown = async function readKnown() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
AuthDB.prototype.parseKnown = function parseKnown(text) {
|
AuthDB.prototype.parseKnown = function parseKnown(text) {
|
||||||
const lines = text.split(/\n+/);
|
assert(typeof text === 'string');
|
||||||
|
|
||||||
for (let line of lines) {
|
if (text.charCodeAt(0) === 0xfeff)
|
||||||
line = line.trim();
|
text = text.substring(1);
|
||||||
|
|
||||||
|
text = text.replace(/\r\n/g, '\n');
|
||||||
|
text = text.replace(/\r/g, '\n');
|
||||||
|
|
||||||
|
let num = 0;
|
||||||
|
|
||||||
|
for (const chunk of text.split('\n')) {
|
||||||
|
const line = chunk.trim();
|
||||||
|
|
||||||
|
num += 1;
|
||||||
|
|
||||||
if (line.length === 0)
|
if (line.length === 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (/^\s*#/.test(line))
|
if (line[0] === '#')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const parts = line.split(/\s+/);
|
const parts = line.split(/\s+/);
|
||||||
|
|
||||||
if (parts.length < 2)
|
if (parts.length < 2)
|
||||||
continue;
|
throw new Error(`No key present on line ${num}: "${line}".`);
|
||||||
|
|
||||||
const hostname = parts[0].trim().split(',');
|
const hosts = parts[0].split(',');
|
||||||
|
|
||||||
let host, ip;
|
let host, addr;
|
||||||
if (hostname.length >= 2) {
|
if (hosts.length >= 2) {
|
||||||
host = hostname[0];
|
host = hosts[0];
|
||||||
ip = hostname[1];
|
addr = hosts[1];
|
||||||
} else {
|
} else {
|
||||||
host = null;
|
host = null;
|
||||||
ip = hostname[0];
|
addr = hosts[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
const hex = parts[1].trim();
|
const key = Buffer.from(parts[1], 'hex');
|
||||||
const key = Buffer.from(hex, 'hex');
|
|
||||||
|
|
||||||
if (key.length !== 33)
|
if (key.length !== 33)
|
||||||
throw new Error(`Invalid key: ${parts[1]}.`);
|
throw new Error(`Invalid key on line ${num}: "${parts[1]}".`);
|
||||||
|
|
||||||
if (host && host.length > 0)
|
if (host && host.length > 0)
|
||||||
this.addKnown(host, key);
|
this.addKnown(host, key);
|
||||||
|
|
||||||
if (ip.length === 0)
|
if (addr.length === 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
this.addKnown(ip, key);
|
this.addKnown(addr, key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -762,21 +771,31 @@ AuthDB.prototype.readAuth = async function readAuth() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
AuthDB.prototype.parseAuth = function parseAuth(text) {
|
AuthDB.prototype.parseAuth = function parseAuth(text) {
|
||||||
const lines = text.split(/\n+/);
|
assert(typeof text === 'string');
|
||||||
|
|
||||||
for (let line of lines) {
|
if (text.charCodeAt(0) === 0xfeff)
|
||||||
line = line.trim();
|
text = text.substring(1);
|
||||||
|
|
||||||
|
text = text.replace(/\r\n/g, '\n');
|
||||||
|
text = text.replace(/\r/g, '\n');
|
||||||
|
|
||||||
|
let num = 0;
|
||||||
|
|
||||||
|
for (const chunk of text.split('\n')) {
|
||||||
|
const line = chunk.trim();
|
||||||
|
|
||||||
|
num += 1;
|
||||||
|
|
||||||
if (line.length === 0)
|
if (line.length === 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (/^\s*#/.test(line))
|
if (line[0] === '#')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const key = Buffer.from(line, 'hex');
|
const key = Buffer.from(line, 'hex');
|
||||||
|
|
||||||
if (key.length !== 33)
|
if (key.length !== 33)
|
||||||
throw new Error(`Invalid key: ${line}.`);
|
throw new Error(`Invalid key on line ${num}: "${line}".`);
|
||||||
|
|
||||||
this.addAuthorized(key);
|
this.addAuthorized(key);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user