summaryrefslogtreecommitdiff
path: root/lib/configuration
diff options
context:
space:
mode:
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/ast.ml21
-rw-r--r--lib/configuration/ast.mli9
-rw-r--r--lib/configuration/graph_tree.ml14
-rw-r--r--lib/configuration/read_conf.ml48
-rw-r--r--lib/configuration/tests/errors.t98
5 files changed, 148 insertions, 42 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index 36ba349..d874428 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -1,10 +1,6 @@
open StdLabels
-module Splay = Splay.Make (struct
- include String
-
- let pp = Format.pp_print_string
-end)
+module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
@@ -18,19 +14,19 @@ type setting = {
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
(** Get the content from the setting. *)
-let get_content : setting -> string -> Model.Reference.t Model.Content.t option
- =
+let get_content :
+ setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option =
fun settings name -> Splay.find name settings.contents
-exception BrokenLink : ('a Type.Id.t * 'a * string) -> exn
-exception NotImplemented : ('a Type.Id.t * 'a * string) -> exn
+exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
+exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Convert the content having fields referencing element by name into content
having fields referencing other contents *)
let rec convert :
'a Type.Id.t ->
setting ->
- ('a * string) Model.Content.t ->
+ ('a * Model.Identifier.t) Model.Content.t ->
Model.Reference.t Model.Content.t =
fun ty setting content ->
let subcontents =
@@ -56,7 +52,8 @@ let rec convert :
(* Also check from the list catalog *)
begin match
List.find_opt setting.lists ~f:(fun { id; _ } ->
- String.equal id name)
+ String.equal id
+ (Model.Identifier.to_string name))
with
| Some v ->
ignore v;
@@ -77,7 +74,7 @@ let rec convert :
giving the possibility to fetch any content or subcontent by is’s name
without traversing the whole tree each time. *)
let register_content :
- 'a Type.Id.t -> setting -> ('a * string) Model.Content.t -> unit =
+ 'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit =
fun ty setting content ->
let rec traverse content =
let () =
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index 9fba7f6..33d9bc8 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -3,7 +3,8 @@ type list' = { id : string; source : string }
val init : unit -> setting
-val get_content : setting -> string -> Model.Reference.t Model.Content.t option
+val get_content :
+ setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option
(** Get the content from the setting. *)
val root_contents : setting -> Model.Reference.t Model.Content.t list
@@ -12,16 +13,16 @@ val root_contents : setting -> Model.Reference.t Model.Content.t list
Only the root content are repored in the list *)
val register_content :
- 'a Type.Id.t -> setting -> ('a * string) Model.Content.t -> unit
+ 'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by is’s name
without traversing the whole tree each time. *)
-exception BrokenLink : ('a Type.Id.t * 'a * string) -> exn
+exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Exception raised when a reference in the configuration targets a
non-existing content *)
-exception NotImplemented : ('a Type.Id.t * 'a * string) -> exn
+exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
val register_list : setting -> list' -> setting
diff --git a/lib/configuration/graph_tree.ml b/lib/configuration/graph_tree.ml
index 25c9366..240c1c9 100644
--- a/lib/configuration/graph_tree.ml
+++ b/lib/configuration/graph_tree.ml
@@ -9,9 +9,13 @@
module Vertex = struct
type t = Model.Reference.t Model.Content.t
- let compare t1 t2 = String.compare (Model.Content.id t1) (Model.Content.id t2)
- let equal t1 t2 = String.equal (Model.Content.id t1) (Model.Content.id t2)
- let hash t = Hashtbl.hash (Model.Content.id t)
+ let compare t1 t2 =
+ Model.Identifier.compare (Model.Content.id t1) (Model.Content.id t2)
+
+ let equal t1 t2 =
+ Model.Identifier.equal (Model.Content.id t1) (Model.Content.id t2)
+
+ let hash t = Model.Identifier.hash (Model.Content.id t)
end
module Edge = struct
@@ -20,7 +24,7 @@ module Edge = struct
let compare e1 e2 =
match (e1, e2) with
| Field f1, Field f2 ->
- String.compare (Model.Field.name f1) (Model.Field.name f2)
+ Model.Identifier.compare (Model.Field.name f1) (Model.Field.name f2)
| Default, Default -> 0
| _, _ -> Stdlib.compare e1 e2
@@ -33,7 +37,7 @@ module GV_Printer = Graph.Graphviz.Dot (struct
include G
let graph_attributes _ = []
- let vertex_name v = Model.Content.id v
+ let vertex_name v = Model.Identifier.to_string (Model.Content.id v)
let vertex_attributes _ = []
let default_vertex_attributes _ = []
let edge_attributes _ = []
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index 66cd1fe..4150295 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -7,10 +7,12 @@ module Make (S : Decoders.Decode.S) = struct
(** Recursive type declaring a content and the subcontents *)
type declared_content =
| C of
- string * (S.value * string) Model.Section.t list * declared_content list
+ Model.Identifier.t
+ * (S.value * Model.Identifier.t) Model.Section.t list
+ * declared_content list
let rec content_builder :
- declared_content -> (S.value * string) Model.Content.t =
+ declared_content -> (S.value * Model.Identifier.t) Model.Content.t =
fun c ->
let (C (content_id, sections, declared_subcontents)) = c in
let children = List.map declared_subcontents ~f:content_builder in
@@ -31,8 +33,17 @@ module Make (S : Decoders.Decode.S) = struct
let dsl = Parser.ExpressionParser.of_string content in
Result.map_error Decoders.Error.make dsl |> S.from_result
- let field_type : (S.value * string) Model.Field.field_ex S.decoder =
- let text_type : (S.value * string) Model.Field.field_ex S.decoder =
+ let identifier : Model.Identifier.t S.decoder =
+ let* id = S.string in
+ if String.equal String.empty id then
+ S.fail "Expected a non empty identifier"
+ else
+ match Model.Identifier.of_string_opt id with
+ | Some id -> S.succeed id
+ | None -> S.fail "Expected an identifier with only the accepted characters"
+
+ let field_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ let text_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
S.and_then
(fun value ->
match String.lowercase_ascii value with
@@ -40,8 +51,8 @@ module Make (S : Decoders.Decode.S) = struct
| "integer" -> S.succeed (Model.Field.T Integer)
| other -> S.fail other)
S.string
- and content : (S.value * string) Model.Field.field_ex S.decoder =
- let* content_t = keep @@ S.field "content" S.string
+ and content : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ let* content_t = keep @@ S.field "content" identifier
and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool
and* constraints = S.field_opt "constraints" dsl in
@@ -62,8 +73,8 @@ module Make (S : Decoders.Decode.S) = struct
let* rule = S.field "rule" dsl and* message = S.field "message" textI18n in
S.succeed Model.Field.{ rule; message }
- let field : (S.value * string) Model.Field.t S.decoder =
- let* name = S.field "name" S.string
+ let field : (S.value * Model.Identifier.t) Model.Field.t S.decoder =
+ let* name = S.field "name" identifier
and* label = S.field "label" textI18n
and* type' = S.field "type" field_type
and* required = S.field_opt_or ~default:false "required" S.bool
@@ -76,22 +87,14 @@ module Make (S : Decoders.Decode.S) = struct
(Model.Field.create type' label ~required ?placeholder ~validation
?criteria ?icon name)
- let section : (S.value * string) Model.Section.t S.decoder =
+ let section : (S.value * Model.Identifier.t) Model.Section.t S.decoder =
let* label = S.field_opt_or ~default:[] "label" textI18n
and* criteria = S.field_opt "criteria" dsl
and* fields = S.field "field" (S.list field) in
let icon = None in
S.succeed Model.Section.{ label; criteria; fields; icon }
- let identifier : string S.decoder =
- let* id = S.string in
- if String.equal String.empty id then
- S.fail "Expected a non empty identifier"
- else if String.contains id '.' then
- S.fail "Expected an identifier with only the accepted characters"
- else S.succeed id
-
- let content : (S.value * string) Model.Content.t S.decoder =
+ let content : (S.value * Model.Identifier.t) Model.Content.t S.decoder =
(* Build a temporary type without the parent information *)
let* c =
S.fix (fun content_decoder ->
@@ -130,7 +133,7 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Undefined target \"" ^ name
+ "Undefined target \"" ^ Model.Identifier.to_string name
^ "\". Expected existing list or content"
in
S.fail_with (Decoders.Error.make ~context message))
@@ -139,12 +142,15 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Unsupported target \"" ^ name
+ "Unsupported target \"" ^ Model.Identifier.to_string name
^ "\". Lists as target are not implemented yet"
in
S.fail_with (Decoders.Error.make ~context message))
| Error (Graph_tree.Cycle contents) ->
- let content_name = List.map contents ~f:(fun c -> Model.Content.id c) in
+ let content_name =
+ List.map contents ~f:(fun c ->
+ Model.Identifier.to_string (Model.Content.id c))
+ in
let message =
String.concat ~sep:" "
("Dependency circle found between:" :: content_name)
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index 58c15ed..bad5b3b 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -117,3 +117,101 @@ Cycles between required content are not accepted
> ]
> }
Dependency circle found between: entity user
+
+Identifiers are validated when the configuration is parsed: characters
+outside letters, digits and underscore are rejected (a double quote could
+otherwise break out of the quoted SQL identifiers):
+
+ $ tests --configuration <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "evil\"; DROP TABLE entity;--"
+ > }
+ > ]
+ > }
+ > EOF
+ in field "content":
+ while decoding a list:
+ element 0:
+ in field "id":
+ Expected an identifier with only the accepted characters, but got
+ "evil\"; DROP TABLE entity;--"
+
+Field names are validated the same way:
+
+ $ tests --configuration <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "bad\"name",
+ > "label": {},
+ > "type": "text"
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ in field "content":
+ while decoding a list:
+ element 0:
+ in field "section":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "name":
+ Expected an identifier with only the accepted characters, but got
+ "bad\"name"
+
+Reference targets are validated the same way:
+
+ $ tests --configuration <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "ref",
+ > "label": {},
+ > "type": {
+ > "content": "bad\"target"
+ > }
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ in field "content":
+ while decoding a list:
+ element 0:
+ in field "section":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "type":
+ I tried the following decoders but they all failed:
+ "text" decoder:
+ Expected a string, but got
+ { "content": "bad\"target" }
+ "content" decoder:
+ in field "content":
+ Expected an identifier with only the accepted characters, but got
+ "bad\"target"