Skip to content

Commit c0d9ca3

Browse files
authored
feat: restrict service creation by characters and length (#6585)
1 parent 4b439f4 commit c0d9ca3

File tree

6 files changed

+421
-12
lines changed

6 files changed

+421
-12
lines changed

.changeset/rotten-scissors-matter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'hive': patch
3+
'@graphql-hive/cli': patch
4+
---
5+
6+
Restrict new service names to 64 characters, alphanumberic, \_, -, and /

integration-tests/tests/cli/__snapshots__/schema.spec.ts.snap

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,85 @@ stdout--------------------------------------------:
9191
ℹ Available at http://__URL__
9292
`;
9393

94+
exports[`FEDERATION > check validates the service name > onlyNumbers 1`] = `
95+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
96+
exitCode------------------------------------------:
97+
1
98+
stderr--------------------------------------------:
99+
__NONE__
100+
stdout--------------------------------------------:
101+
✖ Detected 1 error
102+
103+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
104+
105+
106+
`;
107+
108+
exports[`FEDERATION > check validates the service name > specialCharacters 1`] = `
109+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
110+
exitCode------------------------------------------:
111+
1
112+
stderr--------------------------------------------:
113+
__NONE__
114+
stdout--------------------------------------------:
115+
✖ Detected 1 error
116+
117+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
118+
119+
120+
`;
121+
122+
exports[`FEDERATION > check validates the service name > success 1`] = `
123+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
124+
125+
stdout--------------------------------------------:
126+
✔ Schema registry is empty, nothing to compare your schema with.
127+
View full report:
128+
http://__URL__
129+
`;
130+
131+
exports[`FEDERATION > publish validates the service name > onlyNumbers 1`] = `
132+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
133+
exitCode------------------------------------------:
134+
2
135+
stderr--------------------------------------------:
136+
› Error: Schema publish failed. [300]
137+
› > See https://__URL__ for
138+
› a complete list of error codes and recommended fixes.
139+
› To disable this message set HIVE_NO_ERROR_TIP=1
140+
stdout--------------------------------------------:
141+
✖ Detected 1 error
142+
143+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
144+
145+
146+
`;
147+
148+
exports[`FEDERATION > publish validates the service name > specialCharacters 1`] = `
149+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
150+
exitCode------------------------------------------:
151+
2
152+
stderr--------------------------------------------:
153+
› Error: Schema publish failed. [300]
154+
› > See https://__URL__ for
155+
› a complete list of error codes and recommended fixes.
156+
› To disable this message set HIVE_NO_ERROR_TIP=1
157+
stdout--------------------------------------------:
158+
✖ Detected 1 error
159+
160+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
161+
162+
163+
`;
164+
165+
exports[`FEDERATION > publish validates the service name > success 1`] = `
166+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
167+
168+
stdout--------------------------------------------:
169+
✔ Published initial schema.
170+
ℹ Available at http://__URL__
171+
`;
172+
94173
exports[`FEDERATION > publishing invalid schema SDL provides meaningful feedback for the user. > schemaPublish 1`] = `
95174
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
96175
exitCode------------------------------------------:
@@ -256,6 +335,85 @@ stdout--------------------------------------------:
256335
ℹ Available at http://__URL__
257336
`;
258337

338+
exports[`SINGLE > check validates the service name > onlyNumbers 1`] = `
339+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
340+
exitCode------------------------------------------:
341+
1
342+
stderr--------------------------------------------:
343+
__NONE__
344+
stdout--------------------------------------------:
345+
✖ Detected 1 error
346+
347+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), underscore (_), or forward slash (/).
348+
349+
350+
`;
351+
352+
exports[`SINGLE > check validates the service name > specialCharacters 1`] = `
353+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
354+
exitCode------------------------------------------:
355+
1
356+
stderr--------------------------------------------:
357+
__NONE__
358+
stdout--------------------------------------------:
359+
✖ Detected 1 error
360+
361+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), underscore (_), or forward slash (/).
362+
363+
364+
`;
365+
366+
exports[`SINGLE > check validates the service name > success 1`] = `
367+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
368+
369+
stdout--------------------------------------------:
370+
✔ Schema registry is empty, nothing to compare your schema with.
371+
View full report:
372+
http://__URL__
373+
`;
374+
375+
exports[`SINGLE > publish validates the service name > onlyNumbers 1`] = `
376+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
377+
exitCode------------------------------------------:
378+
2
379+
stderr--------------------------------------------:
380+
› Error: Schema publish failed. [300]
381+
› > See https://__URL__ for
382+
› a complete list of error codes and recommended fixes.
383+
› To disable this message set HIVE_NO_ERROR_TIP=1
384+
stdout--------------------------------------------:
385+
✖ Detected 1 error
386+
387+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), underscore (_), or forward slash (/).
388+
389+
390+
`;
391+
392+
exports[`SINGLE > publish validates the service name > specialCharacters 1`] = `
393+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
394+
exitCode------------------------------------------:
395+
2
396+
stderr--------------------------------------------:
397+
› Error: Schema publish failed. [300]
398+
› > See https://__URL__ for
399+
› a complete list of error codes and recommended fixes.
400+
› To disable this message set HIVE_NO_ERROR_TIP=1
401+
stdout--------------------------------------------:
402+
✖ Detected 1 error
403+
404+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), underscore (_), or forward slash (/).
405+
406+
407+
`;
408+
409+
exports[`SINGLE > publish validates the service name > success 1`] = `
410+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
411+
412+
stdout--------------------------------------------:
413+
✔ Published initial schema.
414+
ℹ Available at http://__URL__
415+
`;
416+
259417
exports[`SINGLE > publishing invalid schema SDL provides meaningful feedback for the user. > schemaPublish 1`] = `
260418
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
261419
exitCode------------------------------------------:
@@ -442,6 +600,85 @@ stdout--------------------------------------------:
442600
ℹ Available at http://__URL__
443601
`;
444602

603+
exports[`STITCHING > check validates the service name > onlyNumbers 1`] = `
604+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
605+
exitCode------------------------------------------:
606+
1
607+
stderr--------------------------------------------:
608+
__NONE__
609+
stdout--------------------------------------------:
610+
✖ Detected 1 error
611+
612+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
613+
614+
615+
`;
616+
617+
exports[`STITCHING > check validates the service name > specialCharacters 1`] = `
618+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
619+
exitCode------------------------------------------:
620+
1
621+
stderr--------------------------------------------:
622+
__NONE__
623+
stdout--------------------------------------------:
624+
✖ Detected 1 error
625+
626+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
627+
628+
629+
`;
630+
631+
exports[`STITCHING > check validates the service name > success 1`] = `
632+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
633+
634+
stdout--------------------------------------------:
635+
✔ Schema registry is empty, nothing to compare your schema with.
636+
View full report:
637+
http://__URL__
638+
`;
639+
640+
exports[`STITCHING > publish validates the service name > onlyNumbers 1`] = `
641+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
642+
exitCode------------------------------------------:
643+
2
644+
stderr--------------------------------------------:
645+
› Error: Schema publish failed. [300]
646+
› > See https://__URL__ for
647+
› a complete list of error codes and recommended fixes.
648+
› To disable this message set HIVE_NO_ERROR_TIP=1
649+
stdout--------------------------------------------:
650+
✖ Detected 1 error
651+
652+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
653+
654+
655+
`;
656+
657+
exports[`STITCHING > publish validates the service name > specialCharacters 1`] = `
658+
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
659+
exitCode------------------------------------------:
660+
2
661+
stderr--------------------------------------------:
662+
› Error: Schema publish failed. [300]
663+
› > See https://__URL__ for
664+
› a complete list of error codes and recommended fixes.
665+
› To disable this message set HIVE_NO_ERROR_TIP=1
666+
stdout--------------------------------------------:
667+
✖ Detected 1 error
668+
669+
- Invalid service name. Service name must be 64 characters or less, must start with a letter, and can only contain alphanumeric characters, dash (-), or underscore (_).
670+
671+
672+
`;
673+
674+
exports[`STITCHING > publish validates the service name > success 1`] = `
675+
:::::::::::::::: CLI SUCCESS OUTPUT :::::::::::::::::
676+
677+
stdout--------------------------------------------:
678+
✔ Published initial schema.
679+
ℹ Available at http://__URL__
680+
`;
681+
445682
exports[`STITCHING > publishing invalid schema SDL provides meaningful feedback for the user. > schemaPublish 1`] = `
446683
:::::::::::::::: CLI FAILURE OUTPUT :::::::::::::::
447684
exitCode------------------------------------------:

integration-tests/tests/cli/schema.spec.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,113 @@ describe.each([ProjectType.Stitching, ProjectType.Federation, ProjectType.Single
9595
},
9696
);
9797

98+
test
99+
.skipIf(projectType === ProjectType.Single)
100+
.concurrent('publish validates the service name', async ({ expect }) => {
101+
const { createOrg } = await initSeed().createOwner();
102+
const { inviteAndJoinMember, createProject } = await createOrg();
103+
await inviteAndJoinMember();
104+
const { createTargetAccessToken } = await createProject(projectType);
105+
const { secret } = await createTargetAccessToken({});
106+
107+
await expect(
108+
schemaPublish([
109+
'--registry.accessToken',
110+
secret,
111+
'--author',
112+
'Kamil',
113+
'--commit',
114+
'abc123',
115+
'--service',
116+
'900',
117+
...serviceUrlArgs,
118+
'fixtures/init-schema.graphql',
119+
]),
120+
).rejects.toMatchSnapshot('onlyNumbers');
121+
122+
await expect(
123+
schemaPublish([
124+
'--registry.accessToken',
125+
secret,
126+
'--author',
127+
'Kamil',
128+
'--commit',
129+
'abc123',
130+
'--service',
131+
'asdf$#%^#@!#',
132+
...serviceUrlArgs,
133+
'fixtures/init-schema.graphql',
134+
]),
135+
).rejects.toMatchSnapshot('specialCharacters');
136+
137+
await expect(
138+
schemaPublish([
139+
'--registry.accessToken',
140+
secret,
141+
'--author',
142+
'Kamil',
143+
'--commit',
144+
'abc123',
145+
'--service',
146+
'valid-name0',
147+
...serviceUrlArgs,
148+
'fixtures/init-schema.graphql',
149+
]),
150+
).resolves.toMatchSnapshot('success');
151+
});
152+
153+
test
154+
.skipIf(projectType === ProjectType.Single)
155+
.concurrent('check validates the service name', async ({ expect }) => {
156+
const { createOrg } = await initSeed().createOwner();
157+
const { inviteAndJoinMember, createProject } = await createOrg();
158+
await inviteAndJoinMember();
159+
const { createTargetAccessToken } = await createProject(projectType);
160+
const { secret } = await createTargetAccessToken({});
161+
162+
await expect(
163+
schemaCheck([
164+
'--registry.accessToken',
165+
secret,
166+
'--author',
167+
'Kamil',
168+
'--commit',
169+
'abc123',
170+
'--service',
171+
'900',
172+
'fixtures/init-schema.graphql',
173+
]),
174+
).rejects.toMatchSnapshot('onlyNumbers');
175+
176+
await expect(
177+
schemaCheck([
178+
'--registry.accessToken',
179+
secret,
180+
'--author',
181+
'Kamil',
182+
'--commit',
183+
'abc123',
184+
'--service',
185+
'asdf$#%^#@!#',
186+
'fixtures/init-schema.graphql',
187+
]),
188+
).rejects.toMatchSnapshot('specialCharacters');
189+
190+
await expect(
191+
schemaCheck([
192+
'--registry.accessToken',
193+
secret,
194+
'--author',
195+
'Kamil',
196+
'--commit',
197+
'abc123',
198+
'--service',
199+
'valid-name0',
200+
'fixtures/init-schema.graphql',
201+
]),
202+
).resolves.toMatchSnapshot('success');
203+
});
204+
98205
test.concurrent(
99206
'publishing invalid schema SDL provides meaningful feedback for the user.',
100207
async ({ expect }) => {

packages/services/api/src/modules/schema/providers/schema-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function ensureSingleSchema(schema: Schema | Schema[]): SingleSchema {
3737
throw new Error('Expected a single schema');
3838
}
3939

40-
export function ensureCompositeSchemas(schemas: readonly Schema[]): CompositeSchema[] | never {
40+
export function ensureCompositeSchemas(schemas: readonly Schema[]): CompositeSchema[] {
4141
return schemas.filter(isCompositeSchema);
4242
}
4343

0 commit comments

Comments
 (0)