Skip to content
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

Disallow new lines in paths when checking with isValidPath #6055

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/spotty-kiwis-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@graphql-tools/utils": patch
---

Disallow new lines in paths when checking with `isValidPath`

A string may sometimes look like a path but is not (like an SDL of a simple
GraphQL schema). To make sure we don't yield false-positives in such cases,
we disallow new lines in paths (even though most Unix systems support new
lines in file names).
10 changes: 9 additions & 1 deletion packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ export function isDocumentString(str: any): boolean {
return false;
}

const invalidPathRegex = /[‘“!%^<>`]/;
const invalidPathRegex = /[‘“!%^<>`\n]/;
/**
* Checkes whether the `str` contains any path illegal characters.
*
* A string may sometimes look like a path but is not (like an SDL of a simple
* GraphQL schema). To make sure we don't yield false-positives in such cases,
* we disallow new lines in paths (even though most Unix systems support new
* lines in file names).
*/
export function isValidPath(str: any): boolean {
return typeof str === 'string' && !invalidPathRegex.test(str);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/utils/tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isValidPath } from '../src/helpers';

describe('helpers', () => {
it.each([
`schema @transport(subgraph: "API", kind: "rest", location: "http://0.0.0.0:4001", headers: "{\"Content-Type\":\"application/json\"}") {
query: Query
mutation: Mutation
subscription: Subscription
}`,
])('should detect "%s" as NOT a valid path', str => {
expect(isValidPath(str)).toBeFalsy();
});

it.each(['file', 'file.tsx', 'some/where/file.tsx', '/some/where/file.tsx'])(
'should detect "%s" as a valid path',
str => {
expect(isValidPath(str)).toBeTruthy();
},
);
});