acmesh-official / acme.sh

A pure Unix shell script implementing ACME client protocol
https://acme.sh
GNU General Public License v3.0
38.7k stars 4.91k forks source link

Bug in _egrep_o() when extended regex passed in and sed emulation used #2385

Open dkerr64 opened 5 years ago

dkerr64 commented 5 years ago

_egrep_o() function accepts extended regex and on systems that do not have egrep uses sed to emulate egrep. This is failing on the specific regex I was using in dns_freedns.sh. See https://github.com/Neilpang/acme.sh/issues/2305 for the problem I was working on.

The problem is that egrep requires extended regex, and if you pass in non-extended it may fail if there are special characters. But on systems that do not have egrep, the _egrep_o function uses sed to emulate egrep. This fails with the extended regex, but works with the regular regex.

Below script illustrates the problem... I split your _egrep_o function into two to illustrate the problem on systems that have egrep (ie, removed the test for failure with egrep)

#!/usr/bin/env sh

_egrep_o1() {
  egrep -o "$1" 2>/dev/null
}

_egrep_o2() {
  sed -n 's/.*\('"$1"'\).*/\1/p'
}

txt="<tr><td>example.com</td><tdalign=right><ahref=/subdomain/edit.php?edit_domain_id=1234567>[add]</a></td></tr></table></td></tr>"

echo "Test 1 (grep -o)"
echo "$txt" | grep -o "edit\.php?edit_domain_id=[0-9a-zA-Z]*"
echo "Test 2 (egrep -o with non-extended regex, should fail)"
echo "$txt" | _egrep_o1 "edit\.php?edit_domain_id=[0-9a-zA-Z]*"
echo "Test 3 (sed emulation of egrep -o with non-extended regex, should fail like test 2"
echo "$txt" | _egrep_o2 "edit\.php?edit_domain_id=[0-9a-zA-Z]*"
echo "Test 4 (egrep -o with extended regex, should work"
echo "$txt" | _egrep_o1 "edit\.php\?edit_domain_id=[0-9a-zA-Z]+"
echo "Test 5 (sed emulation of egrep -o with extended regex, should work like test 4"
echo "$txt" | _egrep_o2 "edit\.php\?edit_domain_id=[0-9a-zA-Z]+"
echo "End"
exit
codestation commented 5 years ago

This bug also breaks dns_linode.sh and dns_linode_v4.sh when used on the docker image. Seems like _egrep_o is broken on alpine.

edit: sorry, my bug seems more like #1364