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 1o1SND-0003cY-9R for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Jun 2022 12:43:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o1SNA-0003Vg-T2 for pgsql-bugs@arkaria.postgresql.org; Wed, 15 Jun 2022 12:43:48 +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 1o1Rwu-0000ra-D9 for pgsql-bugs@lists.postgresql.org; Wed, 15 Jun 2022 12:16:41 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o1Rwr-0002r4-8G for pgsql-bugs@lists.postgresql.org; Wed, 15 Jun 2022 12:16:39 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 11893320083A for ; Wed, 15 Jun 2022 08:16:34 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute5.internal (MEProxy); Wed, 15 Jun 2022 08:16:35 -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:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm2; t= 1655295394; x=1655381794; bh=XhjSCb16oEt0v1enaFjM/oTxZyqn5yKgLDd ZV98XK78=; b=I11RY78VSEgJTI32P8fOWjmunkTpyILYCnHMtMMvHa9rFoudej0 KZt7KzcvWiq4jXuJsyTVQ41HjDLIHQeJ74Dg5Yqshcv5OH6fwG3V1ukgEVdgRJcb KZ3QG8chHIl5a8q0K5voKPKTe8VDrE9upjKWUadQL+xPkkAgEkvTNUlFRU2yRvjX gzYkZuIo0AAkc7o5FA+gVq0wHr+xd6u5NNnK/kLLC6LegZhfwsxD6WXzyKLP04rE kVajGLWR8u7zJWVvvLpmfTTpuda3mcXb/s7DDhBYQAWBDT9AiWJgT7NvJwgeFJU1 /Otuodj7FxH2x2ZHARabnZNZTT0Ys+snBgQ== 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:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1655295394; x= 1655381794; bh=XhjSCb16oEt0v1enaFjM/oTxZyqn5yKgLDdZV98XK78=; b=r pM3/VzIMitAKHKKhC1SdTC7RAO++K3QLJ3YqP64qDlxuv1pf6gpsEXr4npaogwNT LQboRuK4B0fe49OkQP0ZTviCyHguYDGIGU87X0CXj5Q5vCKoxghq92Ws5e7cfOTa tHMfKrv6MKYmyRTzysxrvQFi5vzQJZ9mkS9yMNBruxPvKcvuOj6XOxfL94pAG6wx czX6GEUlzztiR/jRHz5r3HlWuDH0vspq1YbXnoOvrZYqqTPwxgswzFnpgEoiJy8k U0zhFEFfnAQhKXSEC+FixTvAnO1UxnzFFO4IQ/k4lFIqbb+5XQUbKE+7l755n8De Kp+jtZ/hwsCU5N5q3z7tQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedruddvuddggeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedfufhvrghnthgvucftihgthhhtvghrfdcuoehpghhsqhhlqdgs uhhgshesrhhitghhthgvrhdrihguqeenucggtffrrghtthgvrhhnpeevuddvffeuueduje ehuddttdffueeigffftefftdetfeevudeuuefhveelfeeifeenucffohhmrghinhepphho shhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomhepphhgshhqlhdqsghughhssehrihgthhhtvghrrdhiug X-ME-Proxy: Feedback-ID: i64c147e9:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 480D62D40071; Wed, 15 Jun 2022 08:16:34 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-712-gb9e94258b0-fm-20220610.001-gb9e94258 Mime-Version: 1.0 Message-Id: Date: Wed, 15 Jun 2022 14:16:14 +0200 From: "Svante Richter" To: pgsql-bugs@lists.postgresql.org Subject: COPY TO CSV produces data that is incompatible/unsafe for \COPY FROM CSV Content-Type: multipart/alternative; boundary=880944bc5c2b44158326a735838ce2cf List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --880944bc5c2b44158326a735838ce2cf Content-Type: text/plain 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) --880944bc5c2b44158326a735838ce2cf Content-Type: text/html Content-Transfer-Encoding: quoted-printable
Hello!

The documentation for COPY says "To avoid any misin= terpretation, 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.

Thi= s means that COPY TO CSV produces data that \COPY FROM CSV can= not read, which I'm assuming should be fixed (or at the very least docum= ented as a serious limitation of \COPY FROM CSV). I found this out by no= t 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/2019012821444= 8.GH26761%40momjian.us says "I think the question is how many people= are using CSV/STDIN for insecure data loads?") but I would absolutely e= xpect 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=3D1.

SQL to reproduce:

CR= EATE TABLE testtable (a TEXT);
INSERT INTO testtable VALUE= S ('
\.
');
COPY testtable TO = '/run/postgresql/test.csv' CSV;
COPY testtable FROM '/run/= postgresql/test.csv' CSV; -- This one works
\COPY testtabl= e FROM '/run/postgresql/test.csv' CSV; -- This one does not work


Error message:

ERROR:  unterminated CSV quoted field
CONTEX= T:  COPY testtable, line 1: ""
"

Versions tested:

psql (PostgreSQL)= 14.3 (under arch linux)
psql (PostgreSQL) 13.7 (Ubuntu 13= .7-0ubuntu0.21.10.1)

--880944bc5c2b44158326a735838ce2cf--