Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Use static worker with mapbox-gl #40667

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/legacy/server/csp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import { promisify } from 'util';

const randomBytesAsync = promisify(randomBytes);

export const DEFAULT_CSP_RULES = Object.freeze([
`script-src 'unsafe-eval' 'nonce-{nonce}'`,
Copy link
Contributor Author

@thomasneirynck thomasneirynck Jul 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the worker-src blob: and child-src blob: directives because of this change.

The script-src directive, as is, will no longer work as the mapbox-gl code is now trying to load an external script. We could modify it and add strict-dynamic (?)

e.g.:

export const DEFAULT_CSP_RULES = Object.freeze([
  `script-src 'unsafe-eval' 'nonce-{nonce}' 'strict-dynamic`
]);

This is the suggested work-around as discussed here for workers and nonce-based policies w3c/webappsec-csp#375.

Maybe we should load mapbox-gl in a separate script tag (and not as a vendors-bundle), and only enable strict-dynamic for mapbox-gl?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we don't have any options for specifying the nonce attribute on the script tag which is added to the DOM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we don't have any options for specifying the nonce attribute on the script tag which is added to the DOM?

Correct. We don't have control over how the worker-script loaded by mapbox-gl-csp.js, which in turn loads content with importScripts.

If we want to preserve a custom script-src directive with nonce based security (do we want this in the long run (?)), maybe we could modify how we load scripts and provided nonces on all <script> tags (same as now), but load mapbox-gl-csp.js in a separate script-tag (different as now, as it is packaged with vendors.dll.js now). Only for that mapbox-gl-csp.js script, we could enable strict-dynamic, because it's I think the only script which loads child-scripts (ie. the worker).

^ just a thought. Not 100% sure about the impact of adding strict-dynamic (or the removal of script-src directive completely for that matter).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to preserve a custom script-src directive with nonce based security (do we want this in the long run (?))

At this time, that's the intent. There's potential for us to switch to script-src 'unsafe-inline' 'self' but it's not something we have a plan to do.

Only for that mapbox-gl-csp.js script, we could enable strict-dynamic, because it's I think the only script which loads child-scripts (ie. the worker).

I believe that we have to add strict-dynamic as the CSP policy which applies to all scripts, I don't believe there's a way to scope this to only a single script tag.

'worker-src blob:',
'child-src blob:',
]);
export const DEFAULT_CSP_RULES = Object.freeze([]);

export async function generateCSPNonce() {
return (await randomBytesAsync(12)).toString('base64');
Expand Down
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/maps/public/components/map/mb/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
*/

import _ from 'lodash';
import mapboxgl from 'mapbox-gl';
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';
import mbWorkerUrl from '!!file-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
import chrome from 'ui/chrome';
import { MAKI_SPRITE_PATH } from '../../../../common/constants';

mapboxgl.workerUrl = mbWorkerUrl;

function relativeToAbsolute(url) {
const a = document.createElement('a');
a.setAttribute('href', url);
Expand Down