From patchwork Mon May 20 09:06:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonatan Schlag X-Patchwork-Id: 7802 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4VjWsq4T4Wz3x0S for ; Mon, 20 May 2024 09:07:11 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4VjWsl6gytz19F; Mon, 20 May 2024 09:07:07 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4VjWsN0G9fz336w; Mon, 20 May 2024 09:06:48 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4VjWsK21Qfz32xd for ; Mon, 20 May 2024 09:06:45 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4VjWsG0Z4pz5n2; Mon, 20 May 2024 09:06:42 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1716196002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=amAJAI7GA6bRz9FeMwoo5ncLkRcc4Ie5GrNuhHsNjOI=; b=aKxncwS3J18gvJbs+73tboQ/jqXgK8B/RtmPzgingx6YpDRv+/gJCqfxrp01LT/56SFcPS M7P2e5SwtWMINWDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1716196002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=amAJAI7GA6bRz9FeMwoo5ncLkRcc4Ie5GrNuhHsNjOI=; b=p4v5U8ozDa7xdHQy4r4H0RmNSTQyeVe27QyP5iYe89+eC7zvWUGjMDnsPJobqgUegKfXWM qP5GbvW227O9bGYIlaua6iaO2BuyoYk26clfl++mqkhAR1BVH45HCZf2xeLRa4bAA6yYHP 4FSu7u1PBfRwA/OUHaVGN8D6pEhVYB4O5MEFkt3E2wwk7LK63yFXcfItSkBefjc6p3S07Z MrG4s8FXnMftimRq9kV2Ussb9YnAIeJ5S12HPsNycc+lT1VGdJ3wM+psWUQphmroLLN/Fc 6V34H1mhpiD0nMEvsdKmdNfXVp2tpH3vGADMsplKdm/N9U6hrHgaKGzs4Y7ptQ== From: Jonatan Schlag To: development@lists.ipfire.org Subject: [PATCH 17/21] initscripts fkt: ignore invalid keys in readhash Date: Mon, 20 May 2024 11:06:07 +0200 Message-Id: <20240520090611.10406-18-jonatan.schlag@ipfire.org> In-Reply-To: <20240520090611.10406-1-jonatan.schlag@ipfire.org> References: <20240520090611.10406-1-jonatan.schlag@ipfire.org> MIME-Version: 1.0 Message-ID-Hash: FFZWQWWZ42Q4OWTC2QGV5T3BJO6TRNAF X-Message-ID-Hash: FFZWQWWZ42Q4OWTC2QGV5T3BJO6TRNAF X-MailFrom: jonatan.schlag@ipfire.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: IPFire development talk Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Jonatan Schlag --- src/initscripts/system/functions | 6 ++++++ tests/src/initscripts/system/functions/data/2 | 20 +++++++++++++++++++ .../system/functions/data/2_output_stderr | 4 ++++ .../system/functions/data/2_output_stdout | 0 .../src/initscripts/system/functions/test.sh | 11 ++++++++++ 5 files changed, 41 insertions(+) create mode 100644 tests/src/initscripts/system/functions/data/2 create mode 100644 tests/src/initscripts/system/functions/data/2_output_stderr create mode 100644 tests/src/initscripts/system/functions/data/2_output_stdout diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions index 6107463fc..bbcfab95d 100644 --- a/src/initscripts/system/functions +++ b/src/initscripts/system/functions @@ -914,6 +914,12 @@ readhash() { local key="${line%=*}" local val="${line#*=}" + # Skip lines with an invalid key + if ! [[ ${key} =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then + echo "Invalid key '${key}'" >&2 + continue + fi + printf -v "${array}[${key}]" "%s" "${val}" done < "${file}" } diff --git a/tests/src/initscripts/system/functions/data/2 b/tests/src/initscripts/system/functions/data/2 new file mode 100644 index 000000000..3e1a7028b --- /dev/null +++ b/tests/src/initscripts/system/functions/data/2 @@ -0,0 +1,20 @@ +CONFIG_TYPE=3 +GREEN_DEV=green0 +GREEN_MACADDR=00:c0:08:8a:a0:47 +GREEN_DRIVER=r8175 +-RED_DEV=red0 +RE??D_MACADDR=00:c0:08:8a:a0:56 +RED&&_DRIVER=r8283 +# Another Comment +0BLUE_DEV='blue0 net0' +BLUE_MACADDR=bc:30:7d:58:6b:e3 +BLUE_DRIVER=rt2800 +RED_DHCP_HOSTNAME=ipfire +RED_DHCP_FORCE_MTU= +RED_ADDRESS=0.0.0.0 +RED_NETMASK=0.0.0.0 +RED_TYPE=PPPOE +RED_NETADDRESS=0.0.0.0 + +# Comment for testing + # Comment for testing Comments with spaces before diff --git a/tests/src/initscripts/system/functions/data/2_output_stderr b/tests/src/initscripts/system/functions/data/2_output_stderr new file mode 100644 index 000000000..dfcf2154b --- /dev/null +++ b/tests/src/initscripts/system/functions/data/2_output_stderr @@ -0,0 +1,4 @@ +Invalid key '-RED_DEV' +Invalid key 'RE??D_MACADDR' +Invalid key 'RED&&_DRIVER' +Invalid key '0BLUE_DEV' diff --git a/tests/src/initscripts/system/functions/data/2_output_stdout b/tests/src/initscripts/system/functions/data/2_output_stdout new file mode 100644 index 000000000..e69de29bb diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh index f1348a9e8..9981cc732 100755 --- a/tests/src/initscripts/system/functions/test.sh +++ b/tests/src/initscripts/system/functions/test.sh @@ -23,3 +23,14 @@ test_that_array_doesnt_have_key "CONFIG" "# Comment for testing Comments with sp test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1" readhash "CONFIG" "${SCRIPT_PATH}/data/1" test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2" readhash "CONFIG" "${SCRIPT_PATH}/data/1" + +# Check with invalid Lines (values and keys) + +readhash "CONFIG2" "${SCRIPT_PATH}/data/2" &> /dev/null + +# test if we read the correct data +test_that_key_in_arry_has_value "CONFIG2" "RED_DHCP_HOSTNAME" "ipfire" +test_that_key_in_arry_has_value "CONFIG2" "BLUE_MACADDR" "bc:30:7d:58:6b:e3" + +test_that_output_is "${SCRIPT_PATH}/data/2_output_stdout" "1" readhash "CONFIG2" "${SCRIPT_PATH}/data/2" +test_that_output_is "${SCRIPT_PATH}/data/2_output_stderr" "2" readhash "CONFIG2" "${SCRIPT_PATH}/data/2"