Skip to content

Commit

Permalink
Merge pull request #6 from Reloadly/develop
Browse files Browse the repository at this point in the history
Linting support
  • Loading branch information
Arun Patra authored Jan 22, 2023
2 parents e8c72ea + 0600e00 commit 42c1448
Show file tree
Hide file tree
Showing 11 changed files with 688 additions and 592 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "double"]
}
}
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
# coverage
/.nyc_output
/coverage

src
tsconfig.json
.eslintrc
.prettierrc
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 80,
"trailingComma": "all",
"singleQuote": true
}
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prizemates/httpt-firewall",
"version": "0.0.1",
"version": "0.0.2",
"description": "HTTP Firewall based on Spring Security HttpFirewall",
"private": false,
"main": "./dist/demo/demo.js",
Expand All @@ -12,7 +12,11 @@
"build": "tsc",
"start": "node dist/demo/demo.js",
"demo": "node dist/demo/demo.js",
"test": "jest"
"test": "jest",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint",
"prepare" : "npm run build",
"prepublishOnly" : "npm test && npm run lint"
},
"jest": {
"testMatch": [
Expand All @@ -29,18 +33,21 @@
"Security"
],
"author": "Arun Patra",
"license": "MIT",
"license": "Apache-2.0",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"@babel/preset-typescript": "^7.18.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@types/express": "^4.17.15",
"@types/node": "^18.11.18",
"@types/jest": "^29.2.6",
"supertest": "^6.3.0",
"@types/node": "^18.11.18",
"jest": "^29.3.1",
"prettier": "^2.8.3",
"supertest": "^6.3.0",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"typescript": "^4.9.4"
}
}
70 changes: 34 additions & 36 deletions src/__tests__/strict.http.firewall.tests.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
import {HttpFirewallOptions} from "../types";
import {StrictHttpFirewall} from "../strict.http.firewall";
import express from "express";
import request from "supertest";
import { HttpFirewallOptions } from '../types';
import { StrictHttpFirewall } from '../strict.http.firewall';
import express from 'express';
import request from 'supertest';

describe('HttpStrictFirewall test suite', () => {
describe('Integration Tests: .firewall()', () => {
test('Should reject disallowed Http method', async () => {
const app = express();
const options: HttpFirewallOptions = {
allowedHttpMethods: ['POST', 'GET'],
};
app.use(new StrictHttpFirewall(options).firewall);
const res = await request(app)
.head('/')
.set('Content-Type', 'application/json');
expect(res.statusCode).toBe(403);
});

describe('Integration Tests: .firewall()', () => {
test('Should reject disallowed Http method', async () => {
const app = express();
const options: HttpFirewallOptions = {
allowedHttpMethods: ['POST', 'GET'],
}
app.use(new StrictHttpFirewall(options).firewall)
const res = await request(app)
.head('/')
.set('Content-Type', 'application/json')
expect(res.statusCode).toBe(403);
});

test('Should allow configured Http method', async () => {
// Arrange
const app = express();
const options: HttpFirewallOptions = {
allowedHttpMethods: ['POST', 'GET'],
}
app.use(new StrictHttpFirewall(options).firewall)
app.get('/', (req, res) => {
// You're working with an express req and res now.
res.status(200).send();
});
test('Should allow configured Http method', async () => {
// Arrange
const app = express();
const options: HttpFirewallOptions = {
allowedHttpMethods: ['POST', 'GET'],
};
app.use(new StrictHttpFirewall(options).firewall);
app.get('/', (req, res) => {
// You're working with an express req and res now.
res.status(200).send();
});

// Act
const res = await request(app)
.get('/')
.set('Content-Type', 'application/json')
// Act
const res = await request(app)
.get('/')
.set('Content-Type', 'application/json');

// Assert
expect(res.statusCode).toBe(200);
});
// Assert
expect(res.statusCode).toBe(200);
});

});
});
32 changes: 15 additions & 17 deletions src/demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import express, {Express, Request, Response} from 'express';
import {HttpFirewallOptions, Predicate} from "../types";
import {StrictHttpFirewall} from "../index";
import express, { Express, Request, Response } from 'express';
import { HttpFirewallOptions, Predicate } from '../types';
import { StrictHttpFirewall } from '../index';

const app: Express = express();
const port = 5428;

// This must be added first, before adding any routes
app.use(new StrictHttpFirewall(firewallOptions()).firewall)
app.use(new StrictHttpFirewall(firewallOptions()).firewall);

// Or, to simply use the firewall with default rules:
//app.use(httpFirewall)

app.get('/', (req: Request, res: Response) => {
res.send('Http Firewall Demo running');
res.send('Http Firewall Demo running');
});

app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});


function firewallOptions(): HttpFirewallOptions {

// Allows traffic from specific hosts only
const allowedHostnamesPredicate =
Predicate.of<string>(h => h.endsWith('example.com')).or(
Predicate.of<string>(h => h === "localhost"));

return {
allowedHostnames: allowedHostnamesPredicate,
allowedHttpMethods: ['POST', 'GET'],
};
// Allows traffic from specific hosts only
const allowedHostnamesPredicate = Predicate.of<string>((h) =>
h.endsWith('example.com'),
).or(Predicate.of<string>((h) => h === 'localhost'));

return {
allowedHostnames: allowedHostnamesPredicate,
allowedHttpMethods: ['POST', 'GET'],
};
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './strict.http.firewall'
export * from './strict.http.firewall';
Loading

0 comments on commit 42c1448

Please sign in to comment.