Skip to content

Commit 4093f70

Browse files
authored
Disallow new lines in paths when checking with isValidPath (#6055)
* test * disallow new lines * changeset * typo
1 parent fe6ae37 commit 4093f70

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.changeset/spotty-kiwis-crash.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@graphql-tools/utils": patch
3+
---
4+
5+
Disallow new lines in paths when checking with `isValidPath`
6+
7+
A string may sometimes look like a path but is not (like an SDL of a simple
8+
GraphQL schema). To make sure we don't yield false-positives in such cases,
9+
we disallow new lines in paths (even though most Unix systems support new
10+
lines in file names).

packages/utils/src/helpers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ export function isDocumentString(str: any): boolean {
2525
return false;
2626
}
2727

28-
const invalidPathRegex = /[!%^<>`]/;
28+
const invalidPathRegex = /[!%^<>`\n]/;
29+
/**
30+
* Checkes whether the `str` contains any path illegal characters.
31+
*
32+
* A string may sometimes look like a path but is not (like an SDL of a simple
33+
* GraphQL schema). To make sure we don't yield false-positives in such cases,
34+
* we disallow new lines in paths (even though most Unix systems support new
35+
* lines in file names).
36+
*/
2937
export function isValidPath(str: any): boolean {
3038
return typeof str === 'string' && !invalidPathRegex.test(str);
3139
}

packages/utils/tests/helpers.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isValidPath } from '../src/helpers';
2+
3+
describe('helpers', () => {
4+
it.each([
5+
`schema @transport(subgraph: "API", kind: "rest", location: "http://0.0.0.0:4001", headers: "{\"Content-Type\":\"application/json\"}") {
6+
query: Query
7+
mutation: Mutation
8+
subscription: Subscription
9+
}`,
10+
])('should detect "%s" as NOT a valid path', str => {
11+
expect(isValidPath(str)).toBeFalsy();
12+
});
13+
14+
it.each(['file', 'file.tsx', 'some/where/file.tsx', '/some/where/file.tsx'])(
15+
'should detect "%s" as a valid path',
16+
str => {
17+
expect(isValidPath(str)).toBeTruthy();
18+
},
19+
);
20+
});

0 commit comments

Comments
 (0)