Skip to content

Commit 454db63

Browse files
committed
better error reporting/creation of /usr/share/nftables.d/ruleset-post
1 parent e59e6ab commit 454db63

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include $(TOPDIR)/rules.mk
33

44
PKG_NAME:=https-dns-proxy
55
PKG_VERSION:=2026.03.18
6-
PKG_RELEASE:=3
6+
PKG_RELEASE:=4
77

88
PKG_SOURCE_PROTO:=git
99
PKG_SOURCE_URL:=https://github.com/aarond10/https_dns_proxy/
@@ -58,6 +58,7 @@ define Package/https-dns-proxy/install
5858
$(INSTALL_CONF) ./files/etc/config/https-dns-proxy $(1)/etc/config/https-dns-proxy
5959
$(INSTALL_DIR) $(1)/etc/uci-defaults/
6060
$(INSTALL_BIN) ./files/etc/uci-defaults/50-https-dns-proxy-migrate-options.sh $(1)/etc/uci-defaults/50-https-dns-proxy-migrate-options.sh
61+
$(INSTALL_DIR) $(1)/usr/share/nftables.d/ruleset-post
6162
endef
6263

6364
$(eval $(call BuildPackage,https-dns-proxy))

files/etc/init.d/https-dns-proxy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ uci_changes() {
136136
[ -n "$(/sbin/uci ${UCI_CONFIG_DIR:+-c ${UCI_CONFIG_DIR}} changes "$PACKAGE${CONFIG:+.${CONFIG}}${OPTION:+.${OPTION}}")" ]
137137
}
138138
notrack_nft() {
139+
command -v nft >/dev/null 2>&1 || return 0
139140
case "$1" in
140141
update)
141142
local port_set="$2"
@@ -154,8 +155,14 @@ notrack_nft() {
154155
)"
155156
existing_content="$(cat "$NOTRACK_NFT_FILE" 2>/dev/null)"
156157
if [ "$new_content" != "$existing_content" ]; then
157-
mkdir -p "${NOTRACK_NFT_FILE%/*}"
158-
echo "$new_content" > "$NOTRACK_NFT_FILE"
158+
if ! mkdir -p "${NOTRACK_NFT_FILE%/*}"; then
159+
logger -t "$packageName" "Failed to create ${NOTRACK_NFT_FILE%/*}; skipping notrack rules"
160+
return 1
161+
fi
162+
if ! echo "$new_content" > "$NOTRACK_NFT_FILE"; then
163+
logger -t "$packageName" "Failed to write $NOTRACK_NFT_FILE; skipping notrack rules"
164+
return 1
165+
fi
159166
fi
160167
[ -s "$NOTRACK_NFT_FILE" ] && nft -c -f "$NOTRACK_NFT_FILE"
161168
;;

tests/run_tests.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,41 @@ notrack_nft remove
704704
assert_rc "notrack_nft remove succeeds when file and table both absent" 0 $?
705705
__nft_rc=0
706706

707+
# ── nft binary absent: notrack_nft is a no-op ──
708+
# Without firewall4/nftables installed, the package should not error;
709+
# `command -v nft` returns non-zero and notrack_nft returns 0 immediately.
710+
rm -rf "$TESTDIR/usr/share"
711+
__saved_nft_def="$(typeset -f nft 2>/dev/null || declare -f nft)"
712+
unset -f nft
713+
mkdir -p "$TESTDIR/empty-path"
714+
__saved_path="$PATH"
715+
PATH="$TESTDIR/empty-path"
716+
717+
notrack_nft update "53"
718+
assert_rc "notrack_nft update is a no-op when nft binary is absent" 0 $?
719+
720+
[ ! -f "$NOTRACK_TEST_FILE" ]
721+
assert_rc "notrack_nft did not write snippet when nft is absent" 0 $?
722+
723+
PATH="$__saved_path"
724+
eval "$__saved_nft_def"
725+
726+
# ── mkdir failure path returns non-zero ──
727+
# Place a regular file at the would-be parent dir so mkdir -p must fail.
728+
# Defensive logic should return 1 instead of falling through to a broken
729+
# redirection.
730+
rm -rf "$TESTDIR/usr/share"
731+
mkdir -p "$(dirname "$(dirname "$NOTRACK_TEST_FILE")")"
732+
: > "$(dirname "$NOTRACK_TEST_FILE")"
733+
734+
notrack_nft update "53" 2>/dev/null
735+
assert_rc "notrack_nft update returns 1 when parent dir cannot be created" 1 $?
736+
737+
[ ! -f "$NOTRACK_TEST_FILE" ]
738+
assert_rc "notrack_nft did not write snippet on mkdir failure" 0 $?
739+
740+
rm -f "$(dirname "$NOTRACK_TEST_FILE")"
741+
707742
###############################################################################
708743
# SHELL SCRIPT SYNTAX #
709744
###############################################################################

0 commit comments

Comments
 (0)