mirror of
https://github.com/github/awesome-copilot.git
synced 2026-08-02 07:22:32 +00:00
be7a1cf734
* Harden external plugin validation: semver, SPDX, email, unknown fields Extend the canonical external-plugin validator with Open-Plugins-aligned rules, reusing the shared validation functions rather than duplicating checks: - version: enforce Semantic Versioning (allows prerelease/build metadata) - license: validate SPDX identifiers/expressions; warn (not error) on well-formed-but-unrecognized ids so existing entries like SSAL-1.0 pass - author.email: validate format when present - unknown-field detection: warn on typo'd top-level/author/source keys - immutable locator: marketplace warns when source lacks ref/sha; publicSubmission keeps the existing hard error Add eng/external-plugin-validation.test.mjs (node:test) covering each rule plus a regression that committed external.json passes marketplace policy with zero errors. Update CONTRIBUTING.md accordingly. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Make license non-SPDX a warning and share license check with local plugins The agent-plugins-spec schema does not enforce SPDX, and plugins may use proprietary/non-OSS licenses. Relax license validation so any non-empty license string that isn't a recognized SPDX identifier/expression produces a warning rather than an error. Extract the license check into a reusable validateLicenseField() and apply it to both external plugins and local plugin.json manifests via eng/validate-plugins.mjs, so licenses are validated consistently in one place. Update tests and CONTRIBUTING.md accordingly. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Extract shared license validation into eng/lib/license.mjs Move the SPDX constants and license validation (validateLicenseField, isRecognizedSpdxExpression) out of external-plugin-validation.mjs into a dedicated eng/lib/license.mjs module. Both the external plugin catalog validator and the local plugin.json validator now import license logic from this neutral shared module instead of one validator importing from the other. Behavior is unchanged; tests import from the new home. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Fix SPDX license parsing correctness and warning-message injection Rewrite isRecognizedSpdxExpression as a recursive-descent SPDX license expression parser to address PR review feedback: - Validate LicenseRef-/DocumentRef- grammar (non-empty idstring; DocumentRef requires the ":LicenseRef-<id>" suffix) instead of accepting any token with that prefix. - Parse parentheses with balance and placement checks rather than stripping them, so malformed groupings like "(MIT", "MIT)", and "MIT OR (Apache-2.0))" are no longer silently treated as valid. - Treat WITH as a distinct operator whose right operand must be a known SPDX license exception, so "MIT WITH Apache-2.0" and "GPL-2.0-only WITH MIT" are rejected while "GPL-2.0-only WITH Classpath-exception-2.0" is recognized. - Sanitize the untrusted license value in the non-SPDX warning (collapse whitespace, truncate, escape backticks, wrap as inline code) since the warning is rendered verbatim into a Markdown bot comment during intake. Malformed SPDX stays a warning (never an error) so proprietary and non-OSS license strings remain allowed. Adds targeted grammar and sanitization tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Complete backslash escaping and run source unknown-field check for all sources Address two PR review findings: - eng/lib/license.mjs: sanitizeForMessage now escapes backslashes before backticks so the inline-code warning value is fully escaped, resolving a CodeQL "incomplete string escaping" alert. A raw backslash in a license value is now doubled rather than left to combine with a following escaped backtick. - eng/external-plugin-validation.mjs: move the source unknown-field check out of validateGitHubSource into the outer source-object branch so it runs for every object source and then dispatch by source.source. A typo in the source discriminator (e.g. "soruce") or an unsupported source type now still surfaces the unknown-field warning instead of being silently skipped. Adds regression tests for both behaviors. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Harden Markdown safety of validation warnings via inlineCode helper External-plugin validation warnings are rendered verbatim as Markdown list items in the intake bot comment, so untrusted values interpolated into them are an injection vector. Add eng/lib/markdown.mjs exporting inlineCode(), which wraps a value in a code span fenced with more backticks than any run in the content (and pads leading/trailing backticks). This cannot be broken out of, unlike the previous backslash escaping, which does not work inside code spans. - lib/license.mjs: replace sanitizeForMessage with inlineCode for the non-SPDX license warning. - external-plugin-validation.mjs: wrap unknown-field keys (attacker controlled JSON object keys) with inlineCode so newlines/Markdown in a key can no longer inject a new list item. - Add inlineCode unit tests and injection-neutralization tests for both the license value and unknown-field key paths. Addresses PR review comments 3679379606 and 3679434134. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728 * Fixing codespell --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eaa5eed6-5b65-4b28-9904-24f380d26728
262 lines
6.6 KiB
JavaScript
262 lines
6.6 KiB
JavaScript
// Shared license validation used by both the external plugin catalog validator
|
|
// (eng/external-plugin-validation.mjs) and the local plugin.json validator
|
|
// (eng/validate-plugins.mjs). Keeping it here avoids one validator importing from
|
|
// the other and gives license rules a single, dedicated home.
|
|
//
|
|
// The agent-plugins-spec schema (https://github.com/agentplugins/agent-plugins-spec)
|
|
// does not enforce SPDX, and plugins may legitimately use proprietary or non-OSS
|
|
// licenses. A non-SPDX license is therefore a warning, never an error.
|
|
|
|
import { inlineCode } from "./markdown.mjs";
|
|
|
|
// A single SPDX license identifier token (e.g. "MIT", "Apache-2.0", "LicenseRef-Foo").
|
|
const SPDX_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9.-]*\+?$/;
|
|
const SPDX_IDSTRING_PATTERN = /^[A-Za-z0-9.-]+$/;
|
|
|
|
// Curated set of common SPDX license identifiers. Not exhaustive: unrecognized
|
|
// but well-formed identifiers produce a warning rather than an error.
|
|
const KNOWN_SPDX_IDS = new Set([
|
|
"0BSD",
|
|
"AGPL-3.0",
|
|
"AGPL-3.0-only",
|
|
"AGPL-3.0-or-later",
|
|
"Apache-2.0",
|
|
"BSD-2-Clause",
|
|
"BSD-3-Clause",
|
|
"BSL-1.0",
|
|
"CC-BY-4.0",
|
|
"CC-BY-SA-4.0",
|
|
"CC0-1.0",
|
|
"EPL-2.0",
|
|
"GPL-2.0",
|
|
"GPL-2.0-only",
|
|
"GPL-2.0-or-later",
|
|
"GPL-3.0",
|
|
"GPL-3.0-only",
|
|
"GPL-3.0-or-later",
|
|
"ISC",
|
|
"LGPL-2.1",
|
|
"LGPL-2.1-only",
|
|
"LGPL-2.1-or-later",
|
|
"LGPL-3.0",
|
|
"LGPL-3.0-only",
|
|
"LGPL-3.0-or-later",
|
|
"MIT",
|
|
"MPL-2.0",
|
|
"Unlicense",
|
|
"WTFPL",
|
|
"Zlib",
|
|
]);
|
|
|
|
const KNOWN_SPDX_EXCEPTIONS = new Set([
|
|
"Classpath-exception-2.0",
|
|
"GCC-exception-3.1",
|
|
"LLVM-exception",
|
|
"Autoconf-exception-3.0",
|
|
"Bison-exception-2.2",
|
|
"Font-exception-2.0",
|
|
"GPL-3.0-linking-exception",
|
|
"Linux-syscall-note",
|
|
"OpenSSL-exception",
|
|
"Qt-GPL-exception-1.0",
|
|
]);
|
|
|
|
function isNonEmptyString(value) {
|
|
return typeof value === "string" && value.trim().length > 0;
|
|
}
|
|
|
|
function isSpdxIdString(value) {
|
|
return SPDX_IDSTRING_PATTERN.test(value);
|
|
}
|
|
|
|
function isRecognizedLicenseRef(token) {
|
|
if (token.startsWith("LicenseRef-")) {
|
|
return isSpdxIdString(token.slice("LicenseRef-".length));
|
|
}
|
|
|
|
if (!token.startsWith("DocumentRef-")) {
|
|
return false;
|
|
}
|
|
|
|
const separatorIndex = token.indexOf(":LicenseRef-");
|
|
if (separatorIndex === -1) {
|
|
return false;
|
|
}
|
|
|
|
const documentRef = token.slice("DocumentRef-".length, separatorIndex);
|
|
const licenseRef = token.slice(separatorIndex + ":LicenseRef-".length);
|
|
return isSpdxIdString(documentRef) && isSpdxIdString(licenseRef);
|
|
}
|
|
|
|
function isRecognizedSpdxIdToken(token) {
|
|
if (isRecognizedLicenseRef(token)) {
|
|
return true;
|
|
}
|
|
|
|
if (!SPDX_ID_PATTERN.test(token)) {
|
|
return false;
|
|
}
|
|
|
|
const normalized = token.endsWith("+") ? token.slice(0, -1) : token;
|
|
return KNOWN_SPDX_IDS.has(normalized);
|
|
}
|
|
|
|
function isRecognizedSpdxExceptionToken(token) {
|
|
return isSpdxIdString(token) && KNOWN_SPDX_EXCEPTIONS.has(token);
|
|
}
|
|
|
|
function tokenizeSpdxExpression(license) {
|
|
return license
|
|
.replace(/([()])/g, " $1 ")
|
|
.trim()
|
|
.split(/\s+/)
|
|
.filter((token) => token.length > 0);
|
|
}
|
|
|
|
// Returns true only for a well-formed SPDX license expression composed of
|
|
// recognized identifiers joined by SPDX operators (optionally parenthesized).
|
|
// Anything else (proprietary strings, free text, unrecognized ids) returns false so
|
|
// the caller can warn without rejecting it — the plugin spec does not enforce SPDX.
|
|
export function isRecognizedSpdxExpression(license) {
|
|
if (typeof license !== "string") {
|
|
return false;
|
|
}
|
|
|
|
const tokens = tokenizeSpdxExpression(license);
|
|
|
|
if (tokens.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
let position = 0;
|
|
|
|
function peek() {
|
|
return tokens[position];
|
|
}
|
|
|
|
function consume() {
|
|
return tokens[position++];
|
|
}
|
|
|
|
function parsePrimary() {
|
|
const token = peek();
|
|
|
|
if (token === "(") {
|
|
consume();
|
|
if (!parseOrExpression()) {
|
|
return false;
|
|
}
|
|
if (peek() !== ")") {
|
|
return false;
|
|
}
|
|
consume();
|
|
return true;
|
|
}
|
|
|
|
if (!token || token === ")" || ["AND", "OR", "WITH"].includes(token.toUpperCase())) {
|
|
return false;
|
|
}
|
|
|
|
consume();
|
|
return isRecognizedSpdxIdToken(token);
|
|
}
|
|
|
|
function parseSimpleExpression() {
|
|
const token = peek();
|
|
if (!token || token === ")" || ["AND", "OR", "WITH"].includes(token.toUpperCase())) {
|
|
return false;
|
|
}
|
|
|
|
consume();
|
|
return isRecognizedSpdxIdToken(token);
|
|
}
|
|
|
|
function parseWithExpression() {
|
|
if (peek() === "(") {
|
|
return parsePrimary() && peek()?.toUpperCase() !== "WITH";
|
|
}
|
|
|
|
if (!parseSimpleExpression()) {
|
|
return false;
|
|
}
|
|
|
|
if (peek()?.toUpperCase() !== "WITH") {
|
|
return true;
|
|
}
|
|
|
|
consume();
|
|
const exceptionToken = peek();
|
|
if (!exceptionToken || exceptionToken === ")" || ["AND", "OR", "WITH"].includes(exceptionToken.toUpperCase())) {
|
|
return false;
|
|
}
|
|
consume();
|
|
return isRecognizedSpdxExceptionToken(exceptionToken);
|
|
}
|
|
|
|
function parseAndExpression() {
|
|
if (!parseWithExpression()) {
|
|
return false;
|
|
}
|
|
|
|
while (peek()?.toUpperCase() === "AND") {
|
|
consume();
|
|
if (!parseWithExpression()) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function parseOrExpression() {
|
|
if (!parseAndExpression()) {
|
|
return false;
|
|
}
|
|
|
|
while (peek()?.toUpperCase() === "OR") {
|
|
consume();
|
|
if (!parseAndExpression()) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return parseOrExpression() && position === tokens.length;
|
|
}
|
|
|
|
// Canonical license validation. A non-SPDX license is a warning (never an error)
|
|
// so authors may use proprietary or non-OSS licenses. Returns collected
|
|
// errors/warnings so each caller can integrate them into its own reporting.
|
|
//
|
|
// Options:
|
|
// required — when true, a missing license is an error (default false).
|
|
// prefix — optional message prefix (e.g. "external.json[3]") for context.
|
|
export function validateLicenseField(license, options = {}) {
|
|
const { required = false } = options;
|
|
const prefix = options.prefix ? `${options.prefix}: ` : "";
|
|
const errors = [];
|
|
const warnings = [];
|
|
|
|
if (license === undefined) {
|
|
if (required) {
|
|
errors.push(`${prefix}"license" is required`);
|
|
}
|
|
return { errors, warnings };
|
|
}
|
|
|
|
if (!isNonEmptyString(license)) {
|
|
errors.push(`${prefix}"license" must be a non-empty string`);
|
|
return { errors, warnings };
|
|
}
|
|
|
|
if (!isRecognizedSpdxExpression(license)) {
|
|
warnings.push(
|
|
`${prefix}"license" value ${inlineCode(license)} is not a recognized SPDX identifier; prefer a standard SPDX id (https://spdx.org/licenses), though non-SPDX or proprietary licenses are allowed`
|
|
);
|
|
}
|
|
|
|
return { errors, warnings };
|
|
}
|