Skip to content

Commit 4647c06

Browse files
committedJun 18, 2020
feat: add ngSanitize. set up security headers and CSP
- use a nonce for AngularJS Material theme styles - apply `ng-csp` directive to document - set better cache headers - add script for local Firebase Hosting emulation
1 parent b7013f1 commit 4647c06

11 files changed

+88
-9
lines changed
 

‎.firebaserc

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"projects": {
3+
"default": "material-hybrid"
4+
},
25
"targets": {
36
"material-hybrid": {
47
"hosting": {
@@ -8,4 +11,4 @@
811
}
912
}
1013
}
11-
}
14+
}

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ speed-measure-plugin.json
3737
/connect.lock
3838
/coverage
3939
/libpeerconnection.log
40-
.log
40+
*.log
4141
yarn-error.log
4242
testem.log
4343
/typings

‎angular.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"builder": "@angular-devkit/build-angular:browser",
2626
"options": {
2727
"aot": true,
28+
"extractCss": true,
2829
"outputPath": "dist/",
2930
"index": "src/index.html",
3031
"main": "src/main.ts",

‎firebase.json

+59-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,69 @@
55
"public": "dist/",
66
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
77
"appAssociation": "AUTO",
8+
"cleanUrls": true,
89
"rewrites": [
910
{
10-
"source": "**",
11+
"source": "/**/!(*.@(js|ts|html|css|json|svg|png|jpg|jpeg|webp|gif|ico|woff2|woff|ttf|webmanifest))",
1112
"destination": "/index.html"
1213
}
13-
]
14+
],
15+
"headers": [
16+
{
17+
"source": "/**(*.@(css|js|json|html|svg))",
18+
"headers": [
19+
{
20+
"key": "X-Content-Type-Options",
21+
"value": "nosniff"
22+
}
23+
]
24+
},
25+
{
26+
"source": "/**",
27+
"headers": [
28+
{
29+
"key": "X-XSS-Protection",
30+
"value": "1"
31+
},
32+
{
33+
"key": "X-Frame-Options",
34+
"value": "DENY"
35+
},
36+
{
37+
"key": "Content-Security-Policy",
38+
"value": "upgrade-insecure-requests; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src *; media-src 'self'; script-src 'self' 'unsafe-inline' https://*.googleapis.com https://apis.google.com; child-src 'self' blob:; connect-src 'self' https://*.googleapis.com https://fonts.gstatic.com https://apis.google.com;"
39+
}
40+
]
41+
},
42+
{
43+
"source": "/assets/**",
44+
"headers": [
45+
{
46+
"key": "Cache-Control",
47+
"value": "public, max-age=604800, s-maxage=1209600"
48+
}
49+
]
50+
},
51+
{
52+
"source": "/*.@(webmanifest|ico)",
53+
"headers": [
54+
{
55+
"key": "Cache-Control",
56+
"value": "public, max-age=604800, s-maxage=1209600"
57+
}
58+
]
59+
},
60+
{
61+
"source": "/*.@(js|css)",
62+
"headers": [
63+
{
64+
"key": "Cache-Control",
65+
"value": "public, max-age=31536000"
66+
}
67+
]
68+
}
69+
],
70+
"trailingSlash": false
1471
}
1572
]
1673
}

‎package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"scripts": {
66
"ng": "ng",
77
"start": "npm run build:templates && ng serve",
8+
"start:static": "firebase serve --only hosting:app",
89
"build:templates": "gulp templates",
910
"watch:templates": "gulp watch",
1011
"build": "npm run build:templates && ng build",
12+
"build:prod": "npm run build:templates && ng build --prod",
1113
"test": "ng test",
1214
"prettier": "prettier --write \"**/*.{js,json,css,scss,less,md,ts,html,component.html}\"",
1315
"lint": "ng lint",
@@ -36,6 +38,7 @@
3638
"angular-aria": "^1.8.0",
3739
"angular-material": "^1.1.22",
3840
"angular-messages": "^1.8.0",
41+
"angular-sanitize": "^1.8.0",
3942
"firebase": "^7.15.0",
4043
"rxjs": "^6.5.5",
4144
"tslib": "^1.11.2",

‎src/app/angularjs/app-angularjs.module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import 'angular-animate';
33
import 'angular-aria';
44
import 'angular-messages';
55
import 'angular-material';
6+
import 'angular-sanitize';
67
import './templates/index';
78
import { downgradeComponent, downgradeModule } from '@angular/upgrade/static';
9+
import { environment } from '../../environments/environment';
810
import { AppComponent } from '../angular/app.component';
911
import { appAngularJSComponent } from './app-angularjs.component';
1012
import { versionStampComponent } from './version-stamp.component';
@@ -19,12 +21,13 @@ const configFunction = ($mdThemingProvider, $mdGestureProvider) => {
1921
.primaryPalette('indigo')
2022
.accentPalette('green', { default: '500' })
2123
.backgroundPalette('grey', { default: 'A100' });
24+
$mdThemingProvider.setNonce(`${btoa(environment.version)}`);
2225
$mdGestureProvider.skipClickHijack();
2326
};
2427
configFunction.$inject = ['$mdThemingProvider', '$mdGestureProvider'];
2528

2629
export const appAngularjsModule = angular
27-
.module('AngularJSApp', ['ngMaterial', 'ngMessages', 'templates', downgradedModule])
30+
.module('AngularJSApp', ['ngMaterial', 'ngMessages', 'ngSanitize', 'templates', downgradedModule])
2831
.config(configFunction)
2932
.component(appAngularJSComponent.selector, appAngularJSComponent)
3033
.component(versionStampComponent.selector, versionStampComponent)

‎src/environments/environment.prod.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { version } from '../../package.json';
2+
13
export const environment = {
24
production: true,
5+
version,
36
};

‎src/environments/environment.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { version } from '../../package.json';
2+
13
// This file can be replaced during build by using the `fileReplacements` array.
24
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
35
// The list of file replacements can be found in `angular.json`.
46

57
export const environment = {
68
production: false,
9+
version,
710
};
811

912
/*

‎src/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en-US">
2+
<html lang="en-US" ng-csp>
33
<head>
44
<meta charset="utf-8" />
55
<title>AngularJS/Angular Material Hybrid</title>
@@ -17,8 +17,8 @@
1717
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" />
1818
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
1919

20-
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
21-
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
20+
<link rel="preconnect" href="https://fonts.googleapis.com" />
21+
<link rel="preconnect" href="https://fonts.gstatic.com" />
2222
</head>
2323
<body class="mat-typography">
2424
<app-angularjs>Loading...</app-angularjs>

‎tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"importHelpers": true,
1313
"target": "es2015",
1414
"typeRoots": ["node_modules/@types"],
15-
"lib": ["es2018", "dom"]
15+
"lib": ["es2018", "dom"],
16+
"resolveJsonModule": true
1617
}
1718
}

0 commit comments

Comments
 (0)
Failed to load comments.