Skip to content

Commit 6f6a253

Browse files
committed
Updated for further security fixes
1 parent c5b29ac commit 6f6a253

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ All notable changes to this project will be documented in this file.
55

66
This changelog was automatically generated using [Caretaker](https://github.com/DevelopersToolbox/caretaker) by [Wolf Software](https://github.com/WolfSoftware)
77

8+
### [v0.1.3](https://github.com/DevelopersToolbox/ini-file-parser/compare/v0.1.2...v0.1.3)
9+
10+
> Released on January, 6th 2022
11+
12+
- Updated for further security fixes [`[head]`](https://github.com/DevelopersToolbox/ini-file-parser/commit/)
13+
814
### [v0.1.2](https://github.com/DevelopersToolbox/ini-file-parser/compare/v0.1.1...v0.1.2)
915

1016
> Released on January, 4th 2022
1117
12-
- Additional security fix [`[head]`](https://github.com/DevelopersToolbox/ini-file-parser/commit/)
18+
- Additional security fix [`[c5b29ac]`](https://github.com/DevelopersToolbox/ini-file-parser/commit/c5b29acc99feda7e1e66b8ae405fb1778510da3e)
1319

1420
### [v0.1.1](https://github.com/DevelopersToolbox/ini-file-parser/compare/v0.1.0...v0.1.1)
1521

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.2
1+
0.1.3

demos/complete-example.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ value2=5678
2020
value1=abcd
2121
value2=efgh
2222
value3=This has spaces
23-
value4=$(ls / > ~/out.txt)
23+
value4='$(ls / >> ~/out.txt)'
24+
value5=$(ls / >> ~/bob.txt)
25+
value6="$(ls / >> ~/bob2.txt)"
26+
value7='$(ls / >> ~/out.txt)
2427

2528
# Test clean up of section headers / key names / ignore comments
2629
[ section3 ]

demos/parse-example.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ declare section1_value1
3131
# value of true. #
3232
# -------------------------------------------------------------------------------- #
3333

34-
#case_sensitive_sections=false
35-
#case_sensitive_keys=false
36-
#show_config_warnings=false
37-
#show_config_errors=false
34+
export case_sensitive_sections=false
35+
#export case_sensitive_keys=false
36+
#export show_config_warnings=false
37+
#export show_config_errors=false
3838

3939
# -------------------------------------------------------------------------------- #
4040
# Use the source #

src/ini-file-parser.sh

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,40 @@ function process_value()
193193
value="${value%%\#*}" # Remove in line right comments
194194
value="${value##*( )}" # Remove leading spaces
195195
value="${value%%*( )}" # Remove trailing spaces
196-
value="${value#\"*}" # Remove leading string quotes
197-
value="${value%\"*}" # Remove trailing string quotes
198-
value="${value##*( )}" # Remove leading spaces
199-
value="${value%%*( )}" # Remove trailing spaces
196+
197+
value=$(escape_string "$value")
200198

201199
echo "${value}"
202200
}
203201

202+
# -------------------------------------------------------------------------------- #
203+
# Escape string #
204+
# -------------------------------------------------------------------------------- #
205+
# Replace ' with SINGLE_QUOTE to avoid issues with eval. #
206+
# -------------------------------------------------------------------------------- #
207+
208+
function escape_string()
209+
{
210+
local clean
211+
212+
clean=${1//\'/SINGLE_QUOTE}
213+
echo "${clean}"
214+
}
215+
216+
# -------------------------------------------------------------------------------- #
217+
# Un-Escape string #
218+
# -------------------------------------------------------------------------------- #
219+
# Convert SINGLE_QUOTE back to ' when returning the value to the caller. #
220+
# -------------------------------------------------------------------------------- #
221+
222+
function unescape_string()
223+
{
224+
local orig
225+
226+
orig=${1//SINGLE_QUOTE/\'}
227+
echo "${orig}"
228+
}
229+
204230
# -------------------------------------------------------------------------------- #
205231
# Parse ini file #
206232
# -------------------------------------------------------------------------------- #
@@ -220,19 +246,19 @@ function process_ini_file()
220246
while read -r line; do
221247
line_number=$((line_number+1))
222248

223-
if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
249+
if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
224250
continue;
225251
fi
226252

227-
if [[ $line =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section'
253+
if [[ $line =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section'
228254
section=$(process_section_name "${BASH_REMATCH[1]}")
229255

230256
if ! in_array sections "${section}"; then
231257
eval "${section}_keys=()" # Use eval to declare the keys array
232258
eval "${section}_values=()" # Use eval to declare the values array
233-
sections+=("$section") # Add the section name to the list
259+
sections+=("${section}") # Add the section name to the list
234260
fi
235-
elif [[ $line =~ ^(.*)"="(.*) ]]; then # Match patter for a key=value pair
261+
elif [[ $line =~ ^(.*)"="(.*) ]]; then # Match patter for a key=value pair
236262
key=$(process_key_name "${BASH_REMATCH[1]}")
237263
value=$(process_value "${BASH_REMATCH[2]}")
238264

@@ -280,7 +306,8 @@ function get_value()
280306

281307
for i in "${!keys[@]}"; do
282308
if [[ "${keys[$i]}" = "${key}" ]]; then
283-
printf '%s' "${values[$i]}"
309+
orig=$(unescape_string "${values[$i]}")
310+
printf '%s' "${orig}"
284311
fi
285312
done
286313
}
@@ -308,7 +335,8 @@ function display_config()
308335
eval "values=( \"\${${section}_values[@]}\" )"
309336

310337
for i in "${!keys[@]}"; do
311-
printf '%s=%s\n' "${keys[$i]}" "${values[$i]}"
338+
orig=$(unescape_string "${values[$i]}")
339+
printf '%s=%s\n' "${keys[$i]}" "${orig}"
312340
done
313341
printf '\n'
314342
done
@@ -336,7 +364,8 @@ function display_config_by_section()
336364
eval "values=( \"\${${section}_values[@]}\" )"
337365

338366
for i in "${!keys[@]}"; do
339-
printf '%s=%s\n' "${keys[$i]}" "${values[$i]}"
367+
orig=$(unescape_string "${values[$i]}")
368+
printf '%s=%s\n' "${keys[$i]}" "${orig}"
340369
done
341370
printf '\n'
342371
}

0 commit comments

Comments
 (0)