Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-side data validation in Meteor

I have a form and a submit function in my client file:

function submitme () {
    var message = $('#daform').serializeJSON();
    message.owner = Meteor.user().username;
    if(!message.description || !message.location.lat || !message.location.lng || !message.mysex || !message.yoursex) {
      return;          
      }
      else
      {
          lists.insert(message);
          console.log("Submitted!");
          $('#daform')[0].reset();
      }
}

That works pretty well although - it's CLIENT side validation => not secure.

How do I implement a "back-up" validation check in my server file? ( + bonus question : how do I set a timer so that once you've submitted you need to wait X seconds before you re-submit? )

like image 397
George Katsanos Avatar asked Mar 10 '13 16:03

George Katsanos


People also ask

What is server-side data validation?

When you enter data, the browser and/or the web server will check to see that the data is in the correct format and within the constraints set by the application. Validation done in the browser is called client-side validation, while validation done on the server is called server-side validation.

How can we do server-side validation?

In the Server Side Validation, the input submitted by the user is being sent to the server and validated using one of server side scripting languages such as ASP.Net, PHP etc. After the validation process on the Server Side, the feedback is sent back to the client by a new dynamically generated web page.

What is server-side validation in asp net?

Server-side validation helps prevent users from bypassing validation by disabling or changing the client script. Security Note: By default, ASP.NET Web pages automatically validate that malicious users are not attempting to send script or HTML elements to your application.

Is server-side validation necessary?

Always! Web technologies have evolved a lot in the past few years, both on the server-side as well as on the client's side. There are many web frameworks, UI kits, JavaScript libraries and everything you need to easily and rapidly develop a website or web application.


1 Answers

You can use http://docs.meteor.com/#deny (You can use allow but it might be easier to put validation stuff in a seperate deny) as deny will override allow in the event it shouldn't be inserted:

It works just as a backup method on the server just before its inserted.

With your message collection

Server Js

message.deny({
    insert: function (userId, doc) {
        return (!doc.description || !doc.location.lat || !doc.location.lng || !doc.mysex || !doc.yoursex);
    },
    update: function (userId, docs, fields, modifier) {
        return (!doc.description || !doc.location.lat || !doc.location.lng || !doc.mysex || !doc.yoursex);
    }
);

Note: Returning false from deny means not to deny. To deny the update, you must return true.

like image 52
Tarang Avatar answered Oct 26 '22 23:10

Tarang