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 --- lib/model/content.ml | 7 ++++--- lib/model/content.mli | 4 ++-- lib/model/field.ml | 6 +++--- lib/model/field.mli | 4 ++-- lib/model/identifier.ml | 35 +++++++++++++++++++++++++++++++ lib/model/identifier.mli | 49 ++++++++++++++++++++++++++++++++++++++++++++ lib/model/tests/identifier.t | 38 ++++++++++++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 lib/model/identifier.ml create mode 100644 lib/model/identifier.mli create mode 100644 lib/model/tests/identifier.t (limited to 'lib/model') diff --git a/lib/model/content.ml b/lib/model/content.ml index 4fe8e92..7b9d9a4 100644 --- a/lib/model/content.ml +++ b/lib/model/content.ml @@ -1,13 +1,13 @@ open StdLabels type 'a t = { - id : string; + id : Identifier.t; sections : 'a Section.t list; subcontents : 'a t list; parent : 'a t Weak.t; } -let v : string -> 'a Section.t list -> 'a t list -> 'a t = +let v : Identifier.t -> 'a Section.t list -> 'a t list -> 'a t = fun id sections subcontents -> let parent = Weak.create 1 in let content = { id; sections; subcontents; parent } in @@ -32,7 +32,8 @@ let fields : 'a t -> 'a Field.t Seq.t = let find_target : 'a t -> string -> 'a option = fun t name -> let feature_opt = - Seq.find (fun f -> String.equal (Field.name f) name) (fields t) + Seq.find (fun f -> String.equal (Identifier.to_string (Field.name f)) name) + (fields t) in let ( let* ) = Option.bind in diff --git a/lib/model/content.mli b/lib/model/content.mli index e972b83..7f0e2cb 100644 --- a/lib/model/content.mli +++ b/lib/model/content.mli @@ -1,7 +1,7 @@ type 'a t -val v : string -> 'a Section.t list -> 'a t list -> 'a t -val id : _ t -> string +val v : Identifier.t -> 'a Section.t list -> 'a t list -> 'a t +val id : _ t -> Identifier.t val subcontents : 'a t -> 'a t list (** Return the direct subcontent *) diff --git a/lib/model/field.ml b/lib/model/field.ml index 8ea38fd..c7e4667 100644 --- a/lib/model/field.ml +++ b/lib/model/field.ml @@ -5,7 +5,7 @@ type validation = { rule : string list Dsl.t; message : textI18n } type 't field_ex = T : ('a, 't) Typ.t -> 't field_ex type 't t = { - name : string; (** The internal name *) + name : Identifier.t; (** The internal name *) label : textI18n; placeholder : textI18n option; icon : unit option; @@ -16,7 +16,7 @@ type 't t = { (** Display the field when the criteria is valid *) } -let name : 'ref t -> string = fun t -> t.name +let name : 'ref t -> Identifier.t = fun t -> t.name let label : 'ref t -> textI18n = fun t -> t.label let placeholder : 'ref t -> textI18n option = fun t -> t.placeholder let icon : 'ref t -> unit option = fun t -> t.icon @@ -33,7 +33,7 @@ let create : ?required:bool -> ?validation:validation list -> ?criteria:string list Dsl.t -> - string -> + Identifier.t -> 't t = fun type_ label ?placeholder ?icon ?(required = false) ?(validation = []) ?criteria name -> diff --git a/lib/model/field.mli b/lib/model/field.mli index 8e1432a..0bcb209 100644 --- a/lib/model/field.mli +++ b/lib/model/field.mli @@ -15,10 +15,10 @@ val create : ?required:bool -> ?validation:validation list -> ?criteria:string list Dsl.t -> - string -> + Identifier.t -> 'ref t -val name : _ t -> string +val name : _ t -> Identifier.t (** Name (identifier) for the field *) val label : _ t -> textI18n diff --git a/lib/model/identifier.ml b/lib/model/identifier.ml new file mode 100644 index 0000000..f1a981e --- /dev/null +++ b/lib/model/identifier.ml @@ -0,0 +1,35 @@ +type t = string + +exception Invalid of string + +let is_valid : string -> bool = + fun id -> + id <> "" + && String.for_all + (fun c -> + (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || c = '_' || c = '-') + id + +let of_string_opt : string -> t option = + fun id -> if is_valid id then Some id else None + +let of_string : string -> t = + fun id -> + match of_string_opt id with Some id -> id | None -> raise (Invalid id) + +let to_string : t -> string = fun id -> id + +(** The fixed columns of the generated schema, predefined so that the SQL + generation can refer to them directly. *) +let id : t = of_string "id" +let parent : t = of_string "parent" +let target : t = of_string "target" +let quote : t -> string = fun id -> "\"" ^ id ^ "\"" +let dot : t -> t -> t = fun a b -> a ^ "." ^ b +let equal : t -> t -> bool = String.equal +let compare : t -> t -> int = String.compare +let hash : t -> int = Hashtbl.hash +let pp : Format.formatter -> t -> unit = Format.pp_print_string diff --git a/lib/model/identifier.mli b/lib/model/identifier.mli new file mode 100644 index 0000000..eed5d83 --- /dev/null +++ b/lib/model/identifier.mli @@ -0,0 +1,49 @@ +type t +(** An SQL identifier (table or column name). + + The type is abstract and implemented as a string: the only way to create a + value is {!of_string}, which validates the string. A [t] is therefore always + safe to embed in a quoted SQL identifier, and the query layer never + manipulates raw strings for table or column names. *) + +exception Invalid of string +(** Raised by {!of_string} when the string is not a valid identifier. *) + +val is_valid : string -> bool +(** [is_valid s] is [true] iff [s] is a non-empty sequence of letters, digits, + underscores or hyphens. In particular the double quote (which could break + out of a quoted identifier) and the dot (used internally to build the names + of the dedicated tables for multivalued fields) are rejected. *) + +val of_string : string -> t +(** The only way to build an identifier. @raise Invalid if the string is not + valid. *) + +val of_string_opt : string -> t option +(** [of_string_opt s] is [Some id] if [s] is valid, [None] otherwise. *) + +val to_string : t -> string +(** The underlying string. *) + +val id : t +(** The [id] column: primary key of every content table. *) + +val parent : t +(** The [parent] column: foreign key to the parent content, present in + subcontent tables and in the dedicated tables of multivalued fields. *) + +val target : t +(** The [target] column of the dedicated tables for multivalued fields. *) + +val quote : t -> string +(** [quote id] is the SQL quoted form ["..."] of the identifier. *) + +val dot : t -> t -> t +(** [dot a b] is the identifier ["a.b"], used for the dedicated tables of + multivalued fields. Both parts are already validated and contain no dot, so + the result is safe to quote as a single identifier. *) + +val equal : t -> t -> bool +val compare : t -> t -> int +val hash : t -> int +val pp : Format.formatter -> t -> unit diff --git a/lib/model/tests/identifier.t b/lib/model/tests/identifier.t new file mode 100644 index 0000000..39460ff --- /dev/null +++ b/lib/model/tests/identifier.t @@ -0,0 +1,38 @@ +Test the [Model.Identifier] module: validation of the names, their quoted SQL +form, the predefined columns and the composite names used for the dedicated +tables of multivalued fields. + +Valid identifiers are printed in their quoted SQL form: + + $ tests --identifier check entity user-details user_type_list + "entity" + "user-details" + "user_type_list" + +Invalid identifiers are rejected: the empty string, and any character outside +letters, digits, underscore and hyphen — in particular the double quote (which +could break out of a quoted identifier) and the dot (reserved for the composite +table names): + + $ tests --identifier check 'evil"; DROP TABLE entity;--' foo.bar '' + invalid: "evil"; DROP TABLE entity;--" + invalid: "foo.bar" + invalid: "" + +The fixed columns of the generated schema are predefined: + + $ tests --identifier predefined + id = "id" + parent = "parent" + target = "target" + +A composite name is built from two valid identifiers: + + $ tests --identifier dot entity references + "entity.references" + +An invalid part is rejected when building a composite name: + + $ tests --identifier dot 'bad"name' ref + Fatal error: exception Model.Identifier.Invalid("bad\"name") + [2] -- cgit v1.2.3