Skip to content

Commit ff94ed5

Browse files
gatsbybottyhopppieh
authored
fix(gatsby-plugin-mdx): don't allow JS frontmatter by default (#35830) (#35834)
Co-authored-by: Ty Hopp <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 36f21b0 commit ff94ed5

29 files changed

+351
-21
lines changed

e2e-tests/mdx-less-babel/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ yarn-error.log
99

1010
# Cypress output
1111
cypress/videos/
12+
cypress/screenshots/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Nothing here, do not remove
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe(`webpack loader`, () => {
2+
it(`---js frontmatter should not parse by default`, () => {
3+
cy.visit(`/js-frontmatter`).waitForRouteChange()
4+
5+
// Check frontmatter not parsed in page context
6+
cy.get(`[data-cy="js-frontmatter"]`).invoke(`text`).should(`eq`, `disabled`)
7+
})
8+
9+
it(`---javascript frontmatter should not parse by default`, () => {
10+
cy.visit(`/javascript-frontmatter`).waitForRouteChange()
11+
12+
// Check frontmatter not parsed in page context
13+
cy.get(`[data-cy="js-frontmatter"]`).invoke(`text`).should(`eq`, `disabled`)
14+
})
15+
})
16+
17+
describe(`data layer`, () => {
18+
it(`---js or ---javascript frontmatter should not parse by default`, () => {
19+
cy.visit(`/mdx-query-js-frontmatter/`).waitForRouteChange()
20+
cy.contains(`I should not be parsed`).should("not.exist")
21+
})
22+
})
23+
24+
it(`---js and ---javascript frontmatter should not allow remote code execution`, () => {
25+
cy.readFile(`cypress/fixtures/file-to-attempt-rce-on.txt`).should(
26+
`eq`,
27+
`Nothing here, do not remove`
28+
)
29+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
exports.createSchemaCustomization = ({ actions }) => {
2+
const { createTypes } = actions
3+
4+
createTypes(`
5+
type Mdx implements Node {
6+
frontmatter: Frontmatter
7+
}
8+
type Frontmatter {
9+
title: String
10+
}
11+
`)
12+
}

e2e-tests/mdx-less-babel/package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"@mdx-js/mdx": "^1.6.6",
77
"@mdx-js/react": "^1.6.6",
88
"cypress": "^3.1.0",
9-
"gatsby": "^2.0.118",
10-
"gatsby-plugin-mdx": "^1.2.19",
11-
"gatsby-source-filesystem": "^2.3.14",
9+
"gatsby": "^3.0.0",
10+
"gatsby-plugin-mdx": "^2.0.0",
11+
"gatsby-source-filesystem": "^3.0.0",
1212
"react": "^16.9.0",
1313
"react-dom": "^16.9.0",
1414
"theme-ui": "^0.3.1"
@@ -18,10 +18,11 @@
1818
],
1919
"license": "MIT",
2020
"scripts": {
21-
"build": "gatsby build",
22-
"develop": "gatsby develop",
21+
"clean": "gatsby clean",
22+
"build": "cross-env CYPRESS_SUPPORT=y gatsby build",
23+
"develop": "cross-env CYPRESS_SUPPORT=y gatsby develop",
2324
"format": "prettier --write '**/*.js'",
24-
"test": "cross-env CYPRESS_SUPPORT=y npm run build && npm run start-server-and-test",
25+
"test": "npm run build && npm run start-server-and-test",
2526
"start-server-and-test": "start-server-and-test serve http://localhost:9000 cy:run",
2627
"serve": "gatsby serve",
2728
"cy:open": "cypress open",
@@ -34,4 +35,4 @@
3435
"prettier": "2.0.4",
3536
"start-server-and-test": "^1.7.1"
3637
}
37-
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---javascript
2+
(() => {
3+
require(`fs`).writeFileSync(`${process.cwd()}/cypress/fixtures/file-to-attempt-rce-on.txt`, (new Error('Helpful stack trace if this does execute. It should not execute.')).stack)
4+
console.trace()
5+
return {
6+
title: `I should not be parsed`
7+
}
8+
})()
9+
10+
---
11+
12+
<h1>JS frontmatter engine is disabled by default</h1>
13+
14+
<span data-cy="js-frontmatter">
15+
{props.pageContext.frontmatter?.title || `disabled`}
16+
</span>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---js
2+
(() => {
3+
require(`fs`).writeFileSync(`${process.cwd()}/cypress/fixtures/file-to-attempt-rce-on.txt`, (new Error('Helpful stack trace if this does execute. It should not execute.')).stack)
4+
console.trace()
5+
return {
6+
title: `I should not be parsed`
7+
}
8+
})()
9+
10+
---
11+
12+
<h1>JS frontmatter engine is disabled by default</h1>
13+
14+
<span data-cy="js-frontmatter">
15+
{props.pageContext.frontmatter?.title || `disabled`}
16+
</span>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react"
2+
import { graphql } from "gatsby"
3+
4+
export default function PageRunningGraphqlResolversOnJSFrontmatterTestInputs({
5+
data,
6+
}) {
7+
return <pre>{JSON.stringify(data.allMdx.nodes, null, 2)}</pre>
8+
}
9+
10+
export const query = graphql`
11+
{
12+
allMdx(filter: { slug: { glob: "frontmatter-engine/*" } }) {
13+
nodes {
14+
frontmatter {
15+
title
16+
}
17+
body
18+
excerpt
19+
tableOfContents
20+
timeToRead
21+
wordCount {
22+
paragraphs
23+
sentences
24+
words
25+
}
26+
mdxAST
27+
}
28+
}
29+
}
30+
`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---javascript
2+
(() => {
3+
require(`fs`).writeFileSync(`${process.cwd()}/cypress/fixtures/file-to-attempt-rce-on.txt`, (new Error('Helpful stack trace if this does execute. It should not execute.')).stack)
4+
console.trace()
5+
return {
6+
title: `I should not be parsed`
7+
}
8+
})()
9+
10+
---
11+
12+
<h1>JS frontmatter engine is disabled by default w</h1>
13+
14+
<span data-cy="js-frontmatter">
15+
{props.pageContext.frontmatter?.title || `disabled`}
16+
</span>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---js
2+
(() => {
3+
require(`fs`).writeFileSync(`${process.cwd()}/cypress/fixtures/file-to-attempt-rce-on.txt`, (new Error('Helpful stack trace if this does execute. It should not execute.')).stack)
4+
console.trace()
5+
return {
6+
title: `I should not be parsed`
7+
}
8+
})()
9+
10+
---
11+
12+
<h1>JS frontmatter engine is disabled by default</h1>
13+
14+
<span data-cy="js-frontmatter">
15+
{props.pageContext.frontmatter?.title || `disabled`}
16+
</span>

0 commit comments

Comments
 (0)