All files / src/utils validate.ts

97.18% Statements 69/71
95.65% Branches 44/46
100% Functions 10/10
97.18% Lines 69/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216                            43x   43x           43x                             43x             43x 43x   43x       1776x 57x     1719x 14x     1705x     43x       1704x       1704x       1704x       1704x   1704x     43x       1776x 72x           1704x 2x           1702x         43x       4990x       2x     4988x     43x       4989x 2x     4987x         1x         4986x 4986x 2x         4984x     43x       480x   480x 2x         478x     43x         438x 1x   437x   437x 436x 1x   435x 2x     433x 3208x     431x   1x     43x 888x 1x     887x 887x 1x     886x     43x 818x 1x     817x 817x 1x     816x    
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
import { isIP, isPort } from 'validator';
 
import {
	INCOMPATIBLE_NETWORK_REASON,
	INCOMPATIBLE_PROTOCOL_VERSION_REASON,
	INVALID_PEER_INFO_LIST_REASON,
	PEER_INFO_LIST_TOO_LONG_REASON,
} from '../constants';
import {
	InvalidNodeInfoError,
	InvalidPeerInfoError,
	InvalidPeerInfoListError,
	InvalidProtocolMessageError,
	InvalidRPCRequestError,
} from '../errors';
import {
	P2PCompatibilityCheckReturnType,
	P2PMessagePacket,
	P2PNodeInfo,
	P2PPeerInfo,
	P2PRequestPacket,
} from '../p2p_types';
 
import { getByteSize, sanitizeIncomingPeerInfo } from '.';
 
interface RPCPeerListResponse {
	readonly peers: ReadonlyArray<object>;
	readonly success?: boolean; // Could be used in future
}
 
const IPV4_NUMBER = 4;
const IPV6_NUMBER = 6;
 
const validateNetworkCompatibility = (
	peerInfo: P2PPeerInfo,
	nodeInfo: P2PNodeInfo,
): boolean => {
	if (!peerInfo.sharedState) {
		return false;
	}
 
	if (!peerInfo.sharedState.networkId) {
		return false;
	}
 
	return (peerInfo.sharedState.networkId as string) === nodeInfo.networkId;
};
 
const validateProtocolVersionCompatibility = (
	peerInfo: P2PPeerInfo,
	nodeInfo: P2PNodeInfo,
): boolean => {
	Iif (!peerInfo.sharedState) {
		return false;
	}
 
	Iif (typeof peerInfo.sharedState.protocolVersion !== 'string') {
		return false;
	}
 
	const peerHardForks = parseInt(
		peerInfo.sharedState.protocolVersion.split('.')[0],
		10,
	);
	const systemHardForks = parseInt(nodeInfo.protocolVersion.split('.')[0], 10);
 
	return systemHardForks === peerHardForks && peerHardForks >= 1;
};
 
export const validatePeerCompatibility = (
	peerInfo: P2PPeerInfo,
	nodeInfo: P2PNodeInfo,
): P2PCompatibilityCheckReturnType => {
	if (!validateNetworkCompatibility(peerInfo, nodeInfo)) {
		return {
			success: false,
			error: INCOMPATIBLE_NETWORK_REASON,
		};
	}
 
	if (!validateProtocolVersionCompatibility(peerInfo, nodeInfo)) {
		return {
			success: false,
			error: INCOMPATIBLE_PROTOCOL_VERSION_REASON,
		};
	}
 
	return {
		success: true,
	};
};
 
export const validatePeerAddress = (
	ipAddress: string,
	wsPort: number,
): boolean => {
	if (
		(!isIP(ipAddress, IPV4_NUMBER) && !isIP(ipAddress, IPV6_NUMBER)) ||
		!isPort(wsPort.toString())
	) {
		return false;
	}
 
	return true;
};
 
export const validatePeerInfo = (
	peerInfo: P2PPeerInfo | undefined,
	maxByteSize: number,
): P2PPeerInfo => {
	if (!peerInfo) {
		throw new InvalidPeerInfoError(`Invalid peer object`);
	}
 
	if (
		!peerInfo.ipAddress ||
		!peerInfo.wsPort ||
		!validatePeerAddress(peerInfo.ipAddress, peerInfo.wsPort)
	) {
		throw new InvalidPeerInfoError(
			`Invalid peer ipAddress or port for peer with ip: ${peerInfo.ipAddress} and wsPort ${peerInfo.wsPort}`,
		);
	}
 
	const byteSize = getByteSize(peerInfo);
	if (byteSize > maxByteSize) {
		throw new InvalidPeerInfoError(
			`PeerInfo is larger than the maximum allowed size ${maxByteSize} bytes`,
		);
	}
 
	return peerInfo;
};
 
export const validateNodeInfo = (
	nodeInfo: P2PNodeInfo,
	maxByteSize: number,
): void => {
	const byteSize = getByteSize(nodeInfo);
 
	if (byteSize > maxByteSize) {
		throw new InvalidNodeInfoError(
			`Invalid NodeInfo was larger than the maximum allowed ${maxByteSize} bytes`,
		);
	}
 
	return;
};
 
export const validatePeerInfoList = (
	rawBasicPeerInfoList: unknown,
	maxPeerInfoListLength: number,
	maxPeerInfoByteSize: number,
): ReadonlyArray<P2PPeerInfo> => {
	if (!rawBasicPeerInfoList) {
		throw new InvalidPeerInfoListError(INVALID_PEER_INFO_LIST_REASON);
	}
	const { peers } = rawBasicPeerInfoList as RPCPeerListResponse;
 
	if (Array.isArray(peers)) {
		if (peers.length === 0) {
			return [];
		}
		if (peers.length > maxPeerInfoListLength) {
			throw new InvalidPeerInfoListError(PEER_INFO_LIST_TOO_LONG_REASON);
		}
 
		const sanitizedPeerList = peers.map<P2PPeerInfo>(peerInfo =>
			validatePeerInfo(sanitizeIncomingPeerInfo(peerInfo), maxPeerInfoByteSize),
		);
 
		return sanitizedPeerList;
	}
	throw new InvalidPeerInfoListError(INVALID_PEER_INFO_LIST_REASON);
};
 
export const validateRPCRequest = (request: unknown): P2PRequestPacket => {
	if (!request) {
		throw new InvalidRPCRequestError('Invalid request');
	}
 
	const rpcRequest = request as P2PRequestPacket;
	if (typeof rpcRequest.procedure !== 'string') {
		throw new InvalidRPCRequestError('Request procedure name is not a string');
	}
 
	return rpcRequest;
};
 
export const validateProtocolMessage = (message: unknown): P2PMessagePacket => {
	if (!message) {
		throw new InvalidProtocolMessageError('Invalid message');
	}
 
	const protocolMessage = message as P2PMessagePacket;
	if (typeof protocolMessage.event !== 'string') {
		throw new InvalidProtocolMessageError('Protocol message is not a string');
	}
 
	return protocolMessage;
};