mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Fix copy-paste error in datum_to_jsonb_internal()
Commit 3c152a27b0 mistakenly repeated JSONTYPE_JSON in a condition,
omitting JSONTYPE_CAST. As a result, datum_to_jsonb_internal() failed
to reject inputs that were casts (e.g., from an enum to json as in the
example below) when used as keys in JSON constructors.
This led to a crash in cases like:
  SELECT JSON_OBJECT('happy'::mood: '123'::jsonb);
where 'happy'::mood is implicitly cast to json. The missing check
meant such casted values weren’t properly rejected as invalid
(non-scalar) JSON keys.
Reported-by: Maciek Sakrejda <maciek@pganalyze.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Maciek Sakrejda <maciek@pganalyze.com>
Discussion: https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry+Xrq-ghjcejBRhcRMzWZvbd__QdgJA@mail.gmail.com
Backpatch-through: 17
			
			
This commit is contained in:
		@@ -657,7 +657,7 @@ datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result,
 | 
				
			|||||||
			  tcategory == JSONTYPE_COMPOSITE ||
 | 
								  tcategory == JSONTYPE_COMPOSITE ||
 | 
				
			||||||
			  tcategory == JSONTYPE_JSON ||
 | 
								  tcategory == JSONTYPE_JSON ||
 | 
				
			||||||
			  tcategory == JSONTYPE_JSONB ||
 | 
								  tcategory == JSONTYPE_JSONB ||
 | 
				
			||||||
			  tcategory == JSONTYPE_JSON))
 | 
								  tcategory == JSONTYPE_CAST))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		ereport(ERROR,
 | 
							ereport(ERROR,
 | 
				
			||||||
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 | 
									(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -573,6 +573,18 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WIT
 | 
				
			|||||||
 {"1": 1, "3": 1, "5": "a"}
 | 
					 {"1": 1, "3": 1, "5": "a"}
 | 
				
			||||||
(1 row)
 | 
					(1 row)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- BUG: https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry%2BXrq-ghjcejBRhcRMzWZvbd__QdgJA%40mail.gmail.com
 | 
				
			||||||
 | 
					-- datum_to_jsonb_internal() didn't catch keys that are casts instead of a simple scalar
 | 
				
			||||||
 | 
					CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
 | 
				
			||||||
 | 
					CREATE FUNCTION mood_to_json(mood) RETURNS json AS $$
 | 
				
			||||||
 | 
					  SELECT to_json($1::text);
 | 
				
			||||||
 | 
					$$ LANGUAGE sql IMMUTABLE;
 | 
				
			||||||
 | 
					CREATE CAST (mood AS json) WITH FUNCTION mood_to_json(mood) AS IMPLICIT;
 | 
				
			||||||
 | 
					SELECT JSON_OBJECT('happy'::mood: '123'::jsonb);
 | 
				
			||||||
 | 
					ERROR:  key value must be scalar, not array, composite, or json
 | 
				
			||||||
 | 
					DROP CAST (mood AS json);
 | 
				
			||||||
 | 
					DROP FUNCTION mood_to_json;
 | 
				
			||||||
 | 
					DROP TYPE mood;
 | 
				
			||||||
-- JSON_ARRAY()
 | 
					-- JSON_ARRAY()
 | 
				
			||||||
SELECT JSON_ARRAY();
 | 
					SELECT JSON_ARRAY();
 | 
				
			||||||
 json_array 
 | 
					 json_array 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -152,6 +152,17 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITH UNIQUE RETURNING
 | 
				
			|||||||
SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITHOUT UNIQUE RETURNING jsonb);
 | 
					SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITHOUT UNIQUE RETURNING jsonb);
 | 
				
			||||||
SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WITH UNIQUE RETURNING jsonb);
 | 
					SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WITH UNIQUE RETURNING jsonb);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- BUG: https://postgr.es/m/CADXhmgTJtJZK9A3Na_ry%2BXrq-ghjcejBRhcRMzWZvbd__QdgJA%40mail.gmail.com
 | 
				
			||||||
 | 
					-- datum_to_jsonb_internal() didn't catch keys that are casts instead of a simple scalar
 | 
				
			||||||
 | 
					CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
 | 
				
			||||||
 | 
					CREATE FUNCTION mood_to_json(mood) RETURNS json AS $$
 | 
				
			||||||
 | 
					  SELECT to_json($1::text);
 | 
				
			||||||
 | 
					$$ LANGUAGE sql IMMUTABLE;
 | 
				
			||||||
 | 
					CREATE CAST (mood AS json) WITH FUNCTION mood_to_json(mood) AS IMPLICIT;
 | 
				
			||||||
 | 
					SELECT JSON_OBJECT('happy'::mood: '123'::jsonb);
 | 
				
			||||||
 | 
					DROP CAST (mood AS json);
 | 
				
			||||||
 | 
					DROP FUNCTION mood_to_json;
 | 
				
			||||||
 | 
					DROP TYPE mood;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- JSON_ARRAY()
 | 
					-- JSON_ARRAY()
 | 
				
			||||||
SELECT JSON_ARRAY();
 | 
					SELECT JSON_ARRAY();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user