Skip to content

Commit e99b961

Browse files
wardpeetGatsbyJS Bot
authored and
GatsbyJS Bot
committed
fix(gatsby-plugin-guess-js): don't leak jwt in gatsby-browser (#18434)
* fix(gatsby-plugin-guess-js): don't leak jwt in gatsby-browser * add tests for jwt removal * move to preInit to delete pluginOptions which actually works
1 parent 40cccc6 commit e99b961

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { onPreInit, onPreBootstrap } from "../gatsby-node"
2+
import { GuessPlugin } from "guess-webpack"
3+
4+
jest.mock(`guess-webpack`)
5+
6+
describe(`gatsby-plugin-guess-js`, () => {
7+
describe(`onPreInit`, () => {
8+
it(`should delete jwt pluginOptions`, () => {
9+
const pluginOptions = {
10+
jwt: `mykeys`,
11+
}
12+
13+
onPreInit({}, pluginOptions)
14+
15+
expect(pluginOptions).not.toHaveProperty(`jwt`)
16+
})
17+
})
18+
19+
describe(`onPreBootstrap`, () => {
20+
it(`should still have a jwt token to be used in jwt`, () => {
21+
const pluginOptions = {
22+
jwt: `mykeys`,
23+
// period: {
24+
// start: `2019-10-09`,
25+
// end: `2019-10-10`,
26+
// },
27+
GAViewID: `1234`,
28+
}
29+
30+
onPreInit({}, pluginOptions)
31+
onPreBootstrap({}, pluginOptions)
32+
33+
expect(GuessPlugin).toHaveBeenNthCalledWith(
34+
1,
35+
expect.objectContaining({
36+
jwt: `mykeys`,
37+
GA: `1234`,
38+
})
39+
)
40+
})
41+
})
42+
})

packages/gatsby-plugin-guess-js/src/gatsby-node.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
const { GuessPlugin } = require(`guess-webpack`)
22

33
let guessPlugin
4+
let jwt
5+
46
exports.onPreInit = (_, pluginOptions) => {
5-
const { period, jwt, GAViewID, reportProvider } = pluginOptions
6-
// delete sensitive information after use
7+
jwt = pluginOptions.jwt
8+
9+
// remove jwt from our config as we don't want it to leak into gatsby-browser.js
710
delete pluginOptions.jwt
8-
period.startDate = new Date(period.startDate)
9-
period.endDate = new Date(period.endDate)
11+
}
12+
13+
exports.onPreBootstrap = (_, pluginOptions) => {
14+
const { GAViewID, reportProvider } = pluginOptions
15+
let { period } = pluginOptions
16+
17+
if (period) {
18+
period.startDate = new Date(period.startDate)
19+
period.endDate = new Date(period.endDate)
20+
} else {
21+
const startDate = new Date()
22+
// We'll load 1 year of data if no period is being specified
23+
startDate.setDate(startDate.getDate() - 365)
24+
period = {
25+
startDate,
26+
endDate: new Date(),
27+
}
28+
}
29+
1030
guessPlugin = new GuessPlugin({
1131
// GA view ID.
1232
GA: GAViewID,
@@ -28,12 +48,7 @@ exports.onPreInit = (_, pluginOptions) => {
2848

2949
// Optional argument. It takes the data for the last year if not
3050
// specified.
31-
period: period
32-
? period
33-
: {
34-
startDate: new Date(`2018-1-1`),
35-
endDate: new Date(),
36-
},
51+
period,
3752
})
3853
}
3954

0 commit comments

Comments
 (0)