Skip to content

Commit cbaddb8

Browse files
committed
fix: obfus - ignore var + tests
1 parent c75d182 commit cbaddb8

10 files changed

+124
-18
lines changed

Diff for: .bin/obfus

+14-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ sub print_usage() {
2222
say "\t\t\tThe default is 'a', which means all variables will be changed to a0,a1,a2,a3,...";
2323
say "\t\t-C\tis an option to clean out full line comments and blank lines.";
2424
say "\t\t-F\tis an option to flatten out the code (remove indentations)";
25-
say "\t\t-A\tis an option to aggressive obfuscation (implies using -F and -C)(tries to put more content on same line when possible)";
25+
say "\t\t-A\tis an option to aggressive obfuscation, one line (implies using -F and -C)";
26+
say "\t\t-N\tis an option to not obfuscate variable names";
2627
say "\t\t-I\tis an option to ignore specific variables in the obfuscation process, separated by commas (default: usage,args)";
2728
exit 0;
2829
}
@@ -64,12 +65,17 @@ sub parse_cmd_args {
6465
say "Input or output file not specified!!";
6566
&print_usage();
6667
}
67-
return ($input_file,$output_file,$new_variable_prefix,$delete_blanks,$flatten,$aggressive,$ignore_vars);
68+
return ($input_file, $output_file, $new_variable_prefix, $delete_blanks, $flatten, $aggressive, $ignore_vars);
6869
}
6970

7071
sub parse_vars_from_file {
7172
my $file_name=shift;
7273
my $ignore_vars=shift;
74+
75+
if ($ignore_vars eq "*") {
76+
return ();
77+
}
78+
7379
open(my $file_handle, "<", $file_name) || die "Couldn't open '".$file_name."' for reading because: ".$!;
7480
my %vars=();
7581
my $skip_next_line=0;
@@ -132,8 +138,12 @@ sub parse_vars_from_file {
132138
}
133139

134140
# go through $ignore_vars split by ,
135-
for my $name (split /,/, $ignore_vars) {
136-
delete $vars{$name};
141+
for my $re (split /,/, $ignore_vars) {
142+
for my $var (keys %vars) {
143+
if ($var =~ m/$re/) {
144+
delete $vars{$var};
145+
}
146+
}
137147
}
138148

139149
close $file_handle;

Diff for: .docker/docker-entrypoint.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ argsh::minify() {
4343
} >>"${content}"
4444
done
4545
done
46-
iVars=""
46+
local -a iVars=()
4747
if (( ${#ignore_variable[@]} )); then
48-
iVars="-I $(array::join "," "${ignore_variable[@]}")"
48+
# shellcheck disable=
49+
iVars=(-I "$(array::join "," "${ignore_variable[@]}")")
4950
fi
5051
# shellcheck disable=SC2086
51-
obfus -i "${content}" -o "${tout}" -A ${iVars}
52+
obfus -i "${content}" -o "${tout}" -A "${iVars[@]}"
5253
local -r data="$(cat "${tout}")"
5354
if [[ -z "${template:-}" ]]; then
5455
echo -n "${data}" >"${out}"

Diff for: .docker/test/minify.bats

+22
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,26 @@ load_source
1616
grep -q 'local usage' "${stdout}"
1717
grep -q 'local args' "${stdout}"
1818
grep -vq obfuscate "${stdout}"
19+
}
20+
21+
@test "ignore all variables" {
22+
(
23+
docker-entrypoint.sh minify -i '*' "${PATH_FIXTURES}/ignore_vars.sh"
24+
) >"${stdout}" 2>"${stderr}" || status="${?}"
25+
26+
is_empty stderr
27+
grep -q 'local usage' "${stdout}"
28+
grep -q 'local args' "${stdout}"
29+
grep -q obfuscate "${stdout}"
30+
}
31+
32+
@test "ignore regex variables" {
33+
(
34+
docker-entrypoint.sh minify -i '^u' "${PATH_FIXTURES}/ignore_vars.sh"
35+
) >"${stdout}" 2>"${stderr}" || status="${?}"
36+
37+
is_empty stderr
38+
grep -q 'local usage' "${stdout}"
39+
grep -vq 'local args' "${stdout}"
40+
grep -vq obfuscate "${stdout}"
1941
}

Diff for: argsh.min.sh

+7-7
Large diffs are not rendered by default.

Diff for: coverage/coverage.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"files": [
33
{"file": "/workspace/libraries/fixtures/import/print_out.sh", "percent_covered": "100.00", "covered_lines": "2", "total_lines": "2"},
4+
{"file": "/workspace/libraries/fixtures/args/rest.sh", "percent_covered": "71.43", "covered_lines": "5", "total_lines": "7"},
45
{"file": "/workspace/libraries/fixtures/args/usage.sh", "percent_covered": "67.57", "covered_lines": "25", "total_lines": "37"},
56
{"file": "/workspace/libraries/fixtures/args/attrs.sh", "percent_covered": "52.50", "covered_lines": "21", "total_lines": "40"},
67
{"file": "/workspace/libraries/array.sh", "percent_covered": "35.71", "covered_lines": "5", "total_lines": "14"},
@@ -13,11 +14,11 @@
1314
{"file": "/workspace/libraries/fixtures/args/fmt.sh", "percent_covered": "33.33", "covered_lines": "13", "total_lines": "39"},
1415
{"file": "/workspace/libraries/args.sh", "percent_covered": "90.03", "covered_lines": "280", "total_lines": "311"}
1516
],
16-
"percent_covered": "77.05",
17-
"covered_lines": 433,
18-
"total_lines": 562,
17+
"percent_covered": "76.98",
18+
"covered_lines": 438,
19+
"total_lines": 569,
1920
"percent_low": 25,
2021
"percent_high": 75,
2122
"command": "bats",
22-
"date": "2024-04-05 10:46:08"
23+
"date": "2024-04-05 12:15:20"
2324
}

Diff for: libraries/args.bats

+30
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,36 @@ source "${PATH_FIXTURES}/usage.sh"
248248
snapshot stdout
249249
}
250250

251+
# -----------------------------------------------------------------------------
252+
# Now test rest of positional arguments
253+
source "${PATH_FIXTURES}/rest.sh"
254+
255+
@test "rest: -h, --help" {
256+
for arg in -h --help; do
257+
(
258+
:test::rest "${arg}"
259+
) >"${stdout}" 2>"${stderr}" || status=$?
260+
261+
assert "${status}" -eq 0
262+
is_empty stderr
263+
snapshot stdout
264+
done
265+
}
266+
267+
@test "rest: positional parameters" {
268+
:validate() {
269+
assert "${#all[@]}" -eq 5
270+
assert "${all[*]}" = "pos1 pos2 pos3 pos4 pos5"
271+
}
272+
(
273+
:test::rest "pos1" "pos2" "pos3" "pos4" "pos5"
274+
) >"${stdout}" 2>"${stderr}" || status=$?
275+
276+
assert "${status}" -eq 0
277+
is_empty stderr
278+
snapshot stdout
279+
}
280+
251281
# -----------------------------------------------------------------------------
252282
# Now test format stuff
253283
source "${PATH_FIXTURES}/fmt.sh"

Diff for: libraries/fixtures/args/rest.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2034
3+
set -euo pipefail
4+
5+
:test::rest() {
6+
local -a all args=(
7+
'all' 'All arguments'
8+
)
9+
:args "Test rest parameters" "${@}"
10+
:validate >&3
11+
echo "${all[@]:-}"
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Test rest parameters
2+
3+
Usage:
4+
argsh ...all
5+
6+
Arguments:
7+
all string All arguments
8+
9+
Options:
10+
-h, --help
11+
Show this help message
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pos1 pos2 pos3 pos4 pos5

Diff for: test/helper.bash

+17
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,21 @@ filter_control_sequences() {
109109
log_on_failure() {
110110
echo Failed with status "${status}" and output:
111111
echo "${output}"
112+
}
113+
114+
declare -p grep 2>/dev/null || {
115+
grep="$(command -v grep)"
116+
readonly grep
117+
}
118+
grep() {
119+
$grep "${@}" || {
120+
local status="${?}"
121+
echo "■■ grep failed with status ${status}"
122+
if [[ -f "${*: -1}" ]]; then
123+
echo "■■ >>>"
124+
cat "${*: -1}"
125+
echo "<<<"
126+
fi
127+
return "${status}"
128+
}
112129
}

0 commit comments

Comments
 (0)