@@ -29,6 +29,18 @@ normalize_key() {
2929 esac
3030}
3131
32+ # Quote value if it contains spaces, special characters, or is empty
33+ quote_value () {
34+ case " $1 " in
35+ * [[:space:]]* |* [\"\'\`\$\\ ]* |' ' )
36+ printf ' "%s"' " $1 "
37+ ;;
38+ * )
39+ printf ' %s' " $1 "
40+ ;;
41+ esac
42+ }
43+
3244giv_config () {
3345 cmd=" $1 "
3446 case " $cmd " in
@@ -72,8 +84,13 @@ giv_config() {
7284 ;;
7385 --get|get)
7486 key=" $2 "
75- # ENV override
87+ # Validate key format
7688 env_key=" $( normalize_key " $key " ) "
89+ if [ -z " $env_key " ]; then
90+ printf ' %s\n' " Invalid key format: $key " >&2
91+ return 1
92+ fi
93+ # ENV override
7794 if [ -n " $env_key " ]; then
7895 eval " env_val=\"\$ {$env_key -}\" "
7996 if [ -n " $env_val " ]; then
@@ -92,8 +109,8 @@ giv_config() {
92109 givkey=" $( normalize_key " $key " ) "
93110 val=$( grep -E " ^$givkey =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
94111 fi
95- # Remove surrounding quotes if present
96- val=$( printf ' %s' " $val " | sed ' s/^"\(.*\)"$/\1/' )
112+ # Remove surrounding quotes if present (both single and double)
113+ val=$( printf ' %s' " $val " | sed -e ' s/^"\(.*\)"$/\1/' -e " s/^'\(.*\)'$/\1/ " )
97114 printf ' %s\n' " $val "
98115 ;;
99116 --unset|unset)
@@ -119,7 +136,7 @@ giv_config() {
119136 grep -v -E " ^($key |$givkey )=" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
120137 # Always write in GIV_... format
121138 writekey=" $( normalize_key " $key " ) "
122- printf ' %s\n' " $writekey = $ value" >> " $tmpfile "
139+ printf ' %s=%s \n' " $writekey " " $( quote_value " $ value" ) " >> " $tmpfile "
123140 mv " $tmpfile " " $GIV_CONFIG_FILE "
124141 ;;
125142 -* |help)
@@ -130,9 +147,14 @@ giv_config() {
130147 * )
131148 key=" $1 "
132149 value=" ${2:- } "
150+ # Validate key format
151+ env_key=" $( normalize_key " $key " ) "
152+ if [ -z " $env_key " ]; then
153+ printf ' %s\n' " Invalid key format: $key " >&2
154+ return 1
155+ fi
133156 if [ -z " $value " ]; then
134157 # ENV override
135- env_key=" $( normalize_key " $key " ) "
136158 if [ -n " $env_key " ]; then
137159 eval " env_val=\"\$ {$env_key -}\" "
138160 if [ -n " $env_val " ]; then
@@ -156,8 +178,8 @@ giv_config() {
156178 givkey=" $( normalize_key " $key " ) "
157179 val=$( grep -E " ^$givkey =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
158180 fi
159- # Remove surrounding quotes if present
160- val=$( printf ' %s' " $val " | sed ' s/^"\(.*\)"$/\1/' )
181+ # Remove surrounding quotes if present (both single and double)
182+ val=$( printf ' %s' " $val " | sed -e ' s/^"\(.*\)"$/\1/' -e " s/^'\(.*\)'$/\1/ " )
161183 printf ' %s\n' " $val "
162184 else
163185 mkdir -p " $GIV_HOME "
@@ -169,7 +191,7 @@ giv_config() {
169191 grep -v -E " ^($key |$givkey )=" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
170192 # Always write in GIV_... format
171193 writekey=" $( normalize_key " $key " ) "
172- printf ' %s\n' " $writekey = $ value" >> " $tmpfile "
194+ printf ' %s=%s \n' " $writekey " " $( quote_value " $ value" ) " >> " $tmpfile "
173195 mv " $tmpfile " " $GIV_CONFIG_FILE "
174196 fi
175197 ;;
0 commit comments