Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding grunt validation and cleanup tasks, adding npm test
  • Loading branch information
gvn committed Dec 18, 2013
1 parent f37c722 commit d06dc11
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.10
notifications:
email: false
30 changes: 30 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,30 @@
# Contribution Guidelines

## Reporting issues

- **Search for existing issues.** Please check to see if someone else has reported the same issue.
- **Share as much information as possible.** Include operating system and version, browser and version. Also, include steps to reproduce the bug.

## Project Setup

Refer to the [README]().

## Code Style

### JavaScript

JS files must pass JSHint using the provided [.jshintrc]() settings.

Additionally, JS files need to be run through [JSBeautify](https://github.com/einars/js-beautify) with the provided [.jsbeautifyrc]().

**TL;DR** Run `grunt clean` before pushing a commit. It will validate and beautify your JS.

#### Variable Naming

- `lowerCamelCase` General variables
- `UpperCamelCase` Constructor functions

## Pull requests

- Try not to pollute your pull request with unintended changes – keep them simple and small. If possible, squash your commits.
- If your PR resolves an issue, include **closes #ISSUE_NUMBER** in your commit message (or a [synonym](https://help.github.com/articles/closing-issues-via-commit-messages)).
38 changes: 38 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,38 @@
module.exports = function (grunt) {

grunt.initConfig({
jshint: {
all: ['*.js'],
options: {
jshintrc: '.jshintrc'
}
},
jsbeautifier: {
modify: {
src: ['*.js'],
options: {
config: '.jsbeautifyrc'
}
},
validate: {
src: ['*.js'],
options: {
mode: 'VERIFY_ONLY',
config: '.jsbeautifyrc'
}
}
}
});

grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.loadNpmTasks('grunt-contrib-jshint');

grunt.registerTask('default', ['clean']);

// Clean code before a commit
grunt.registerTask('clean', ['jsbeautifier:modify', 'jshint']);

// Validate code (read only)
grunt.registerTask('validate', ['jsbeautifier:validate', 'jshint']);

};
66 changes: 30 additions & 36 deletions app.js
@@ -1,6 +1,6 @@
var http = require('http');
var sequelize = require('sequelize');
var fs = require('fs');
var sequelize = require('sequelize');
var JSV = require('JSV').JSV;

var config = JSON.parse(fs.readFileSync('./env.json', {
Expand Down Expand Up @@ -70,7 +70,7 @@ function storeViolation(reportBody) {
sourceFile: reportBody['source-file'],
lineNumber: reportBody['column-number'],
statusCode: reportBody['status-code'],
userAgent: reportBody['userAgent']
userAgent: reportBody.userAgent
}).complete(function () {
console.log('Violation stored.');
});
Expand All @@ -92,55 +92,49 @@ http.createServer(function (req, res) {
var body;
var userAgent = req.headers['user-agent'];

// Attempt to parse JSON from stream
try {
body = Buffer.concat(bodyParts, bytes).toString('utf8');
json = JSON.parse(body);
console.log('Attempting to store violation:');
} catch (ex) {
console.log(body);
}

json.userAgent = userAgent;
var violatorDomain = json['csp-report']['document-uri'].match(/\/\/(.*)\//)[1];
var allowedDomain = false;
var allowedSource = true;

var violatorDomain = json['csp-report']['document-uri'].match(/\/\/(.*)\//)[1];
var allowedDomain = false;
var allowedSource = true;
// Ensure domain is allowed to report
config.domainWhitelist.forEach(function (domain) {
if (violatorDomain === domain) {
allowedDomain = true;
}
});

config.domainWhitelist.forEach(function (domain) {
if (violatorDomain === domain) {
allowedDomain = true;
// Ensure source isn't blacklisted
if (config.sourceBlacklist) {
config.sourceBlacklist.forEach(function (source) {
if (json['csp-report']['source-file'] === source) {
allowedSource = false;
}
});

// Ensure source isn't blacklisted
if (config.sourceBlacklist) {
config.sourceBlacklist.forEach(function (source) {
if (json['csp-report']['source-file'] === source) {
allowedSource = false;
}
});
}

if (allowedDomain && allowedSource) {
var report = json['csp-report'];
report.userAgent = userAgent;
storeViolation(report);
} else {
console.log('Ignoring CSP report');
}
} catch (ex) {
console.log(body);
}
});

req.on('close', function () {
console.log('req close');
// Log the CSP violation
if (allowedDomain && allowedSource) {
console.log('Attempting to store violation:');
var report = json['csp-report'];
report.userAgent = userAgent;
storeViolation(report);
} else {
console.log('Ignoring CSP report');
}
});

req.on('error', function () {
console.log('req error');
});

res.writeHead(200, {
'content-type': 'text/plain'
});

res.writeHead(200);
res.end();
}).listen(2600);
8 changes: 8 additions & 0 deletions package.json
Expand Up @@ -7,6 +7,9 @@
"type": "git",
"url": "git://github.com/gvn/csp-logger.git"
},
"scripts": {
"test": "grunt validate"
},
"keywords": [
"csp"
],
Expand All @@ -19,5 +22,10 @@
"sequelize": "~2.0.0-beta.2",
"mysql": "~2.0.0-rc2",
"JSV": "~4.0.2"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.7.2",
"grunt-jsbeautifier": "~0.2.6"
}
}

0 comments on commit d06dc11

Please sign in to comment.