Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added json schema validation to all incoming non-binary messages #71

Merged
merged 2 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"author": "LAB at Rockwell Group",
"license": "MIT",
"dependencies": {
"ajv": "^3.8.3",
"finalhandler": "^0.4.1",
"forever-monitor": "1.1.0",
"serve-static": "^1.10.2",
Expand Down
92 changes: 92 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id":"http://spacebrew.cc/messaging-schema#",
"$schema":"http://json-schema.org/draft-04/schema#",
"description":"a schema defining the messages sent by clients and admins",
"type":"object",
"oneOf":[
{"$ref":"#/definitions/config"},
{"$ref":"#/definitions/message"},
{"$ref":"#/definitions/admin"},
{"$ref":"#/definitions/route"}],
"definitions":{
"config":{
"type":"object",
"required":["config"],
"properties":{
"config":{
"type":"object",
"required":["name","description"],
"properties":{
"name":{"type":"string"},
"description":{"type":"string"},
"subscribe":{
"type":"object",
"properties":{
"messages":{
"type":"array",
"items":{
"type":"object",
"required":["type","name"],
"properties":{
"type":{"type":"string"},
"name":{"type":"string"},
"default":{}}}}}},
"publish":{
"type":"object",
"properties":{
"messages":{
"type":"array",
"items":{
"type":"object",
"required":["type","name"],
"properties":{
"type":{"type":"string"},
"name":{"type":"string"},
"default":{}}}}}},
"options":{"type":"object"}}}}},
"message":{
"type":"object",
"required":["message"],
"properties":{
"message":{
"type":"object",
"required":["clientName","name","type","value"],
"properties":{
"clientName":{"type":"string"},
"name":{"type":"string"},
"type":{"type":"string"},
"value":{}}}}},
"admin":{
"type":"object",
"required":["admin"],
"properties":{
"admin":{},
"no_msgs":{}}},
"route":{
"type":"object",
"required":["route"],
"properties":{
"route":{
"type":"object",
"required":["publisher","subscriber","type"],
"properties":{
"type":{"enum":["add","remove"]},
"publisher":{
"type":"object",
"required":["type","clientName","remoteAddress","name"],
"properties":{
"type":{"type":"string"},
"clientName":{"type":"string"},
"name":{"type":"string"},
"remoteAddress":{"type":"string"}},
"subscriber":{
"type":"object",
"required":["type","clientName","remoteAddress","name"],
"properties":{
"type":{"type":"string"},
"clientName":{"type":"string"},
"name":{"type":"string"},
"remoteAddress":{"type":"string"}}}}}}}}
}
}

16 changes: 16 additions & 0 deletions spacebrew.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ var path = require('path')
, serveStatic = require('serve-static')
, http = require('http')
, finalhandler = require('finalhandler')
, AJV = require('ajv')
, fs = require('fs')
;

//setup validator
var ajv = AJV();
var validate = ajv.compile(JSON.parse(fs.readFileSync('schema.json')));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, should these be inside spacebrew.createServer, so they are scoped instead of global? That probably isn't an issue with Node.js though right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. In working on the electron version, I'm realizing we need to have some better mgmt of things outside scripts might want to manage, e.g. having some way of closing/editing server after open. With this, it's only an issue if there's something weird about a) when the file is loaded and b) where it's loaded from. Should it be __dirname + 'shema.json'??


//create a new WebsocketServer
spacebrew.createServer = function( opts ){
Expand Down Expand Up @@ -157,6 +163,7 @@ spacebrew.createServer = function( opts ){
var bValidMessage = false;
if (message && !flags.binary) {
logger.log("info", "[wss.onmessage] text message content: " + message);
logger.log('info', "[wss.onmessage] source: " + getClientAddress(connection));
// process WebSocket message
try {
var tMsg = JSON.parse(message);
Expand All @@ -165,6 +172,15 @@ spacebrew.createServer = function( opts ){
return;
}

//validate message against schema
if (!validate(tMsg)){
logger.log("warn", "[wss.onmessage] message did not pass JSON validation.");
for(var i = 0; i < validate.errors.length; i++){
logger.log('info', '[wss.onmessage] error ' + (i+1) + ': ' + validate.errors[i].message);
}
return;
}

try{
// handle client app configuration messages
if (tMsg['config']) {
Expand Down