Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nix/npm-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
../package-lock.json
];
};
hash = "sha256-hx8lHdGcTqDBKNnzvi1aFcFN70zgjW4KaPh+tU7pNBc=";
hash = "sha256-fqD1ndVts599I+d1KI+biRtCgzoxevLCqPXkzBE1nk0=";
};

node_modules = pkgs.runCommand "node-modules" {
Expand Down
96 changes: 0 additions & 96 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"ogl": "^1.0.11",
"openapi-fetch": "0.17.0",
"packageurl-js": "^2.0.1",
"purl": "2.3.0",
"react": "19.3.1",
"react-day-picker": "10.0.2",
"react-dom": "19.3.1",
Expand Down
151 changes: 0 additions & 151 deletions src/components/common/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { javascript } from "@codemirror/legacy-modes/mode/javascript";
import { load, YAMLException } from "js-yaml";
import { keymap, placeholder as placeholderExtension } from "@codemirror/view";
import { indentWithTab } from "@codemirror/commands";
import valid from "purl/valid";
import normalize from "purl/normalize";
import { cn } from "@/lib/utils";
import { celParseLinter, celCompletionSource } from "./celLinter";

Expand All @@ -47,151 +45,6 @@ function tomlParseLinter() {
};
}

const ociReferenceRegex = new RegExp(
"^((?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))+)?(?::[0-9]+)?/)?[a-z0-9]+(?:(?:(?:[._]|__|[-]*)[a-z0-9]+)+)?(?:(?:/[a-z0-9]+(?:(?:(?:[._]|__|[-]*)[a-z0-9]+)+)?)+)?)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}))?$",
);

function purlParseLinter() {
return (view: EditorView): Diagnostic[] => {
const text = view.state.doc.toString();
const lines = text.split("\n");
const diagnostics: Diagnostic[] = [];
lines.forEach((line, index) => {
if (line.trim() === "") return;
if (line.trim().startsWith("#")) return;
const tokens = line.trim().split(/\s+/);
const pos = view.state.doc.line(index + 1).from;
if (tokens.length > 1) {
diagnostics.push({
from: pos,
to: pos + line.length,
severity: "error",
message: `Each line must contain exactly one package URL (e.g. pkg:npm/lodash@4.17.21)`,
});
return;
}
const purl = tokens[0];
const validPurl = valid(purl);
if (!validPurl) {
diagnostics.push({
from: pos,
to: pos + line.length,
severity: "error",
message: `Invalid package URL, expected format: pkg:<ecosystem>/<name>@<version> (e.g. pkg:npm/lodash@4.17.21)`,
});
return;
} else {
const normalizedPurl = normalize(purl);
if (normalizedPurl !== purl) {
diagnostics.push({
from: pos,
to: pos + line.length,
severity: "error",
message: `Package URL is not normalized, did you mean "${normalizedPurl}"?`,
});
}
}
// Check that a version is present (PURL version comes after @)
const versionMatch = purl.match(/@([^?#]+)/);
if (!versionMatch || versionMatch[1].trim() === "") {
diagnostics.push({
from: pos,
to: pos + line.length,
severity: "error",
message: `Package URL must include a version (e.g. pkg:npm/lodash@4.17.21)`,
});
}
});
return diagnostics;
};
}

function isValidDependencyProxyRule(line: string): [boolean, string | null] {
const trimmed = line.trim();
// Comments are valid
if (trimmed.startsWith("#")) return [true, null];
// Strip optional negation prefix
const stripped = trimmed.startsWith("!") ? trimmed.slice(1) : trimmed;
if (stripped.trim() === "")
return [
false,
"Empty line, use a package pattern (e.g. pkg:npm/lodash@4.17.21) or a wildcard (*)",
];

// check if wildcard is inside - everything is valid then
if (stripped.includes("*")) {
return [true, null];
}

if (stripped.startsWith("pkg:")) {
// Valid fully-qualified PURL (pkg:npm/lodash@4.17.21)
if (valid(stripped)) {
const normalizedPurl = normalize(stripped);
if (normalizedPurl !== stripped) {
return [
false,
`Package URL is not normalized, did you mean "${normalizedPurl}"?`,
];
}
const hasVersionOrWildcard =
stripped.includes("@") || stripped.includes("*");
if (!hasVersionOrWildcard) {
return [
false,
"Package rule must specify a version or wildcard (e.g. pkg:npm/lodash@4.17.21 or pkg:npm/lodash@*)",
];
}
return [true, null];
}
return [
false,
"Invalid package URL, expected format: pkg:<ecosystem>/<name>@<version>, e.g. pkg:npm/lodash@4.17.21 or pkg:npm/react*",
];
} else if (stripped.startsWith("*")) {
const segments = stripped.split("/");
for (const segment of segments) {
if (segment === "") {
return [
false,
"Invalid wildcard pattern, path segments cannot be empty (e.g. avoid trailing or double /)",
];
}
}
return [true, null];
} else if (ociReferenceRegex.test(stripped)) {
return [true, null];
}

return [
false,
"Invalid package rule, expected a package pattern (e.g. pkg:npm/lodash@4.17.21) or a wildcard (e.g. *lodash* or **lodash**) or an OCI reference (e.g. registry.example.com/repo/image:tag or registry.example.com/repo/image@sha256:abcdef...)",
];
}

function dependencyProxyRuleParseLinter() {
return (view: EditorView): Diagnostic[] => {
const text = view.state.doc.toString();
const lines = text.split("\n");
const diagnostics: Diagnostic[] = [];
lines.forEach((line, index) => {
if (line.trim() === "") return;
const [isValid, errorMessage] = isValidDependencyProxyRule(line);
if (!isValid) {
const pos = view.state.doc.line(index + 1).from;
diagnostics.push({
from: pos,
to: pos + line.length,
severity: "error",
message:
errorMessage ||
`Invalid package URL or pattern at line ${index + 1}`,
});
}
});
return diagnostics;
};
}

function yamlParseLinter() {
return (view: EditorView): Diagnostic[] => {
try {
Expand Down Expand Up @@ -238,8 +91,6 @@ const languageExtensions = {
yaml: yaml(),
json: json(),
toml: StreamLanguage.define(toml),
dependencyProxyRule: [],
purl: [],
// CEL has no dedicated grammar; JavaScript is close enough to colour strings,
// numbers, booleans, operators, property access and function calls.
cel: [
Expand All @@ -252,8 +103,6 @@ const languageLinters: Record<Language, (view: EditorView) => Diagnostic[]> = {
yaml: yamlParseLinter(),
json: jsonParseLinter(),
toml: tomlParseLinter(),
dependencyProxyRule: dependencyProxyRuleParseLinter(),
purl: purlParseLinter(),
cel: celParseLinter(),
};

Expand Down
Loading
Loading