Trying to start get
function from deployed website and get this error:
Refused to connect to 'https://www.themealdb.com/api/json/v2/xxx/search.php?s=apple' because it violates the following Content Security Policy directive: "default-src 'self' http://*.google-analytics.com http://www.googletagmanager.com https://*.google.com https://*.google-analytics.com https://*.googletagmanager.com https://*.gstatic.com https://*.googleapis.com https://authedmine.com https://az743702.vo.msecnd.net https://sentry.io ws://localhost:4200". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
While running website on localhost server everything was working fine.
Added this meta tag
to my index.html
and still getting the same error message.
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* 'unsafe-inline' 'unsafe-eval'">
So, at first I added meta tag for my index.html
page and it didin't worked.
After that I created interceptor
, to add headers to my get
function.
More about interceptors you can read at https://angular.io/api/common/http/HttpInterceptor
Still not working. Then I figured out that in my server.ts file there is these lines of code:
app.use(helmet());
app.use(helmet.contentSecurityPolicy({
directives: AppConfig.cspDirectives
}));
. More info about helmet
:
https://www.npmjs.com/package/helmet
Then in my app.module providers section there is assigned {provide: APP_CONFIG, useValue: AppConfig}
configuration.
Here is how app.config.ts file looks like now:
import {InjectionToken} from '@angular/core';
export let APP_CONFIG = new InjectionToken('app.config');
export const AppConfig: any = {
cspDirectives: {
defaultSrc: [
'\'self\'',
'http://*.google-analytics.com',
'http://www.googletagmanager.com',
'https://*.google.com',
'https://*.google-analytics.com',
'https://*.googletagmanager.com',
'https://*.gstatic.com',
'https://*.googleapis.com',
'https://authedmine.com',
'https://az743702.vo.msecnd.net',
'https://sentry.io',
'https://www.themealdb.com/',
'ws:<my-webpage-url>',
],
styleSrc: [
'\'self\'',
'\'unsafe-inline\'',
'https://*.googleapis.com'
],
scriptSrc: [
'\'self\'',
'\'unsafe-inline\'',
'http://*.googletagmanager.com',
'https://*.google-analytics.com'
]
}
};