🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.vms
Article #74

Re: PHP "Features"

#74
From: Arne Vajhøj
Date: Sat, 24 Oct 2026 23:49
76 lines
3954 bytes
On 10/24/2026 6:43 PM, Lawrence D’Oliveiro wrote:
>> But there is a reason that the function is called escape
>> and not escape_and_quote - it just escapes - it does not quote.
> 
> You need a function that turns an object in the application language
> into an appropriate literal in the query language, including quotes
> where appropriate but leaving them out if not. For example, whatever
> in your language maps to “NULL” in SQL must never be quoted!
> 
> For example, here’s function I’ve been using in Python code, that
> does strictly standard SQL quoting:
> 
>      def sql_string(s) :
>          "returns an SQL string literal that evaluates to s."
>          if s == None :
>              q = "null"
>          elif isinstance(s, int) :
>              q = "%d" % s
>          elif isinstance(s, (bytes, bytearray)) :
>              q = "X'" + "".join("%02x" % d for d in s) + "'"
>          elif isinstance(s, str) :
>              q = "'" + s.replace("'", "''") + "'"
>          else :
>              raise TypeError("can only convert bytes, str, int or None types")
>          #end if
>          return q
>      #end sql_string
Two good rules:
1) never ever do you own quoting code instead of using prepare
2) never ever believe anything a person writing own quoting code instead
    of using prepare says about security

import pymysql

def sql_string(s) :
     "returns an SQL string literal that evaluates to s."
     if s == None :
         q = "null"
     elif isinstance(s, int) :
         q = "%d" % s
     elif isinstance(s, (bytes, bytearray)) :
         q = "X'" + "".join("%02x" % d for d in s) + "'"
     elif isinstance(s, str) :
         q = "'" + s.replace("'", "''") + "'"
     else :
         raise TypeError("can only convert bytes, str, int or None types")
     #end if
     return q
#end sql_string

def test(b):
     print(b + ' -> SELECT f1,f2 FROM t1 WHERE f2 = ' + sql_string(b))
     con = 
pymysql.connect(host='arnepc5',user='arne',password='hemmeligt',db='test')
     c = con.cursor()
     c.execute('SELECT f1,f2 FROM t1 WHERE f2 = ' + sql_string(b))
     data = c.fetchall()
     c.close()
     for row in data:
         print('%d %s' % (row[0], row[1]))
     con.close()

test("BB")
test("BB\\' or 1=1 #")

gives:

BB -> SELECT f1,f2 FROM t1 WHERE f2 = 'BB'
2 BB
BB\' or 1=1 # -> SELECT f1,f2 FROM t1 WHERE f2 = 'BB\'' or 1=1 #'
1 A
2 BB
3 CCC

Arne
Technical Details
Message-ID: <11bjg4u$35qiv$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11bcbi1$k068$1@dont-email.me> <nk1gv8F1eecU1@mid.individual.net> <11bgih8$f7a$1@reader2.panix.com> <nk2bhaF5j2sU1@mid.individual.net> <11bh3ei$19g$1@reader2.panix.com> <11bi7bg$2mdi4$1@dont-email.me> <11bikkl$2rllh$1@dont-email.me> <11bjcal$340l8$9@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Arne Vajhøj <arne@vajhoej.dk>
Newsgroups: comp.os.vms
Subject: Re: PHP "Features"
Date: Sat, 24 Oct 2026 19:49:00 -0400
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <11bjg4u$35qiv$2@dont-email.me>
References: <11bcbi1$k068$1@dont-email.me> <nk1gv8F1eecU1@mid.individual.net>
 <11bgih8$f7a$1@reader2.panix.com> <nk2bhaF5j2sU1@mid.individual.net>
 <11bh3ei$19g$1@reader2.panix.com> <11bi7bg$2mdi4$1@dont-email.me>
 <11bikkl$2rllh$1@dont-email.me> <11bjcal$340l8$9@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 24 Oct 2026 23:48:46 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3336799"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18RKHCd1ncMy4woqO3K9RHFe12KM/oK4x8="; posting-host="90629bce1c2634862ed525d6a667363b"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UvAl3DOIbbjNYGcPpTB8dVrCpQI= sha256:mtSyVAGfW2yQ9g+2hXYEtFzV2UPNo09emx6yqShsnPk=
	sha1:uUVuSt3bpiHLFTFxqHvr8wADN1o= sha256:8z0TgKsdUO+BSQMPnTYbrPV+AtvS/xKth+qMj4nHbao=
In-Reply-To: <11bjcal$340l8$9@dont-email.me>
Content-Language: en-US