validate plugin and module ids

Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2025-07-09 21:33:23 +02:00
parent 4ad63b8d9f
commit 5766fc71c6
9 changed files with 109 additions and 10 deletions
+8
View File
@@ -0,0 +1,8 @@
---
'@backstage/backend-plugin-api': minor
'@backstage/backend-app-api': minor
'@backstage/config-loader': patch
'@backstage/config': patch
---
The backend will now throw an error if a plugin or a module doesn't have a valid ID
@@ -11,10 +11,10 @@ As a rule, all names should be camel case, with the exceptions of plugin and mod
### Plugins
| Description | Pattern | Examples |
| ----------- | ----------------- | ------------------------------------- |
| export | `<camelId>Plugin` | `catalogPlugin`, `userSettingsPlugin` |
| ID | `'<kebab-id>'` | `'catalog'`, `'user-settings'` |
| Description | Pattern | Examples | Notes |
| ----------- | ----------------- | ------------------------------------- | --------------------------------------------------------------------- |
| export | `<camelId>Plugin` | `catalogPlugin`, `userSettingsPlugin` | |
| ID | `'<kebab-id>'` | `'catalog'`, `'user-settings'` | letters, digits, dashes, and underscores only, starting with a letter |
Example:
@@ -27,10 +27,10 @@ export const userSettingsPlugin = createBackendPlugin({
### Modules
| Description | Pattern | Examples |
| ----------- | ---------------------------- | ----------------------------------- |
| export | `<pluginId>Module<ModuleId>` | `catalogModuleGithubEntityProvider` |
| ID | `'<module-id>'` | `'github-entity-provider'` |
| Description | Pattern | Examples | Notes |
| ----------- | ---------------------------- | ----------------------------------- | --------------------------------------------------------------------- |
| export | `<pluginId>Module<ModuleId>` | `catalogModuleGithubEntityProvider` | |
| ID | `'<module-id>'` | `'github-entity-provider'` | letters, digits, dashes, and underscores only, starting with a letter |
Example:
@@ -1011,6 +1011,53 @@ describe('BackendInitializer', () => {
"Service or extension point dependencies of module 'test-mod' for plugin 'test' are missing for the following ref(s): serviceRef{a}",
);
});
it('should reject plugins with invalid pluginId', async () => {
const init = new BackendInitializer(baseFactories);
init.add(
createBackendPlugin({
pluginId: 'test:invalid&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
await expect(init.start()).rejects.toThrow(
"Invalid pluginId 'test:invalid&id', must match the pattern /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i (letters, digits, dashes, and underscores only, starting with a letter)",
);
});
it('should reject modules with invalid moduleId', async () => {
const init = new BackendInitializer(baseFactories);
init.add(
createBackendPlugin({
pluginId: 'test',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
init.add(
createBackendModule({
pluginId: 'test',
moduleId: 'invalid:module&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
await expect(init.start()).rejects.toThrow(
"Invalid moduleId 'invalid:module&id' for plugin 'test', must match the pattern /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i (letters, digits, dashes, and underscores only, starting with a letter)",
);
});
it('should properly load double-default CJS modules', async () => {
expect.assertions(3);
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { CONFIG_KEY_PART_PATTERN } from '@backstage/config';
import { BackendFeature } from '../types';
import {
BackendModuleRegistrationPoints,
@@ -55,6 +56,12 @@ export function createBackendModule(
options: CreateBackendModuleOptions,
): BackendFeature {
function getRegistrations() {
if (!CONFIG_KEY_PART_PATTERN.test(options.moduleId)) {
throw new Error(
`Invalid moduleId '${options.moduleId}' for plugin '${options.pluginId}', must match the pattern ${CONFIG_KEY_PART_PATTERN} (letters, digits, dashes, and underscores only, starting with a letter)`,
);
}
const extensionPoints: InternalBackendPluginRegistration['extensionPoints'] =
[];
let init: InternalBackendModuleRegistration['init'] | undefined = undefined;
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { CONFIG_KEY_PART_PATTERN } from '@backstage/config';
import { BackendFeature } from '../types';
import {
BackendPluginRegistrationPoints,
@@ -49,6 +50,12 @@ export function createBackendPlugin(
options: CreateBackendPluginOptions,
): BackendFeature {
function getRegistrations() {
if (!CONFIG_KEY_PART_PATTERN.test(options.pluginId)) {
throw new Error(
`Invalid pluginId '${options.pluginId}', must match the pattern ${CONFIG_KEY_PART_PATTERN} (letters, digits, dashes, and underscores only, starting with a letter)`,
);
}
const extensionPoints: InternalBackendPluginRegistration['extensionPoints'] =
[];
let init: InternalBackendPluginRegistration['init'] | undefined = undefined;
+3
View File
@@ -43,6 +43,9 @@ export type Config = {
getOptionalStringArray(key: string): string[] | undefined;
};
// @public
export const CONFIG_KEY_PART_PATTERN: RegExp;
// @public
export class ConfigReader implements Config {
constructor(
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The pattern that config keys must match.
*
* @remarks
* keys must only contain the letters `a` through `z` and digits, in groups separated by
* dashes or underscores. Additionally, the very first character of each such group
* must be a letter, not a digit
*
* @public
*/
export const CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z0-9]+)*$/i;
+1
View File
@@ -29,3 +29,4 @@ export type {
export { readDurationFromConfig } from './readDurationFromConfig';
export { ConfigReader } from './reader';
export type { AppConfig, Config } from './types';
export { CONFIG_KEY_PART_PATTERN } from './constants';
+1 -2
View File
@@ -17,8 +17,7 @@
import { JsonObject, JsonValue } from '@backstage/types';
import { AppConfig, Config } from './types';
// Update the same pattern in config-loader package if this is changed
const CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_:][a-z0-9]+)*$/i;
import { CONFIG_KEY_PART_PATTERN } from './constants';
function isObject(value: JsonValue | undefined): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);