Skip to content

Commit 418dce1

Browse files
wardpeetvladar
authored andcommitted
fix(gatsby-source-wordpress): don't leak auth details (#32303)
(cherry picked from commit 4d7ec18)
1 parent 4319a6d commit 418dce1

9 files changed

Lines changed: 86 additions & 10 deletions

File tree

integration-tests/gatsby-source-wordpress/.env.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ WPGRAPHQL_GUTENBERG_VERSION=v0.3.8
1111
WPGRAPHQL_URL="http://localhost:8001/graphql"
1212
HTACCESS_USERNAME="admin"
1313
HTACCESS_PASSWORD="secret"
14-
WORDPRESS_BASIC_AUTH=YWRtaW46c2VjcmV0

integration-tests/gatsby-source-wordpress/__tests__/__snapshots__/index.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ Object {
219219
},
220220
"name": "main menu",
221221
},
222+
Object {
223+
"count": null,
224+
"databaseId": 37,
225+
"id": "dGVybTozNw==",
226+
"menuItems": Object {
227+
"nodes": Array [],
228+
},
229+
"name": "Primary",
230+
},
231+
Object {
232+
"count": null,
233+
"databaseId": 38,
234+
"id": "dGVybTozOA==",
235+
"menuItems": Object {
236+
"nodes": Array [],
237+
},
238+
"name": "Social Media",
239+
},
222240
],
223241
},
224242
},
@@ -324,6 +342,24 @@ Object {
324342
},
325343
"name": "main menu",
326344
},
345+
Object {
346+
"count": null,
347+
"databaseId": 37,
348+
"id": "dGVybTozNw==",
349+
"menuItems": Object {
350+
"nodes": Array [],
351+
},
352+
"name": "Primary",
353+
},
354+
Object {
355+
"count": null,
356+
"databaseId": 38,
357+
"id": "dGVybTozOA==",
358+
"menuItems": Object {
359+
"nodes": Array [],
360+
},
361+
"name": "Social Media",
362+
},
327363
],
328364
},
329365
},

integration-tests/gatsby-source-wordpress/__tests__/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
15
require(`dotenv`).config({
26
path: `.env.test`,
37
})
@@ -53,14 +57,17 @@ describe(`[gatsby-source-wordpress] Build default options`, () => {
5357
describe(`[gatsby-source-wordpress] Run tests on develop build`, () => {
5458
let gatsbyDevelopProcess
5559

56-
beforeAll(async done => {
60+
beforeAll(async () => {
5761
if (!isWarmCache) {
5862
await gatsbyCleanBeforeAll()
5963
}
6064

61-
if (isWarmCache && !process.env.WORDPRESS_BASIC_AUTH) {
65+
if (
66+
isWarmCache &&
67+
(!process.env.HTACCESS_USERNAME || !process.env.HTACCESS_PASSWORD)
68+
) {
6269
console.log(
63-
`Please add the env var WORDPRESS_BASIC_AUTH. It should be a string in the following pattern: base64Encode(\`\${username}:\${password}\`)`
70+
`Please add the env var HTACCESS_USERNAME and HTACCESS_PASSWORD. It should be a string in the following pattern: base64Encode(\`\${username}:\${password}\`)`
6471
)
6572

6673
await new Promise(resolve => setTimeout(resolve, 100))
@@ -78,7 +85,6 @@ describe(`[gatsby-source-wordpress] Run tests on develop build`, () => {
7885
gatsbyDevelopProcess = spawnGatsbyProcess(`develop`)
7986

8087
await urling(`http://localhost:8000`)
81-
done()
8288
})
8389

8490
require(`../test-fns/index`)

integration-tests/gatsby-source-wordpress/gatsby-config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const requestConcurrency = 1
99
const mediaItemTypeSettings = {
1010
localFile: {
1111
excludeByMimeTypes: ['video/mp4'],
12-
/**
12+
/**
1313
* This is set to one byte smaller than the largest image in the Gatsby site so that we will have exactly one image that isn't fetched
1414
* during the site build
1515
*/
@@ -99,6 +99,12 @@ module.exports = {
9999
develop: {
100100
hardCacheMediaFiles: true,
101101
},
102+
auth: {
103+
htaccess: {
104+
username: process.env.HTACCESS_USERNAME,
105+
password: process.env.HTACCESS_PASSWORD,
106+
},
107+
},
102108
...wpPluginOptions,
103109
},
104110
},

integration-tests/gatsby-source-wordpress/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"description": "A simple Gatsby WP site for running integrations tests",
66
"scripts": {
7-
"run-jest": "jest --runInBand && WARM_CACHE=true jest --runInBand",
7+
"run-jest": "jest --runInBand && cross-env WARM_CACHE=true jest --runInBand",
88
"test": "npm run docker-clean-start && npm run run-jest",
99
"docker-start": "docker-compose up --build --force-recreate --always-recreate-deps --remove-orphans -d",
1010
"docker-clean-start": "docker-compose stop && docker-compose rm -f && docker volume rm gatsby-source-wordpress_db_data gatsby-source-wordpress_wp_data || true && docker-compose pull && docker-compose build --no-cache --force-rm --pull && docker-compose up -d"
@@ -24,6 +24,7 @@
2424
"cross-env": "^7.0.3",
2525
"dotenv": "^8.2.0",
2626
"jest": "^26.6.3",
27+
"node-fetch": "^2.6.1",
2728
"rimraf": "^3.0.2",
2829
"urling": "^1.0.7"
2930
}

integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ describe(`data resolution`, () => {
4141
expect(data[`allWpComment`].totalCount).toBe(1)
4242
expect(data[`allWpTaxonomy`].totalCount).toBe(3)
4343
expect(data[`allWpCategory`].totalCount).toBe(9)
44-
expect(data[`allWpMenu`].totalCount).toBe(1)
44+
expect(data[`allWpMenu`].totalCount).toBe(3)
4545
expect(data[`allWpMenuItem`].totalCount).toBe(4)
4646
expect(data[`allWpPostFormat`].totalCount).toBe(0)
47-
expect(data[`allWpContentType`].totalCount).toBe(6)
47+
expect(data[`allWpContentType`].totalCount).toBe(9)
4848
})
4949

5050
testResolvedData({
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fetch = require("node-fetch")
2+
3+
jest.setTimeout(100000)
4+
5+
describe(`auth in gatsby-browser`, () => {
6+
test(`should not be present`, async () => {
7+
const res = await fetch("http://localhost:8000/commons.js")
8+
const jsFile = await res.text()
9+
10+
expect(jsFile.includes("/gatsby-source-wordpress/gatsby-browser.js")).toBe(
11+
true
12+
)
13+
expect(jstFile)
14+
.test(/auth\\":.*?\\"htaccess\\"/)
15+
.toBe(false)
16+
expect(jstFile)
17+
.test(/\\"username\\": \\"admin\\"/)
18+
.toBe(false)
19+
expect(jstFile)
20+
.test(/\\"password\\": \\"secret\\"/)
21+
.toBe(false)
22+
})
23+
})

integration-tests/gatsby-source-wordpress/test-fns/test-utils/authed-wpgql-request.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ exports.authedWPGQLRequest = async (query, { variables } = {}) => {
1212
variables,
1313
url: process.env.WPGRAPHQL_URL,
1414
headers: {
15-
Authorization: `Basic ${process.env.WORDPRESS_BASIC_AUTH}`,
15+
Authorization: `Basic ${Buffer.from(
16+
`${process.env.HTACCESS_USERNAME}:${process.env.HTACCESS_PASSWORD}`
17+
).toString("base64")}`,
1618
},
1719
})
1820

packages/gatsby-source-wordpress/src/steps/process-and-validate-plugin-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,8 @@ export const processAndValidatePluginOptions = (
8686
}
8787
})
8888

89+
// remove auth from pluginOptions so we don't leak into the browser
90+
delete pluginOptions.auth
91+
8992
return userPluginOptions
9093
}

0 commit comments

Comments
 (0)