Skip to content

Commit a44e822

Browse files
Dan WahlinDan Wahlin
Dan Wahlin
authored and
Dan Wahlin
committed
Updated how csrf is handled
1 parent 125e429 commit a44e822

14 files changed

+91
-61
lines changed

src/.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceRoot}/server.js",
12+
"cwd": "${workspaceRoot}",
13+
"outFiles": [],
14+
"sourceMaps": true
15+
},
16+
{
17+
"type": "node",
18+
"request": "attach",
19+
"name": "Attach to Process",
20+
"port": 5858,
21+
"outFiles": [],
22+
"sourceMaps": true
23+
}
24+
]
25+
}

src/controllers/api/tokens/tokens.controller.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
const util = require('util');
1+
const util = require('util'),
2+
url = require('url');
3+
4+
//#### WARNING: Shown for an example but not recommended!!!!
5+
//#### Read more at https://github.com/pillarjs/understanding-csrf
6+
//#### The following is not recommended - said that twice now!!!! :-)
27

38
class TokensController {
49

510
constructor(router) {
11+
//Check referer
12+
router.use(this.refererCheck.bind(this));
13+
14+
//This can be VERY, VERY DANGEROUS if not done properly so just avoid it! Make sure:
15+
//1. CORS is disabled for this route if you've enabled CORS (CORS is not enabled in this app)
16+
// Note that disabling CORS won't prevent GET/POST requests using standard HTML though
17+
//2. Should always check referrer to be safe (see referrerCheck() middleware above)
618
router.get('/csrf', this.getCsrfToken.bind(this));
719
}
820

21+
refererCheck(req, res, next) {
22+
//Simple check to ensure that calls to routes here are only supported for http(s)://localhost:3000
23+
var referer = url.parse(req.headers.referer);
24+
console.log('Referer: ' + req.headers.referer);
25+
if (referer.host !== 'localhost' && referer.port !== '3000') {
26+
throw new Error('Invalid request');
27+
}
28+
next();
29+
}
30+
931
getCsrfToken(req, res) {
1032
console.log('*** getCsrfToken');
11-
const csrfToken = res.locals._csrf;
12-
res.json({ csrfToken: csrfToken });
33+
res.json({ csrfToken: res.locals._csrf });
1334
}
1435
}
1536

src/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"reflect-metadata": "^0.1.8",
2727
"rxjs": "5.0.0-beta.12",
2828
"zone.js": "^0.6.26",
29-
"body-parser": "~1.15.2",
29+
"cookie-parser": "^1.4.3",
30+
"body-parser": "^1.15.2",
3031
"csurf": "^1.9.0",
3132
"errorhandler": "^1.5.0",
3233
"express": "^4.14.0",

src/public/app/core/core.module.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/app/core/core.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/app/core/core.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NgModule, Optional, SkipSelf } from '@angular/core';
2-
import { HttpModule } from '@angular/http';
2+
import { HttpModule, XSRFStrategy, CookieXSRFStrategy } from '@angular/http';
33

44
import { DataService } from './data.service';
55
import { DataFilterService } from './data-filter.service';
@@ -9,7 +9,10 @@ import { EnsureModuleLoadedOnceGuard } from '../shared/ensureModuleLoadedOnceGua
99

1010
@NgModule({
1111
imports: [ HttpModule ],
12-
providers: [DataService, DataFilterService, Sorter, TrackByService] // these should be singleton
12+
providers: [
13+
//Default XSRF provider setup (change cookie or header name if needed):
14+
//{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('XSRF-TOKEN', 'X-XSRF-TOKEN') },
15+
DataService, DataFilterService, Sorter, TrackByService] // these should be singleton
1316
})
1417
export class CoreModule extends EnsureModuleLoadedOnceGuard { //Ensure that CoreModule is only loaded into AppModule
1518

src/public/app/core/data.service.js

Lines changed: 6 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/app/core/data.service.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/app/core/data.service.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,11 @@ import { ICustomer, IOrder, IState } from '../shared/interfaces';
1313
export class DataService {
1414

1515
baseUrl: string = '/api/customers';
16-
csrfToken: string = null;
1716

1817
constructor(private http: Http) {
19-
this.onInit();
20-
}
21-
22-
onInit() {
23-
this.getCsrfToken();
24-
}
2518

26-
getCsrfToken() {
27-
return this.http.get('/api/tokens/csrf')
28-
.map((res: Response) => res.json().csrfToken)
29-
.catch(this.handleError)
30-
.subscribe((token: string) => {
31-
this.csrfToken = token;
32-
},
33-
(err) => console.log(err));
3419
}
35-
20+
3621
getCustomers() : Observable<ICustomer[]> {
3722
return this.http.get(this.baseUrl)
3823
.map((res: Response) => {
@@ -64,7 +49,7 @@ export class DataService {
6449
}
6550

6651
insertCustomer(customer: ICustomer) : Observable<ICustomer> {
67-
return this.http.post(this.baseUrl, customer, this.getRequestOptions())
52+
return this.http.post(this.baseUrl, customer)
6853
.map((res: Response) => {
6954
const data = res.json();
7055
console.log('insertCustomer status: ' + data.status);
@@ -74,7 +59,7 @@ export class DataService {
7459
}
7560

7661
updateCustomer(customer: ICustomer) : Observable<ICustomer> {
77-
return this.http.put(this.baseUrl + '/' + customer._id, customer, this.getRequestOptions())
62+
return this.http.put(this.baseUrl + '/' + customer._id, customer)
7863
.map((res: Response) => {
7964
const data = res.json();
8065
console.log('updateCustomer status: ' + data.status);
@@ -84,14 +69,15 @@ export class DataService {
8469
}
8570

8671
deleteCustomer(id: string) : Observable<boolean> {
87-
return this.http.delete(this.baseUrl + '/' + id, this.getRequestOptions())
72+
return this.http.delete(this.baseUrl + '/' + id)
8873
.map((res: Response) => res.json().status)
8974
.catch(this.handleError);
9075
}
9176

9277
getRequestOptions() {
78+
//Not needed since
9379
const options = new RequestOptions({
94-
headers: new Headers({ 'csrf-token': this.csrfToken })
80+
headers: new Headers({ 'x-xsrf-token': this.csrfToken })
9581
});
9682
return options;
9783
}

src/public/app/customers/customer-edit-reactive.component.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)