Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kx5Wq-0006j8-5K for pgsql-general@arkaria.postgresql.org; Wed, 06 Jan 2021 09:54:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kx5Wp-000897-3e for pgsql-general@arkaria.postgresql.org; Wed, 06 Jan 2021 09:54:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kx5Wo-00088z-Fi for pgsql-general@lists.postgresql.org; Wed, 06 Jan 2021 09:54:54 +0000 Received: from out2-50.antispamcloud.com ([185.201.17.50]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kx5Wl-00020A-DH for pgsql-general@postgresql.org; Wed, 06 Jan 2021 09:54:53 +0000 Received: from hp309.hostpapa.com ([69.90.160.170]) by mx136.antispamcloud.com with esmtpsa (TLSv1.2:AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from ) id 1kx5We-0000Mr-9N; Wed, 06 Jan 2021 10:54:47 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=pendari.org ; s=default; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References :In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=A/HUK3SZoEMymNHuD8n5+tDUJ1uTSu8zOsQjvhGTOfg=; b=io+FRXbWG9dB0P45GgOpB4cl9F Ev+PjyWCu8iRaYoKF7ygfcZqWGJLBPLgo5rvexXq0+34IKtev8SQkrSTwLLOHxLUN7GkXrkiPJSOC k3RsHyHBUki6DVnVNZv1HtOJfJbIZC9kXzch/WakSESvwh6AC3zQeS99cyp+ePXtZSeJIjzAvecNJ UpGjvgfG0yhnXnCMNGXB5f2Gyb+mBHsCq/KOWnkQIuYGQZgmg3HU3k1Auwq9wayv6ePmU/g7kfW6Y 7KL5xpYuRCrJraozsU8FnZmJhGQ7L/Tp47fFHxIXuFfCgjaGO8GXCYzO5kc4gRD/kYVQSTKrI4R6N 7z3IEeJw==; Received: from [1.129.212.206] (port=50726 helo=[192.168.20.53]) by hp309.hostpapa.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kx5Vw-009kcf-44; Wed, 06 Jan 2021 20:54:01 +1100 From: "Gavan Schneider" To: "Pavel Stehule" Cc: Durumdara , "Postgres General" Subject: Re: How to convert escaped text column - force E prefix Date: Wed, 06 Jan 2021 20:53:53 +1100 X-Mailer: MailMate (1.14r5757) Message-ID: In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_MailMate_8E9B736B-2B75-4E18-8A62-657AEEA980EF_=" Content-Transfer-Encoding: 8bit X-OutGoing-Spam-Status: No, score=-1.0 X-Get-Message-Sender-Via: hp309.hostpapa.com: authenticated_id: list.pg.gavan@pendari.org X-Authenticated-Sender: hp309.hostpapa.com: list.pg.gavan@pendari.org X-AuthUser: list.pg.gavan@pendari.org X-Originating-IP: 69.90.160.170 X-Spampanel-Domain: outgoing-shared.hostpapa.com X-Spampanel-Username: outgoingshared Authentication-Results: antispamcloud.com; auth=pass (login) smtp.auth=outgoingshared@outgoing-shared.hostpapa.com X-Spampanel-Outgoing-Class: ham X-Spampanel-Outgoing-Evidence: Combined (0.09) X-Recommended-Action: accept X-Filter-ID: Pt3MvcO5N4iKaDQ5O6lkdGlMVN6RH8bjRMzItlySaT+HFbORVhR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+gAwStMlZq+GdFjag2t+nf4qk/UVXVfWvSylKWMQkY6n2XZ+wwW48= X-Report-Abuse-To: spam@quarantine10.antispamcloud.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_MailMate_8E9B736B-2B75-4E18-8A62-657AEEA980EF_= Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit On 6 Jan 2021, at 19:43, Pavel Stehule wrote: > Currently there are not any functions that you need. You need to write > your > own. > CREATE OR REPLACE FUNCTION public.unistr(text) RETURNS text LANGUAGE plpgsql IMMUTABLE STRICT AS $function$ declare r text; begin execute 'select e''' || quote_literal($1) || '''' into r; return r; end; $function$; > > Attention: This is ugly and possible sql injection vulnerable!!! But > there > is not another way. The fix is in queue https://www.postgresql.org/docs/current/functions-string.html quote_literal ( text ) → text Returns the given string suitably quoted to be used as a string literal in an SQL statement string. Embedded single-quotes and backslashes are properly doubled. Note that quote_literal returns null on null input; if the argument might be null, quote_nullable is often more suitable. See also Example 42.1. quote_literal(E'O\'Reilly') → 'O''Reilly' It is even more ugly but would it at least help with the SQL injection risk? Gavan Schneider —— Gavan Schneider, Sodwalls, NSW, Australia Explanations exist; they have existed for all time; there is always a well-known solution to every human problem — neat, plausible, and wrong. — H. L. Mencken, 1920 --=_MailMate_8E9B736B-2B75-4E18-8A62-657AEEA980EF_= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On 6 Jan 2021, at 19:43, Pavel Stehule wrote:

Currently there are not any functions that you need. You need to wri= te your
own.

CREATE OR REPLACE=
 FUNCTION public.unistr(text)
  RETURNS text
  LANGUAGE plpgsql
  IMMUTABLE STRICT
 AS $function$
 declare r text;
 begin
   execute 'select e''' || quote_literal($1) || '''' into r;
   return r;
 end;
 $function$;

Attention: This is ugly and possible sql injection vulnerable!!! But= there
is not another way. The fix is in queue

https://www.postg=
resql.org/docs/current/functions-string.html
quote_literal ( text ) =E2=86=92 text
  Returns the given string suitably quoted to be used as a string literal=
 in an SQL statement string.
  Embedded single-quotes and backslashes are properly doubled.
  Note that quote_literal returns null on null input; if the argument mig=
ht be null, quote_nullable is often more suitable.
  See also Example 42.1. quote_literal(E'O\'Reilly') =E2=86=92 'O''Reilly=
'

It is even more ugly but would it at least help with the = SQL injection risk?

Gavan Schneider
=E2=80=94=E2=80=94
Gavan Schneider, Sodwalls, NSW, Australia
Explanations exist; they have existed for all time; there is always a wel= l-known solution to every human problem =E2=80=94 neat, plausible, and wr= ong.
=E2=80=94 H. L. Mencken, 1920

--=_MailMate_8E9B736B-2B75-4E18-8A62-657AEEA980EF_=--