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 1oLhT1-0002QL-IH for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Aug 2022 08:53:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oLhSz-0000Bc-Q5 for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Aug 2022 08:53:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLWAO-0008AQ-Ua for pgsql-bugs@lists.postgresql.org; Tue, 09 Aug 2022 20:49:34 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oLWAH-00021q-MT for pgsql-bugs@lists.postgresql.org; Tue, 09 Aug 2022 20:49:31 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 2624C5C0158 for ; Tue, 9 Aug 2022 16:49:24 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute5.internal (MEProxy); Tue, 09 Aug 2022 16:49:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=richter.id; h=cc :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; t=1660078164; x=1660164564; bh=7PVq7CQe57 KQnqlwobOPUomiLC493n55CPWNF4mfrz0=; b=p6SFUMn1pHI2SVN6lDk4uu6olU rbpBfFNyNuDvujs59UDbFHVbUWWtYu0g3t2ODkl0vB2LvYXYYbzNaz3JmqmXVmfO VFXwU8ZXWkbM2goSGJeG1/7UhpAZP4mppb+6XkueiQKdvl5Ul0yvmGIQtYLj7Cni B2A9KBmWHhOjkb1RqaTBuOVdGiKtEuciPjA2+oKvch78tacG5rdTDUwVWuE7vISD OwKssEfKZvZH1Fl/5v0j/TVtTm0YW0ig14y0hJz3t03KRV0s5GYocndM1FQgxqJ3 Fod1rh/cdXjz1a85elA6T8cIMgephXlFE09v9d39ggeEmAkVvupWUqpLldLg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1660078164; x=1660164564; bh=7PVq7CQe57KQnqlwobOPUomiLC49 3n55CPWNF4mfrz0=; b=o6aQe2/ZJHTKdZB3X3ilVaH4AFjooDdnjkBStIyze2pm 6QBj/M9wFLhb31WCnkLq81wWtki+MZeZC50xAtzv/mZRKdnAuP/lOTZWADkc/+sj CmHtFmndeZBZs4ibW8opKSfKvfAix6AR6vmC/AYOFbIvk9LI6DDWLaeM2eEJXF/4 AurCFLmooP1eNwH2cFaLxyOXWujMy8uDs3XS3NMsC2jCpJbAvjENW0+Evc+f5/4I B4wTipkqjF7pkdbFchdnuC0BBsR8ilmgoYwcJalIpYc1msz+QBm5KyxWn/jPy00H e/GFZYfIW1i07ZPTj/sO7e+FFesBsGsGKxVi0JTmYQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdegtddgudehhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvffutgesrg dtreerreertdenucfhrhhomhepfdfuvhgrnhhtvgcutfhitghhthgvrhdfuceophhgshhq lhdqsghughhssehrihgthhhtvghrrdhiugeqnecuggftrfgrthhtvghrnhepkeeghfektd ejudffteeiheefleeggedvteetfefhhedtvdettddtjeeikedvvdeunecuffhomhgrihhn pehpohhsthhgrhgvshhqlhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehpghhsqhhlqdgsuhhgshesrhhitghhthgvrhdrihgu X-ME-Proxy: Feedback-ID: i64c147e9:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 8BD372D40074; Tue, 9 Aug 2022 16:49:23 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-811-gb808317eab-fm-20220801.001-gb808317e Mime-Version: 1.0 Message-Id: In-Reply-To: References: Date: Tue, 09 Aug 2022 22:48:56 +0200 From: "Svante Richter" To: pgsql-bugs@lists.postgresql.org Subject: Re: COPY TO CSV produces data that is incompatible/unsafe for \COPY FROM CSV Content-Type: multipart/alternative; boundary=6d34f7e8cc0f4d3387d61246ae275a52 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --6d34f7e8cc0f4d3387d61246ae275a52 Content-Type: text/plain A gentle bump for this, hopefully that's alright! On Wed, Jun 15, 2022, at 2:16 PM, Svante Richter wrote: > Hello! > > The documentation for COPY says "To avoid any misinterpretation, a `\.` data value appearing as a lone entry on a line is automatically quoted on output, and on input, if quoted, is not interpreted as the end-of-data marker". > > The input part only seems to work when using the COPY FROM CSV command, not \COPY FROM CSV. This is mentioned in a previous message here https://www.postgresql.org/message-id/a89f47e1-f716-4ae3-b882-cab5032a5d66%40manitou-mail.org but not documented. > > This means that COPY TO CSV produces data that \COPY FROM CSV cannot read, which I'm assuming should be fixed (or at the very least documented as a serious limitation of \COPY FROM CSV). I found this out by not being able to load a backup of a table that I had exported via COPY TO CSV. > > As the above message also mentioned this can be a security risk if using \COPY FROM STDIN CSV with untrusted data (https://www.postgresql.org/message-id/20190128214448.GH26761%40momjian.us says "I think the question is how many people are using CSV/STDIN for insecure data loads?") but I would absolutely expect data produced with COPY TO CSV to be safe to pipe to a \COPY FROM CSV, but this bug makes that unsafe unless I also explicitly set ON_ERROR_STOP=1. > > SQL to reproduce: > > CREATE TABLE testtable (a TEXT); > INSERT INTO testtable VALUES (' > \. > '); > COPY testtable TO '/run/postgresql/test.csv' CSV; > COPY testtable FROM '/run/postgresql/test.csv' CSV; -- This one works > \COPY testtable FROM '/run/postgresql/test.csv' CSV; -- This one does not work > > > Error message: > > ERROR: unterminated CSV quoted field > CONTEXT: COPY testtable, line 1: "" > " > > Versions tested: > > psql (PostgreSQL) 14.3 (under arch linux) > psql (PostgreSQL) 13.7 (Ubuntu 13.7-0ubuntu0.21.10.1) > --6d34f7e8cc0f4d3387d61246ae275a52 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
A gentle b= ump for this, hopefully that's alright!

On = Wed, Jun 15, 2022, at 2:16 PM, Svante Richter wrote:
Hello!

The documentation for COPY says "To avoid any misinterpretation, a= \. data=20 value appearing as a lone entry on a line is automatically quoted on=20 output, and on input, if quoted, is not interpreted as the end-of-data=20 marker".

The input part only seems to work = when using the COPY FROM CSV command, not \COPY FROM CSV. This is m= entioned in a previous message here ht= tps://www.postgresql.org/message-id/a89f47e1-f716-4ae3-b882-cab5032a5d66= %40manitou-mail.org but not documented.

This means that COPY TO CSV produces data that \COPY FROM CSV= cannot read, which I'm assuming should be fixed (or at the very least d= ocumented as a serious limitation of \COPY FROM CSV). I found this out b= y not being able to load a backup of a table that I had exported via COP= Y TO CSV.

As the above message also mention= ed this can be a security risk if using \COPY FROM STDIN CSV with u= ntrusted data (https://www.postgresql.org/message-id/20190= 128214448.GH26761%40momjian.us says "I think the question is how man= y people are using CSV/STDIN for insecure data loads?") but I would abso= lutely expect data produced with COPY TO CSV to be safe to pipe to = a \COPY FROM CSV, but this bug makes that unsafe unless I also explicitl= y set ON_ERROR_STOP=3D1.

SQL to reproduce:

CREATE TABLE testtable (a TEXT);
INSERT INTO testtab= le VALUES ('
\.
');
COPY testt= able TO '/run/postgresql/test.csv' CSV;
COPY testtable FRO= M '/run/postgresql/test.csv' CSV; -- This one works
\COPY = testtable FROM '/run/postgresql/test.csv' CSV; -- This one does not work=


Error message:

ERROR:  unterminated CSV quoted field
CONTEXT:  COPY testtable, line 1: ""
"

Versions tested:

psql (Pos= tgreSQL) 14.3 (under arch linux)
psql (PostgreSQL) 13.7 (U= buntu 13.7-0ubuntu0.21.10.1)

<= br>
--6d34f7e8cc0f4d3387d61246ae275a52--