Maj terminée. Pour consulter la release notes associée voici le lien :
https://about.gitlab.com/releases/2021/07/07/critical-security-release-gitlab-14-0-4-released/

web_site.ml 8.15 KB
Newer Older
Stephane Glondu's avatar
Stephane Glondu committed
1 2 3
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
Stephane Glondu's avatar
Stephane Glondu committed
4
(*  Copyright © 2012-2014 Inria                                           *)
Stephane Glondu's avatar
Stephane Glondu committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful, but   *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

22
open Lwt
23
open Serializable_t
24
open Signatures
25
open Common
Stephane Glondu's avatar
Stephane Glondu committed
26
open Web_serializable_j
27
open Web_common
28
open Web_signatures
Stephane Glondu's avatar
Stephane Glondu committed
29

30 31 32 33
module type CONFIG = sig
  val name : string
  val path : string list
  val source_file : string
34
  val auth_config : auth_config list
35
end
Stephane Glondu's avatar
Stephane Glondu committed
36

37 38 39 40 41 42 43 44 45 46 47 48
let get_single_line x =
  match_lwt Lwt_stream.get x with
  | None -> return None
  | Some _ as line ->
    lwt b = Lwt_stream.is_empty x in
    if b then (
      return line
    ) else (
      Lwt_stream.junk_while (fun _ -> true) x >>
      return None
    )

49
module Make (C : CONFIG) : SITE = struct
50
  open Eliom_service
Stephane Glondu's avatar
Stephane Glondu committed
51 52
  open Eliom_registration

53 54
  let make_path x = C.path @ x

55
  module Auth = Web_auth.Make (C)
Stephane Glondu's avatar
Stephane Glondu committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69
  let store = Ocsipersist.open_store C.name

  (* Persistent table, used to initialize the server. *)
  let election_ptable = Ocsipersist.open_table (C.name ^ "_elections")

  (* In-memory table, indexed by UUID, contains closures. *)
  let election_table = ref SMap.empty

  lwt main_election =
    Ocsipersist.make_persistent store "main_election" None

  lwt featured =
    Ocsipersist.make_persistent store "featured_elections" []
Stephane Glondu's avatar
Stephane Glondu committed
70 71 72 73

  (* The following reference is there to cut a dependency loop:
     S.register_election depends on S (via Templates). It will be set
     to a proper value once we have called Templates.Make. *)
74

75
  let import_election_ref = ref (fun _ -> assert false)
Stephane Glondu's avatar
Stephane Glondu committed
76 77 78 79 80

  (* We use an intermediate module S that will be passed to Templates
     and Web_election. S is not meant to leak and will be included
     in the returned module later. *)

81
  module S : SITE = struct
Stephane Glondu's avatar
Stephane Glondu committed
82
    include Auth.Services
83
    include Auth.Handlers
Stephane Glondu's avatar
Stephane Glondu committed
84 85
    open Eliom_parameter

Stephane Glondu's avatar
Stephane Glondu committed
86 87
    let scope = Eliom_common.default_session_scope

Stephane Glondu's avatar
Stephane Glondu committed
88
    let home = service
89
      ~path:(make_path [""])
Stephane Glondu's avatar
Stephane Glondu committed
90
      ~get_params:unit
91 92
      ()

Stephane Glondu's avatar
Stephane Glondu committed
93
    let source_code = service
94
      ~path:(make_path ["belenios.tar.gz"])
Stephane Glondu's avatar
Stephane Glondu committed
95 96 97 98
      ~get_params:unit
      ()

    let get_randomness = service
99
      ~path:(make_path ["get-randomness"])
Stephane Glondu's avatar
Stephane Glondu committed
100 101 102
      ~get_params:unit
      ()

Stephane Glondu's avatar
Stephane Glondu committed
103 104
    let cont = Eliom_reference.eref ~scope
      (fun () () -> Eliom_registration.Redirection.send home)
105

106
    let import_election ~featured f = !import_election_ref featured f
107

108 109 110 111 112 113 114 115 116
    let set_main_election x =
      if SMap.mem x !election_table then (
        Ocsipersist.set main_election (Some x)
      ) else (
        Lwt.fail Not_found
      )

    let unset_main_election () =
      Ocsipersist.set main_election None
117

Stephane Glondu's avatar
Stephane Glondu committed
118 119
  end

Stephane Glondu's avatar
Stephane Glondu committed
120 121
  include S

122
  module T = Web_templates.Make (S)
Stephane Glondu's avatar
Stephane Glondu committed
123

124 125 126 127 128 129 130 131 132 133 134
  let register_election params web_params =
    let module P = (val params : ELECTION_PARAMS) in
    let uuid = Uuidm.to_string P.params.e_uuid in
    let module D = struct
      module G = P.G
      let election = {
        e_params = P.params;
        e_pks = None;
        e_fingerprint = P.fingerprint;
      }
    end in
135 136
    let module P = (val web_params : WEB_PARAMS) in
    let module R = Web_election.Make (D) (P) in
Stephane Glondu's avatar
Stephane Glondu committed
137
    let module W = R.W in
138
    let module X : EMPTY = R.Register (S) (T) in
Stephane Glondu's avatar
Stephane Glondu committed
139
    let election = (module W : WEB_ELECTION) in
140 141 142 143 144 145 146
    election_table := SMap.add uuid election !election_table;
    lwt () =
      if W.featured then (
        lwt the_featured = Ocsipersist.get featured in
        Ocsipersist.set featured (uuid :: the_featured)
      ) else return ()
    in
Stephane Glondu's avatar
Stephane Glondu committed
147 148
    return election

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  let () = import_election_ref := fun featured f ->
    lwt raw_election =
      Lwt_io.lines_of_file f.f_election |>
      get_single_line >>=
      (function
      | Some e -> return e
      | None -> Printf.ksprintf
        failwith "%s must contain a single line" f.f_election
      )
    in
    let params = Group.election_params_of_string raw_election in
    let module P = (val params : ELECTION_PARAMS) in
    let uuid = Uuidm.to_string P.params.e_uuid in
    lwt exists =
      try_lwt
        lwt _ = Ocsipersist.find election_ptable uuid in
        return true
      with Not_found -> return false
    in
    if exists then (
      return None
    ) else (
      lwt metadata =
        Lwt_io.chars_of_file f.f_metadata |>
        Lwt_stream.to_string >>=
        wrap1 metadata_of_string
      in
      let module X = struct
        let metadata = metadata
        let featured = featured
        let params_fname = f.f_election
        let public_keys_fname = f.f_public_keys
      end in
      let web_params = (module X : WEB_PARAMS) in
      Ocsipersist.add election_ptable uuid (raw_election, web_params) >>
      lwt election = register_election params web_params in
      let module W = (val election : WEB_ELECTION) in
      begin try_lwt
        let () =
          Ocsigen_messages.debug (fun () ->
            Printf.sprintf "Injecting credentials for %s" uuid
          )
        in
        Lwt_io.lines_of_file f.f_public_creds |>
        Lwt_stream.iter_s W.B.inject_cred
      with Not_found ->
        return ()
      end >>
      return (Some election)
    )

  lwt () =
    Ocsipersist.iter_step (fun uuid (raw_election, web_params) ->
      let params = Group.election_params_of_string raw_election in
      lwt election = register_election params web_params in
      let module W = (val election : WEB_ELECTION) in
      assert (uuid = Uuidm.to_string W.election.e_params.e_uuid);
      Ocsigen_messages.debug (fun () ->
        Printf.sprintf "Initialized election %s from persistent store" uuid
      );
      return ()
    ) election_ptable

212
  let () = let module X : EMPTY = Auth.Register (S) (T.Login (S)) in ()
Stephane Glondu's avatar
Stephane Glondu committed
213 214 215

  let () = Any.register ~service:home
    (fun () () ->
Stephane Glondu's avatar
Stephane Glondu committed
216
      Eliom_reference.unset cont >>
217
      match_lwt Ocsipersist.get main_election with
Stephane Glondu's avatar
Stephane Glondu committed
218
      | None ->
219 220 221 222 223 224 225 226 227 228
        lwt featured =
          Ocsipersist.get featured >>=
          Lwt_list.map_p (fun x ->
            let module W = (val SMap.find x !election_table : WEB_ELECTION) in
            return (module W : WEB_ELECTION_RO)
          )
        in
        T.home ~featured () >>= Html5.send
      | Some x ->
        let module W = (val SMap.find x !election_table : WEB_ELECTION) in
Stephane Glondu's avatar
Stephane Glondu committed
229 230 231 232 233 234
        Redirection.send W.S.home
    )

  let () = File.register
    ~service:source_code
    ~content_type:"application/x-gzip"
235
    (fun () () -> return C.source_file)
Stephane Glondu's avatar
Stephane Glondu committed
236

Stephane Glondu's avatar
Stephane Glondu committed
237 238 239 240 241 242 243 244 245
  let do_get_randomness =
    let prng = Lazy.lazy_from_fun (Lwt_preemptive.detach (fun () ->
      Cryptokit.Random.(pseudo_rng (string secure_rng 16))
    )) in
    let mutex = Lwt_mutex.create () in
    fun () ->
      Lwt_mutex.with_lock mutex (fun () ->
        lwt prng = Lazy.force prng in
        return Cryptokit.Random.(string prng 32)
Stephane Glondu's avatar
Stephane Glondu committed
246 247
      )

Stephane Glondu's avatar
Stephane Glondu committed
248 249 250 251 252 253 254 255
  let () = String.register
    ~service:get_randomness
    (fun () () ->
      lwt r = do_get_randomness () in
      Cryptokit.(transform_string (Base64.encode_compact ()) r) |>
      (fun x -> string_of_randomness { randomness=x }) |>
      (fun x -> return (x, "application/json"))
    )
Stephane Glondu's avatar
Stephane Glondu committed
256 257

end