diff --git a/example/d-form/README.md b/example/d-form/README.md index 8a80c227..0e7327bf 100644 --- a/example/d-form/README.md +++ b/example/d-form/README.md @@ -16,7 +16,8 @@ let show_form ?message request =
You entered: <%s message %>!
% end; - <%s! Dream.form_tag ~action:"/" request %> + @@ -53,13 +54,13 @@ Try it in the [playground](http://dream.as/d-form).<%s comment %>
<% ); %> - <%s! Dream.form_tag ~action:"/" request %> + diff --git a/example/h-sql/sql.eml.ml b/example/h-sql/sql.eml.ml index fe406b97..582ad660 100644 --- a/example/h-sql/sql.eml.ml +++ b/example/h-sql/sql.eml.ml @@ -25,7 +25,8 @@ let render comments request = % comments |> List.iter (fun (_id, comment) -><%s comment %>
<% ); %> - <%s! Dream.form_tag ~action:"/" request %> + diff --git a/example/w-flash/README.md b/example/w-flash/README.md index 515a547d..d25a1f0d 100644 --- a/example/w-flash/README.md +++ b/example/w-flash/README.md @@ -13,7 +13,8 @@ absolutely primitive form with just one field: let form request = - <%s! Dream.form_tag ~action:"/" request %> + diff --git a/example/w-flash/flash.eml.ml b/example/w-flash/flash.eml.ml index 1b9cd121..8bba6415 100644 --- a/example/w-flash/flash.eml.ml +++ b/example/w-flash/flash.eml.ml @@ -1,7 +1,8 @@ let form request = - <%s! Dream.form_tag ~action:"/" request %> + diff --git a/example/w-multipart-dump/multipart_dump.eml.ml b/example/w-multipart-dump/multipart_dump.eml.ml index 6521436c..879112fe 100644 --- a/example/w-multipart-dump/multipart_dump.eml.ml +++ b/example/w-multipart-dump/multipart_dump.eml.ml @@ -1,7 +1,8 @@ let home request = - <%s! Dream.form_tag ~action:"/" ~enctype:`Multipart_form_data request %> + diff --git a/example/w-upload-stream/README.md b/example/w-upload-stream/README.md index ac03a22d..9105ec1c 100644 --- a/example/w-upload-stream/README.md +++ b/example/w-upload-stream/README.md @@ -11,7 +11,8 @@ the total size of each uploaded file: let home request = - <%s! Dream.form_tag ~action:"/" ~enctype:`Multipart_form_data request %> + @@ -70,7 +71,7 @@ Try it in the [playground](http://dream.as/w-upload-stream). The report page shows one file without a name ("None"). This is, in fact, the CSRF token generated by -[`Dream.form_tag`](https://aantron.github.io/dream/#val-form_tag) inside the +[`Dream.csrf_tag`](https://aantron.github.io/dream/#val-csrf_tag) inside the template. To keep the example simple, we didn't check the CSRF token, nor filter out the `dream.csrf` field that it appears in. If you'd like to do so in your code, see diff --git a/example/w-upload-stream/upload_stream.eml.ml b/example/w-upload-stream/upload_stream.eml.ml index 743f5960..a0e35d7e 100644 --- a/example/w-upload-stream/upload_stream.eml.ml +++ b/example/w-upload-stream/upload_stream.eml.ml @@ -1,7 +1,8 @@ let home request = - <%s! Dream.form_tag ~action:"/" ~enctype:`Multipart_form_data request %> + diff --git a/src/dream.ml b/src/dream.ml index 426a64cc..b5525c90 100644 --- a/src/dream.ml +++ b/src/dream.ml @@ -195,6 +195,7 @@ let verify_csrf_token = Csrf.verify_csrf_token ~now let form_tag ?method_ ?target ?enctype ?csrf_token ~action request = Tag.form_tag ~now ?method_ ?target ?enctype ?csrf_token ~action request +let csrf_tag = Tag.csrf_tag ~now (* Middleware *) diff --git a/src/dream.mli b/src/dream.mli index 504853ba..ed7a6e87 100644 --- a/src/dream.mli +++ b/src/dream.mli @@ -910,12 +910,13 @@ val origin_referrer_check : middleware (** {1 Forms} - {!Dream.form_tag} and {!Dream.val-form} round-trip secure forms. - {!Dream.form_tag} is used inside a template to generate a form header with a - CSRF token: + {!Dream.csrf_tag} and {!Dream.val-form} round-trip secure forms. + {!Dream.csrf_tag} is used inside a form template to generate a hidden field + with a CSRF token: {[ - <%s! Dream.form_tag ~action:"/" request %> + ]} @@ -956,13 +957,13 @@ type 'a form_result = [ val form : ?csrf:bool -> request -> (string * string) list form_result promise (** Parses the request body as a form. Performs CSRF checks. Use - {!Dream.form_tag} in a template to transparently generate forms that will - pass these checks. See {!section-templates} and example + {!Dream.csrf_tag} in a form template to transparently generate forms that + will pass these checks. See {!section-templates} and example {{:https://github.com/aantron/dream/tree/master/example/d-form#readme} [d-form]}. - [Content-Type:] must be [application/x-www-form-urlencoded]. - - The form must have a field named [dream.csrf]. {!Dream.form_tag} adds such + - The form must have a field named [dream.csrf]. {!Dream.csrf_tag} adds such a field. - {!Dream.form} calls {!Dream.verify_csrf_token} to check the token in [dream.csrf]. @@ -1047,15 +1048,14 @@ type multipart_form = val multipart : ?csrf:bool -> request -> multipart_form form_result promise (** Like {!Dream.form}, but also reads files, and [Content-Type:] must be - [multipart/form-data]. The [