-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby-plugin-mdx): Do not leak frontmatter into page #35859
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
e2e-tests/mdx-less-babel/cypress/integration/frontmatter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const page = { | ||
js: `/frontmatter-js`, | ||
javascript: `/frontmatter-javascript`, | ||
yaml: `/frontmatter-yaml`, | ||
json: `/frontmatter-json`, | ||
graphqlQuery: `/frontmatter-graphql-query`, | ||
} | ||
|
||
// Attribute selector for element we assert against in pages | ||
const selector = `[data-cy="frontmatter"]` | ||
|
||
// Strings used for frontmatter titles | ||
const titles = { | ||
notParsed: `I should not be parsed`, | ||
parsed: `I am parsed`, | ||
} | ||
|
||
// Frontmatter that should not be rendered | ||
const otherKey = `Some other key` | ||
|
||
describe(`webpack loader`, () => { | ||
describe(`---yaml frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.yaml).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, titles.parsed) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---json frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.json).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, titles.parsed) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---js frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.js).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, `disabled`) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---javascript frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.javascript).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, `disabled`) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`data layer`, () => { | ||
it(`---js or ---javascript frontmatter should not parse by default`, () => { | ||
cy.visit(page.graphqlQuery).waitForRouteChange() | ||
cy.contains(titles.notParsed).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
it(`---js and ---javascript frontmatter should not allow remote code execution`, () => { | ||
cy.readFile(`cypress/fixtures/file-to-attempt-rce-on.txt`).should( | ||
`eq`, | ||
`Nothing here, do not remove` | ||
) | ||
}) |
29 changes: 0 additions & 29 deletions
29
e2e-tests/mdx-less-babel/cypress/integration/js-frontmatter.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
---json | ||
{ "title": "I am parsed", "otherKey": "Some other key" } | ||
|
||
--- | ||
|
||
<h1>A page with JSON frontmatter</h1> | ||
|
||
<span data-cy="frontmatter">{props.pageContext.frontmatter?.title}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
---yaml | ||
title: I am parsed | ||
otherKey: Some other key | ||
|
||
--- | ||
|
||
<h1>A page with YAML frontmatter</h1> | ||
|
||
<span data-cy="frontmatter">{props.pageContext.frontmatter?.title}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const page = { | ||
js: `/frontmatter-js`, | ||
javascript: `/frontmatter-javascript`, | ||
yaml: `/frontmatter-yaml`, | ||
json: `/frontmatter-json`, | ||
graphqlQuery: `/frontmatter-graphql-query`, | ||
} | ||
|
||
// Attribute selector for element we assert against in pages | ||
const selector = `[data-cy="frontmatter"]` | ||
|
||
// Strings used for frontmatter titles | ||
const titles = { | ||
notParsed: `I should not be parsed`, | ||
parsed: `I am parsed`, | ||
} | ||
|
||
// Frontmatter that should not be rendered | ||
const otherKey = `Some other key` | ||
|
||
describe(`webpack loader`, () => { | ||
describe(`---yaml frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.yaml).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, titles.parsed) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---json frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.json).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, titles.parsed) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---js frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.js).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, `disabled`) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
describe(`---javascript frontmatter`, () => { | ||
beforeEach(() => { | ||
cy.visit(page.javascript).waitForRouteChange() | ||
}) | ||
|
||
it(`should parse`, () => { | ||
cy.get(selector).invoke(`text`).should(`eq`, `disabled`) | ||
}) | ||
|
||
it(`should not leak into the page`, () => { | ||
cy.contains(otherKey).should(`not.exist`) | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`data layer`, () => { | ||
it(`---js or ---javascript frontmatter should not parse by default`, () => { | ||
cy.visit(page.graphqlQuery).waitForRouteChange() | ||
cy.contains(titles.notParsed).should(`not.exist`) | ||
}) | ||
}) | ||
|
||
it(`---js and ---javascript frontmatter should not allow remote code execution`, () => { | ||
cy.readFile(`cypress/fixtures/file-to-attempt-rce-on.txt`).should( | ||
`eq`, | ||
`Nothing here, do not remove` | ||
) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react" | ||
import { graphql } from "gatsby" | ||
|
||
export default function PageRunningGraphqlResolversOnJSFrontmatterTestInputs({ | ||
data, | ||
}) { | ||
return <pre>{JSON.stringify(data.allMdx.nodes, null, 2)}</pre> | ||
} | ||
|
||
export const query = graphql` | ||
{ | ||
allMdx(filter: { slug: { glob: "frontmatter-engine/*" } }) { | ||
nodes { | ||
frontmatter { | ||
title | ||
} | ||
body | ||
excerpt | ||
tableOfContents | ||
timeToRead | ||
wordCount { | ||
paragraphs | ||
sentences | ||
words | ||
} | ||
mdxAST | ||
} | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
---json | ||
{ "title": "I am parsed", "otherKey": "Some other key" } | ||
|
||
--- | ||
|
||
<h1>A page with JSON frontmatter</h1> | ||
|
||
<span data-cy="frontmatter">{props.pageContext.frontmatter?.title}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
---yaml | ||
title: I am parsed | ||
otherKey: Some other key | ||
|
||
--- | ||
|
||
<h1>A page with YAML frontmatter</h1> | ||
|
||
<span data-cy="frontmatter">{props.pageContext.frontmatter?.title}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
grayMatter
was not passed options prior to this change,matter
would always beundefined
, resulting in no frontmatter rendered to the page. This changes removesmatter
entirely from the output node raw body.Unsure if this is the correct fix since it's unclear to me why
matter
was included here in the first place (maybe it is stripped in later code paths?).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tracked first introduction of it to ChristopherBiscardi/gatsby-mdx#329 - there is 0 context on it - to me it seem more like debugging code than anything else - the
code
is in the MDX format, so it would just output any content put there as content which doesn;t make much sense (to me anyway).Especially with fact that
matter
in returned object means: