Skip to content

Commit ccc997b

Browse files
author
Arun Patra
authored
Merge pull request #7 from Reloadly/develop
Support for Javascript users
2 parents 42c1448 + c03a019 commit ccc997b

15 files changed

Lines changed: 644 additions & 800 deletions

.eslintrc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"rules": {
3-
"semi": ["error", "always"],
4-
"quotes": ["error", "double"]
3+
"semi": [
4+
"error",
5+
"always"
6+
],
7+
"quotes": [
8+
"error",
9+
"double"
10+
]
511
}
612
}

.gitignore

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
logs
33
*.log
44
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
85

96
.idea/
107
package-lock.json
11-
# Diagnostic reports (https://nodejs.org/api/report.html)
12-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
138

149
# Runtime data
1510
pids
@@ -24,43 +19,10 @@ lib-cov
2419
coverage
2520
*.lcov
2621

27-
# nyc test coverage
28-
.nyc_output
29-
30-
# node-waf configuration
31-
.lock-wscript
32-
33-
# Compiled binary addons (https://nodejs.org/api/addons.html)
34-
build/Release
35-
3622
# Dependency directories
3723
node_modules/
38-
jspm_packages/
39-
40-
# TypeScript v1 declaration files
41-
typings/
42-
43-
# TypeScript cache
44-
*.tsbuildinfo
45-
46-
# Optional npm cache directory
47-
.npm
48-
49-
# Optional eslint cache
50-
.eslintcache
51-
52-
53-
# Optional REPL history
54-
.node_repl_history
5524

5625
# Output of 'npm pack'
5726
*.tgz
5827

59-
# Yarn Integrity file
60-
.yarn-integrity
61-
62-
# dotenv environment variables file
63-
.env
64-
.env.test
65-
66-
dist
28+
lib

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/package-lock.json
44

55
# coverage
6-
/.nyc_output
76
/coverage
87

98
src

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"printWidth": 80,
2+
"printWidth": 120,
33
"trailingComma": "all",
44
"singleQuote": true
55
}

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,60 @@
22

33
Lightweight Http Firewall to protect against common threats.
44

5-
This is a direct port of the [Spring Security HttpFirewall](https://docs.spring.io/spring-security/reference/servlet/exploits/firewall.html).
5+
This is a direct port of
6+
the [Spring Security HttpFirewall](https://docs.spring.io/spring-security/reference/servlet/exploits/firewall.html).
67

7-
- This library is a middleware for Express Server.
8-
- Its highly recommended not to disable firewall rules, since its extremely risky to do so. You can however provided your own overrides that gives you the options of disable rules or provide your own constraints.
8+
- This library is a middleware for Express Server.
9+
- Its highly recommended not to disable firewall rules, since its extremely risky to do so. You can however provided
10+
your own overrides that gives you the options of disable rules or provide your own constraints.
911
- If a threat is detected, a HTTP status code 403 is returned to the caller, and no further processing happens.
1012
- Calls which pass the firewall rules, will process as normal.
1113

1214
## Examples ##
1315

1416
The firewall can be configured as shown below:
1517

18+
### TypeScript Usage ###
19+
1620
```typescript
1721
import express, { Request, Response } from 'express';
18-
import { HttpFirewallOptions, Predicate } from "../types";
19-
import { StrictHttpFirewall } from "../index";
22+
import { HttpFirewallOptions, Predicate, httpFirewall } from '@prizemates/http-firewall';
2023

2124
const app = express();
2225
const port = 3000;
2326

24-
// This must be added first, before adding any routes
25-
app.use(new StrictHttpFirewall(firewallOptions()).firewall)
26-
27-
// Or, to simply use the firewall with default rules:
28-
//app.use(httpFirewall)
27+
// This middleware must be added before adding any other routes
28+
app.use(httpFirewall())
29+
// Or, you can customize the behaviour by providing options. See HttpFirewallOptions
30+
// app.use(httpFirewall({logToConsole : true}));
2931

3032
app.get('/', (req: Request, res: Response) => {
31-
res.send('Http Firewall Demo running');
33+
res.send('Http Firewall Demo running');
3234
});
3335

3436
app.listen(port, () => {
35-
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
37+
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
3638
});
39+
```
3740

41+
### Javascript Usage ###
3842

39-
function firewallOptions(): HttpFirewallOptions {
40-
// Allows traffic from specific hosts only
41-
const allowedHostnamesPredicate =
42-
Predicate.of<string>(h => h.endsWith('example.com')).or(
43-
Predicate.of<string>(h => h === "localhost"));
43+
```javascript
44+
const express = require("express");
45+
const { httpFirewall } = require("@prizemates/http-firewall");
46+
47+
const app = express();
48+
const port = 3000;
4449

45-
return {
46-
allowedHostnames: allowedHostnamesPredicate,
47-
allowedHttpMethods: ['POST', 'GET'],
48-
};
49-
}
50+
app.use(httpFirewall())
51+
// Or, you can customize the behaviour by providing options. See HttpFirewallOptions
52+
// app.use(httpFirewall({logToConsole : true}));
5053

54+
app.get('/', (req: Request, res: Response) => {
55+
res.send('Http Firewall Demo running');
56+
});
57+
58+
app.listen(port, () => {
59+
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
60+
});
5161
```

babel.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
2-
presets: [
3-
['@babel/preset-env', {targets: {node: 'current'}}],
4-
'@babel/preset-typescript',
5-
],
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript"
5+
]
66
};

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "@prizemates/httpt-firewall",
3-
"version": "0.0.2",
2+
"name": "@prizemates/http-firewall",
3+
"version": "0.0.3",
44
"description": "HTTP Firewall based on Spring Security HttpFirewall",
55
"private": false,
6-
"main": "./dist/demo/demo.js",
7-
"types": "./dist/index.d.ts",
6+
"main": "./lib/index.js",
7+
"types": "./lib/index.d.ts",
88
"files": [
9-
"dist/**/*"
9+
"lib/**/*"
1010
],
1111
"scripts": {
1212
"build": "tsc",
13-
"start": "node dist/demo/demo.js",
14-
"demo": "node dist/demo/demo.js",
13+
"start": "node lib/demo/demo.js",
14+
"demo": "node lib/demo/demo.js",
1515
"test": "jest",
1616
"format": "prettier --write \"src/**/*.ts\"",
1717
"lint": "eslint",
18-
"prepare" : "npm run build",
19-
"prepublishOnly" : "npm test && npm run lint"
18+
"prepare": "npm run build",
19+
"prepublishOnly": "npm test && npm run lint"
2020
},
2121
"jest": {
2222
"testMatch": [

src/__tests__/strict.http.firewall.tests.ts renamed to src/__tests__/strict-http-firewall.tests.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { HttpFirewallOptions } from '../types';
2-
import { StrictHttpFirewall } from '../strict.http.firewall';
1+
import { httpFirewall, HttpFirewallOptions } from '../strict-http-firewall';
32
import express from 'express';
43
import request from 'supertest';
54

@@ -10,10 +9,8 @@ describe('HttpStrictFirewall test suite', () => {
109
const options: HttpFirewallOptions = {
1110
allowedHttpMethods: ['POST', 'GET'],
1211
};
13-
app.use(new StrictHttpFirewall(options).firewall);
14-
const res = await request(app)
15-
.head('/')
16-
.set('Content-Type', 'application/json');
12+
app.use(httpFirewall(options));
13+
const res = await request(app).head('/').set('Content-Type', 'application/json');
1714
expect(res.statusCode).toBe(403);
1815
});
1916

@@ -23,16 +20,14 @@ describe('HttpStrictFirewall test suite', () => {
2320
const options: HttpFirewallOptions = {
2421
allowedHttpMethods: ['POST', 'GET'],
2522
};
26-
app.use(new StrictHttpFirewall(options).firewall);
23+
app.use(httpFirewall(options));
2724
app.get('/', (req, res) => {
2825
// You're working with an express req and res now.
2926
res.status(200).send();
3027
});
3128

3229
// Act
33-
const res = await request(app)
34-
.get('/')
35-
.set('Content-Type', 'application/json');
30+
const res = await request(app).get('/').set('Content-Type', 'application/json');
3631

3732
// Assert
3833
expect(res.statusCode).toBe(200);

src/demo/demo.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import express, { Express, Request, Response } from 'express';
2-
import { HttpFirewallOptions, Predicate } from '../types';
3-
import { StrictHttpFirewall } from '../index';
2+
import { httpFirewall, HttpFirewallOptions, Predicate } from '../index';
43

54
const app: Express = express();
65
const port = 5428;
76

87
// This must be added first, before adding any routes
9-
app.use(new StrictHttpFirewall(firewallOptions()).firewall);
10-
11-
// Or, to simply use the firewall with default rules:
12-
//app.use(httpFirewall)
8+
app.use(httpFirewall(firewallOptions()));
139

1410
app.get('/', (req: Request, res: Response) => {
1511
res.send('Http Firewall Demo running');
@@ -21,9 +17,9 @@ app.listen(port, () => {
2117

2218
function firewallOptions(): HttpFirewallOptions {
2319
// Allows traffic from specific hosts only
24-
const allowedHostnamesPredicate = Predicate.of<string>((h) =>
25-
h.endsWith('example.com'),
26-
).or(Predicate.of<string>((h) => h === 'localhost'));
20+
const allowedHostnamesPredicate = Predicate.of<string>((h) => h.endsWith('example.com')).or(
21+
Predicate.of<string>((h) => h === 'localhost'),
22+
);
2723

2824
return {
2925
allowedHostnames: allowedHostnamesPredicate,

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export * from './strict.http.firewall';
1+
export {
2+
httpFirewall,
3+
HttpFirewallOptions,
4+
HttpMethod,
5+
Predicate
6+
} from './strict-http-firewall';

0 commit comments

Comments
 (0)