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 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 52x 52x 52x 52x 52x 52x 1x 1x 52x 1x 1x 52x 52x 1x 1x 51x 51x 1x 1x 52x 52x 52x 52x 104x 2x 2x 102x 102x 102x 41x 2x 2x 102x 51x 51x 10x 51x 2x 2x 49x 61x 61x 4x 4x 57x 6x 6x 61x 10x 2x 2x 8x 8x 6x 6x 6x 2x 2x 2x 2x 52x 2x 2x 50x 50x 1x 1x 50x 50x 1x 1x 52x 16x 16x 11x 11x 16x 2x 2x | /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import "reflect-metadata"; import { FlowRulePort, FLOW_TARGET_TYPES_STR, Logger, LoggerFactory } from "shared_types"; import { TargetsDataSourceService } from "src/service/TargetsDataSourceService"; import { RuleGroupDataSourceService } from "src/service/RuleGroupDataSourceService"; import { FlowRuleInput } from "src/types/FlowRule"; import { inject, injectable } from "tsyringe"; import { InputValidator } from './InputValidator'; const SUPPORTED_PROTOCOLS = ['tcp', 'udp', 'icmp']; const SUPPORTED_ACTIONS = ['pass', 'drop', 'alert']; const RESERVED_OPTIONS = ['msg', 'sid'] const MAX_PORT_NUMBER = 65535; // key in known unsupported list https://docs.aws.amazon.com/network-firewall/latest/developerguide/suricata-limitations-caveats.html const KNOWN_UNSUPPORTED_OPTIONS = [ 'iprep', 'lua', 'geoip', 'filestore', 'fileext', 'filemagic', 'threshold', 'enip_command', 'cip_service', 'datarep', 'dataset'] @injectable() export class CreateRuleInputValidator extends InputValidator<FlowRuleInput> { PORT_RANGE_REGX = /\[(\d+):(\d.+)\]/; logger: Logger; constructor( @inject('LoggerFactory') loggerFactory: LoggerFactory, @inject('RuleGroupDataSourceService') private ruleGroupDataSourceService: RuleGroupDataSourceService, @inject('ObjectsDataSourceService') private objectsDataSourceService: TargetsDataSourceService, ) { super(); this.logger = loggerFactory.getLogger('CreateRuleInputValidator'); } protected async validate(input: FlowRuleInput): Promise<void> { if (!SUPPORTED_PROTOCOLS.includes(input.protocol)) { this.logger.info(`rule : protocol ${input.protocol} is not supported.`); this.errors.push(`rule : protocol ${input.protocol} is not supported.`); } if (!SUPPORTED_ACTIONS.includes(input.action)) { this.logger.info(`rule : action ${input.action} is not supported.`); this.errors.push(`rule : action ${input.action} is not supported.`); } this.validOptionFields(input); if (!input.ruleGroupId) { this.logger.info(`rule : rulegroup is missing`); this.errors.push(`rule : rulegroup is missing`); } else { const ruleGroup = await this.ruleGroupDataSourceService.getRuleGroupBy(input.ruleGroupId) if (!ruleGroup) { this.logger.info(`rule : rulegroup ${input.ruleGroupId} does not exists.`); this.errors.push(`rule : rulegroup ${input.ruleGroupId} does not exists.`); } } await this.validSrcAndDst(input); this.validRulePorts(input.sourcePort, 'sourcePort'); this.validRulePorts(input.destinationPort, 'destinationPort'); return; } private validRulePorts(port: FlowRulePort, portName: string) { if (!FLOW_TARGET_TYPES_STR.includes(port.type)) { this.logger.error(`Invalid ${portName} : port type [${port.type}] is not supported.`); this.errors.push(`Invalid ${portName} : port type [${port.type}] is not supported.`); } else { const portType = port.type; const portValue = port.value; if (portType === 'Any') { if (portValue) { this.logger.error(`Invalid ${portName} : port type [${port.type}] should not associated any value but port value provided [${portValue}].`); this.errors.push(`Invalid ${portName} : port type [${port.type}] should not associated any value but port value provided [${portValue}].`); } } if (portType === 'SinglePort') { this.validSinglePort(portValue, portName); } else if (portType === 'PortRange') { this.validPortRange(portValue, portName); } } } private validSinglePort(portValue: string | undefined, portName: string) { if (!portValue) { this.logger.error(`Invalid ${portName} : port value is empty`); this.errors.push(`Invalid ${portName} : port value is empty`); } else { this.validPort(portValue, portName); } } private validPort(rawPortValue: string, portName: string): number { const port = parseInt(rawPortValue); if (isNaN(port)) { this.logger.error(`Invalid ${portName} : port value [${rawPortValue}] is not a valid port value.`) this.errors.push(`Invalid ${portName} : port value [${rawPortValue}] is not a valid port value.`); } else if (port < 0 || port > MAX_PORT_NUMBER) { this.logger.error(`Invalid ${portName} : port value [${rawPortValue}] is not in range [0, ${MAX_PORT_NUMBER}].`) this.errors.push(`Invalid ${portName} : port value [${rawPortValue}] is not in range [0, ${MAX_PORT_NUMBER}].`); } return port; } private validPortRange(portValue: string | undefined, portName: string) { if (!portValue) { this.logger.error(`Invalid ${portName} : port value is empty`); this.errors.push(`Invalid ${portName} : port value is empty`); } else { const match = portValue.match(this.PORT_RANGE_REGX); if (match) { const from = this.validPort(match[1], portName); const to = this.validPort(match[2], portName); if (from > to) { this.logger.error(`Invalid ${portName} : port value ${portValue} contain from port greater than to port value.`); this.errors.push(`Invalid ${portName} : port value ${portValue} contain from port greater than to port value.`); } } else { this.logger.error(`Invalid ${portName} : port value ${portValue} is not a valid port range value.`); this.errors.push(`Invalid ${portName} : port value ${portValue} is not a valid port range value.`); } } } private async validSrcAndDst(input: FlowRuleInput) { if (!(input.source && input.destination)) { this.logger.info(`rule : src and/or destination is missing`); this.errors.push(`rule : src and/or destination is missing`); } else { const sourceObj = await this.objectsDataSourceService.getTargetBy(input.source); if (!sourceObj) { this.logger.info(`rule : src object ${input.source} does not exists.`); this.errors.push(`rule : src object ${input.source} does not exists.`); } const destinationObj = await this.objectsDataSourceService.getTargetBy(input.destination); if (!destinationObj) { this.logger.info(`rule : destination object ${input.destination} does not exists.`); this.errors.push(`rule : destination object ${input.destination} does not exists.`); } } } private validOptionFields(input: FlowRuleInput) { if (input.optionFields) { // check is valid pairs // check is allowed field input.optionFields.forEach(kp => { // key is if (KNOWN_UNSUPPORTED_OPTIONS.includes(kp.key)) { this.logger.info(`rule : option key ${kp.key} is not supported.`); this.errors.push(`rule : option key ${kp.key} is not supported.`); } if (RESERVED_OPTIONS.includes(kp.key)) { this.logger.info(`rule : option key ${kp.key} is reserved, can not be assigned.`); this.errors.push(`rule : option key ${kp.key} is reserved, can not be assigned.`); } }); } } } |