From 62123e577ee5d4066d53f8ad7e5ac3d2eb190430 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Wed, 18 Nov 2026 20:24:52 +0100 Subject: Securized the queries in the database with a dedicated type for the Identifiers --- test/common.ml | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'test/common.ml') diff --git a/test/common.ml b/test/common.ml index 0ef5cb3..98871a3 100644 --- a/test/common.ml +++ b/test/common.ml @@ -31,12 +31,10 @@ let content_as_json : let () = Seq.iter (fun field -> - Buffer.add_char buff '\''; - Buffer.add_string buff (Model.Field.name field); - - Buffer.add_string buff "',"; - Buffer.add_string buff (Model.Field.name field); - Buffer.add_string buff ",") + let name = Model.Field.name field in + Buffer.add_string buff + ( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name) + ^ "," ^ Model.Identifier.quote name ^ "," )) fields in @@ -46,21 +44,28 @@ let content_as_json : let length = Buffer.length buff in if length > 1 then Buffer.truncate buff (length - 1); () - | Some _ -> Buffer.add_string buff "'parent',parent" + | Some _ -> + Buffer.add_string buff + ( Croquette_common.Sql.quote_literal "parent" ^ "," + ^ Model.Identifier.quote (Model.Identifier.parent) ) in let q = Buffer.contents buff in let open Caqti.Templater in direct T.(int64 -->! string) - ("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content - ^ " where id=?") + ("SELECT json_object(" ^ q ^ ") from " + ^ Model.Identifier.quote (Model.Content.id content) + ^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?") -let query_values (table : Model.Reference.t Model.Content.t) (feature : string) - = +let query_values (table : Model.Reference.t Model.Content.t) + (feature : Model.Identifier.t) = let open Caqti.Templater in direct T.(int64 -->* string) - ("SELECT json_object('" ^ feature ^ "', target) from \"" - ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?") + ("SELECT json_object(" + ^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature) + ^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from " + ^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature) + ^ " where " ^ Model.Identifier.quote (Model.Identifier.parent) ^ "=?") (** Query all the multireferences tables for the given content and the given id. Collect all the values and return them in the same json format as the same @@ -73,7 +78,7 @@ let collect_references : fun db content id -> let module Db = (val db) in let ( let* ) = Lwt.bind in - let content_id = Model.Content.id content in + let content_id = Model.Identifier.to_string (Model.Content.id content) in (* Only keep the multivaluated fields *) let fields = -- cgit v1.2.3