Skip to content

Commit

Permalink
Fix env substitution in access token generation (internal-1893)
Browse files Browse the repository at this point in the history
* Fix ENV substitution in generate-access-token-script.js

* fix CSP urls
  • Loading branch information
stepankuzmin authored and underoot committed Oct 25, 2024
1 parent 123357e commit 63b8d31
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"parserOptions": {
"projectService": {
"defaultProject": "./tsconfig.json",
"allowDefaultProject": ["*.js", "debug/*.html"]
"allowDefaultProject": ["*.js", "debug/*", "build/*"]
}
},
"plugins": [
Expand Down
10 changes: 5 additions & 5 deletions debug/access_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ mapboxgl.accessToken = getAccessToken();

function getAccessToken() {
const accessToken = [
typeof process !== 'undefined' && process.env.MapboxAccessToken,
typeof process !== 'undefined' && process.env.MAPBOX_ACCESS_TOKEN,
process.env.MapboxAccessToken,
process.env.MAPBOX_ACCESS_TOKEN,
getURLParameter('access_token'),
localStorage.getItem('accessToken'),
// this token is a fallback for CI and testing. it is domain restricted to localhost
'pk.eyJ1IjoibWFwYm94LWdsLWpzIiwiYSI6ImNram9ybGI1ajExYjQyeGxlemppb2pwYjIifQ.LGy5UGNIsXUZdYMvfYRiAQ'
].find(Boolean);

try {
localStorage.setItem('accessToken', accessToken);
} catch (_) {
Expand All @@ -20,7 +21,6 @@ function getAccessToken() {
}

function getURLParameter(name) {
var regexp = new RegExp('[?&]' + name + '=([^&#]*)', 'i');
var output = regexp.exec(window.location.href);
return output && output[1];
const url = new URL(window.location.href);
return url.searchParams.get(name);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"test-build": "tsx ./node_modules/.bin/tape test/build/**/*.test.js",
"watch-render": "cross-env SUITE_NAME=render testem -f test/integration/testem/testem.js",
"watch-query": "SUITE_NAME=query testem -f test/integration/testem/testem.js",
"test-csp": "vitest --config vitest.config.csp.js --run",
"test-csp": "npm run build-token && vitest --config vitest.config.csp.ts --run",
"test-render": "cross-env SUITE_NAME=render testem ci -f test/integration/testem/testem.js",
"test-render-firefox": "cross-env BROWSER=Firefox SUITE_NAME=render testem ci -f test/integration/testem/testem.js",
"test-render-safari": "cross-env BROWSER=Safari SUITE_NAME=render testem ci -f test/integration/testem/testem.js",
Expand Down
2 changes: 1 addition & 1 deletion test/integration/csp-tests/fixtures/csp.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div id="map"></div>
<script src="/dist/mapbox-gl.js"></script>
<script src="/debug/access_token.js"></script>
<script src="/debug/access_token_generated.js"></script>
<script>

window.addEventListener('message', (e) => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/csp-tests/fixtures/strict.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div id="map"></div>
<script src="/dist/mapbox-gl-csp.js"></script>
<script src="/debug/access_token.js"></script>
<script src="/debug/access_token_generated.js"></script>
<script>
mapboxgl.workerUrl = '/dist/mapbox-gl-csp-worker.js';
window.addEventListener('message', (e) => {
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.csp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineConfig, mergeConfig} from 'vite';
import {defineConfig, mergeConfig} from 'vitest/config';
import baseConfig from './vitest.config.base';

export default mergeConfig(baseConfig, defineConfig({
Expand Down

0 comments on commit 63b8d31

Please sign in to comment.