Create map module using MapBox

Hello,

I need your help. I’m trying to create a UX module using Mapbox to integrate a map into my supervision system.

I first added Mapbox to my package and copied the JS/CSS files into the vendor folder, but I got a file size error.
Then I tried using a CDN, but I had a CSP error.

I fixed the CSP issue by adding script-src in my station services configuration, but now I’m getting another error.

Is it possible to integrate Mapbox this way?

The second error is almost certainly the worker, and it is worth knowing why fixing script-src did not get you there.

The policy comes from WebService, HttpHeaderProviders, ContentSecurityPolicy, which is a BCspHeaderProvider. Its shipped defaults in 4.15 are default-src ‘self’ workbench, script-src ‘self’ workbench ‘unsafe-inline’ ‘unsafe-eval’, style-src ‘self’ workbench ‘unsafe-inline’, img-src ‘self’ workbench data: module:, connect-src ‘self’ workbench ws://%hostname%:%port% wss://%hostname%:%port%, and report-uri /csp-reports. Everything else on that component is an empty string, and generateHeader skips empty properties, so child-src, frame-src, font-src, media-src and manifest-src are not in the emitted header at all. The %hostname% and %port% tokens are filled per request from the servlet request.

Against that, Mapbox GL’s own CSP page asks for worker-src blob:, img-src data: blob:, and connect-src https://api.mapbox.com https://events.mapbox.com. Three of those four are missing from the default header. There is no worker-src slot on the component and no child-src value, so the fallback chain applies: worker-src, then child-src, then script-src. That lands on script-src ‘self’ workbench ‘unsafe-inline’ ‘unsafe-eval’, and blob: is not in it, so the blob worker is refused. img-src has data: but not blob:, so canvas and generated images go too.

Two ways out. The one that needs no policy edit is Mapbox’s CSP build, mapbox-gl-csp.js, with mapboxgl.workerUrl pointed at the worker file you ship yourself. That worker is then same-origin, and same-origin script is already allowed by the script-src fallback, so what is left to add is connect-src for api.mapbox.com and events.mapbox.com, and blob: on img-src. The other way is additionalDirectives on the CSP component, which is appended verbatim to the header with newlines stripped, so worker-src blob:; child-src blob: goes there rather than into one of the typed slots.

While you are debugging, use the station rather than the browser console. The report-uri default means the browser POSTs each violation back to /csp-reports; the servlet writes the JSON into the violationText property, which puts the component into fault. The first report is logged at SEVERE, along with a line saying further ones go to the web.reporting.csp log at FINE, so turn that log up if you are iterating. Either way the exact blocked-uri and violated-directive land in the station log, and the component has a Clear Fault Status action to reset it. Reports larger than niagara.web.security.csp.maxReportSize, default 8096 bytes, are dropped.

On the file size error, the message text tells you which limit you hit. The station side file upload is capped by niagara.fileRpc.maxFileSize, which defaults to 52428800 in file-ux’s own code, and the platform file transfer has its own “File not transferred, too large”. Neither should fire on a Mapbox bundle, so I would want the literal wording before guessing further.

One thing the CDN route will not survive: styles, glyphs, sprites and tiles are live calls to api.mapbox.com. If the JACE’s network has no egress there, no CSP change makes the map draw, and you are looking at self-hosted tiles instead. Shipping the library inside your own module, rc/ under a runtimeProfile=“ux” jar, addressed as module://yourModule/rc/…, at least makes the library itself an offline asset.