Skip to content

Commit

Permalink
fix: obfus - ignore var + tests (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
fentas authored Apr 5, 2024
1 parent 469e7a4 commit c344a0a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 18 deletions.
18 changes: 14 additions & 4 deletions .bin/obfus
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ sub print_usage() {
say "\t\t\tThe default is 'a', which means all variables will be changed to a0,a1,a2,a3,...";
say "\t\t-C\tis an option to clean out full line comments and blank lines.";
say "\t\t-F\tis an option to flatten out the code (remove indentations)";
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)";
say "\t\t-A\tis an option to aggressive obfuscation, one line (implies using -F and -C)";
say "\t\t-N\tis an option to not obfuscate variable names";
say "\t\t-I\tis an option to ignore specific variables in the obfuscation process, separated by commas (default: usage,args)";
exit 0;
}
Expand Down Expand Up @@ -64,12 +65,17 @@ sub parse_cmd_args {
say "Input or output file not specified!!";
&print_usage();
}
return ($input_file,$output_file,$new_variable_prefix,$delete_blanks,$flatten,$aggressive,$ignore_vars);
return ($input_file, $output_file, $new_variable_prefix, $delete_blanks, $flatten, $aggressive, $ignore_vars);
}

sub parse_vars_from_file {
my $file_name=shift;
my $ignore_vars=shift;

if ($ignore_vars eq "*") {
return ();
}

open(my $file_handle, "<", $file_name) || die "Couldn't open '".$file_name."' for reading because: ".$!;
my %vars=();
my $skip_next_line=0;
Expand Down Expand Up @@ -132,8 +138,12 @@ sub parse_vars_from_file {
}

# go through $ignore_vars split by ,
for my $name (split /,/, $ignore_vars) {
delete $vars{$name};
for my $re (split /,/, $ignore_vars) {
for my $var (keys %vars) {
if ($var =~ m/$re/) {
delete $vars{$var};
}
}
}

close $file_handle;
Expand Down
7 changes: 4 additions & 3 deletions .docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ argsh::minify() {
} >>"${content}"
done
done
iVars=""
local -a iVars=()
if (( ${#ignore_variable[@]} )); then
iVars="-I $(array::join "," "${ignore_variable[@]}")"
# shellcheck disable=
iVars=(-I "$(array::join "," "${ignore_variable[@]}")")
fi
# shellcheck disable=SC2086
obfus -i "${content}" -o "${tout}" -A ${iVars}
obfus -i "${content}" -o "${tout}" -A "${iVars[@]}"
local -r data="$(cat "${tout}")"
if [[ -z "${template:-}" ]]; then
echo -n "${data}" >"${out}"
Expand Down
22 changes: 22 additions & 0 deletions .docker/test/minify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,26 @@ load_source
grep -q 'local usage' "${stdout}"
grep -q 'local args' "${stdout}"
grep -vq obfuscate "${stdout}"
}

@test "ignore all variables" {
(
docker-entrypoint.sh minify -i '*' "${PATH_FIXTURES}/ignore_vars.sh"
) >"${stdout}" 2>"${stderr}" || status="${?}"

is_empty stderr
grep -q 'local usage' "${stdout}"
grep -q 'local args' "${stdout}"
grep -q obfuscate "${stdout}"
}

@test "ignore regex variables" {
(
docker-entrypoint.sh minify -i '^u' "${PATH_FIXTURES}/ignore_vars.sh"
) >"${stdout}" 2>"${stderr}" || status="${?}"

is_empty stderr
grep -q 'local usage' "${stdout}"
grep -vq 'local args' "${stdout}"
grep -vq obfuscate "${stdout}"
}
14 changes: 7 additions & 7 deletions argsh.min.sh

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions coverage/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files": [
{"file": "/workspace/libraries/fixtures/import/print_out.sh", "percent_covered": "100.00", "covered_lines": "2", "total_lines": "2"},
{"file": "/workspace/libraries/fixtures/args/rest.sh", "percent_covered": "71.43", "covered_lines": "5", "total_lines": "7"},
{"file": "/workspace/libraries/fixtures/args/usage.sh", "percent_covered": "67.57", "covered_lines": "25", "total_lines": "37"},
{"file": "/workspace/libraries/fixtures/args/attrs.sh", "percent_covered": "52.50", "covered_lines": "21", "total_lines": "40"},
{"file": "/workspace/libraries/array.sh", "percent_covered": "35.71", "covered_lines": "5", "total_lines": "14"},
Expand All @@ -13,11 +14,11 @@
{"file": "/workspace/libraries/fixtures/args/fmt.sh", "percent_covered": "33.33", "covered_lines": "13", "total_lines": "39"},
{"file": "/workspace/libraries/args.sh", "percent_covered": "90.03", "covered_lines": "280", "total_lines": "311"}
],
"percent_covered": "77.05",
"covered_lines": 433,
"total_lines": 562,
"percent_covered": "76.98",
"covered_lines": 438,
"total_lines": 569,
"percent_low": 25,
"percent_high": 75,
"command": "bats",
"date": "2024-04-05 10:46:08"
"date": "2024-04-05 12:15:20"
}
30 changes: 30 additions & 0 deletions libraries/args.bats
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ source "${PATH_FIXTURES}/usage.sh"
snapshot stdout
}

# -----------------------------------------------------------------------------
# Now test rest of positional arguments
source "${PATH_FIXTURES}/rest.sh"

@test "rest: -h, --help" {
for arg in -h --help; do
(
:test::rest "${arg}"
) >"${stdout}" 2>"${stderr}" || status=$?

assert "${status}" -eq 0
is_empty stderr
snapshot stdout
done
}

@test "rest: positional parameters" {
:validate() {
assert "${#all[@]}" -eq 5
assert "${all[*]}" = "pos1 pos2 pos3 pos4 pos5"
}
(
:test::rest "pos1" "pos2" "pos3" "pos4" "pos5"
) >"${stdout}" 2>"${stderr}" || status=$?

assert "${status}" -eq 0
is_empty stderr
snapshot stdout
}

# -----------------------------------------------------------------------------
# Now test format stuff
source "${PATH_FIXTURES}/fmt.sh"
Expand Down
12 changes: 12 additions & 0 deletions libraries/fixtures/args/rest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034
set -euo pipefail

:test::rest() {
local -a all args=(
'all' 'All arguments'
)
:args "Test rest parameters" "${@}"
:validate >&3
echo "${all[@]:-}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Test rest parameters

Usage:
argsh ...all

Arguments:
all string All arguments

Options:
-h, --help
Show this help message

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pos1 pos2 pos3 pos4 pos5
17 changes: 17 additions & 0 deletions test/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,21 @@ filter_control_sequences() {
log_on_failure() {
echo Failed with status "${status}" and output:
echo "${output}"
}

declare -p grep 2>/dev/null || {
grep="$(command -v grep)"
readonly grep
}
grep() {
$grep "${@}" || {
local status="${?}"
echo "■■ grep failed with status ${status}"
if [[ -f "${*: -1}" ]]; then
echo "■■ >>>"
cat "${*: -1}"
echo "<<<"
fi
return "${status}"
}
}

0 comments on commit c344a0a

Please sign in to comment.