Шлем и contentSecurityPolicy и используя nonce И добавляя его, но все равно получая ошибку - PullRequest
0 голосов
/ 28 апреля 2020

Я использую Helmet.contentSecurityPolicy, и здесь суть моего объекта:

МОИ СКРИПТЫ НЕ ПОГРУЖАЮТСЯ ..... это не загружено, но вы можете видеть, что оно у меня в доверенном items;

НЕ ЗАГРУЗКА: ЭТИ ПУНКТЫ ПОГРУЖЕНЫ ИЗ GOOGLETAGMANGER, НО НА ЭТОМ НЕТ РАЗ? enter image description here

И в теге сценария для некоторых из них, например googleTagmanager, я добавил одноразовый номер. Теперь для некоторых я не смог добавить, но я явно добавил их в конфигурацию.

example of nonce in script tag:
<script nonce="2d4f393ea5bc957db4f385232a53fcc8" async src="https://www.googletagmanager.com/gtag/js?id=*******"></script>

ЭТИ МЕСТНЫЕ ЛОКАЛЬНЫЕ Они созданы веб-пакетом, но у меня явно есть "localhost" в моих приемлемых элементах .... поэтому я запутался. Любая помощь?

The errors, are like the following: But I do HAVE the nonce tag (in some of them) AND you can see I include "unsafe-inline".

Refused to load the script '<URL>' because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' 'unsafe-inline' nonce-a449a007188e909846c2e74148c3e1b0 <URL> *.kustomerapp.com/ <URL> *.segment.com/ <URL> *.cloudfront.net <URL> *.stripe.com <URL> *.split.io <URL> *.googletagmanager.com 'self' <URL> ws://localhost:*". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

МОЙ ШЛЕМ СРЕДНЕГО ОБОРУДОВАНИЯ, ЧТОБЫ ПРИНЯТЬ ..

import helmet from 'helmet';

const trusted = [
  "'self'",
];

if (process.env.NODE_ENV !== 'production') {
  trusted.push('http://localhost:*', 'ws://localhost:*');
}

export default function contentSecurityPolicy(nonce) {
  return helmet.contentSecurityPolicy({
    directives: {
      defaultSrc: trusted,
      scriptSrc: [
        "'unsafe-eval'",
        "'unsafe-inline'",
        `nonce-${nonce}`,
        'https://www.googletagmanager.com',
        '*.googletagmanager.com',
      ].concat(trusted),
      styleSrc: [
        "'unsafe-inline'",
        '*.gstatic.com',
        '*.googleapis.com',
        'https://*.typography.com',
      ].concat(trusted),
      frameSrc: [
        '*.stripe.com',
      ].concat(trusted),
      fontSrc: [
        '*.cloudflare.com',
        'https://*.cloudflare.com',
        '*.bootstrapcdn.com',
        '*.googleapis.com',
        '*.gstatic.com',
        'data',
      ].concat(trusted),
      imgSrc: [
        'www.googletagmanager.com',
      ].concat(trusted),
    },
    // set to true if you only want to report errors
    reportOnly: false,
    // set to true if you want to set all headers
    setAllHeaders: false,
    // set to true if you want to force buggy CSP in Safari 5
    safari5: false
  });
};

Немного кода моего сервера для контекста:

const nonce = crypto.randomBytes(16).toString('hex');
const app = new Express();
app.use(cookieParser());
app.use(helmet());
app.use(helmet.referrerPolicy({ policy: 'same-origin' }));
app.use(contentSecurityPolicy(nonce));

[! [Введите описание изображения здесь] [2]] [2]

...