Merge pull request #30092 from Sarabadu/module-id-validation

validate plugin and module ids
This commit is contained in:
Patrik Oldsberg
2026-02-02 20:13:35 +01:00
committed by GitHub
12 changed files with 158 additions and 9 deletions
@@ -0,0 +1,30 @@
/*
* 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.
*/
// NOTE: changing any of these constants need to be reflected in
// @backstage/backend-plugin-api/src/wiring/constants.ts as well
/**
* The pattern that IDs must match.
*
* @remarks
* ids must only contain the letters `a` through `z` and digits, in groups separated by
* dashes. Additionally, the very first character of the first group
* must be a letter, not a digit
*
* @public
*/
export const ID_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/i;
@@ -142,6 +142,17 @@ describe('createFrontendPlugin', () => {
expect(String(plugin)).toBe('Plugin{id=test}');
});
it('should warn about invalid plugin IDs', () => {
const consoleWarn = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});
createFrontendPlugin({ pluginId: 'invalid&id' });
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringContaining("The pluginId 'invalid&id' will be invalid soon"),
);
consoleWarn.mockRestore();
});
it('should create a plugin with extension instances', async () => {
const plugin = createFrontendPlugin({
pluginId: 'test',
@@ -30,6 +30,7 @@ import { FeatureFlagConfig } from './types';
import { MakeSortedExtensionsMap } from './MakeSortedExtensionsMap';
import { JsonObject } from '@backstage/types';
import { RouteRef, SubRouteRef, ExternalRouteRef } from '../routing';
import { ID_PATTERN } from './constants';
/**
* Information about the plugin.
@@ -208,6 +209,13 @@ export function createFrontendPlugin<
> {
const pluginId = options.pluginId;
if (!ID_PATTERN.test(pluginId)) {
// eslint-disable-next-line no-console
console.warn(
`WARNING: The pluginId '${pluginId}' will be invalid soon, please change it to match the pattern ${ID_PATTERN} (letters, digits, and dashes only, starting with a letter)`,
);
}
const extensions = new Array<Extension<any>>();
const extensionDefinitionsById = new Map<
string,