@shieldaai/security-watch 0.0.0-stage → 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CONTRIBUTING.md +90 -0
  2. package/LICENSE +202 -0
  3. package/README.md +269 -2
  4. package/SECURITY.md +47 -0
  5. package/dist/assessment.d.ts +5 -0
  6. package/dist/assessment.js +398 -0
  7. package/dist/cli.d.ts +2 -0
  8. package/dist/cli.js +792 -0
  9. package/dist/config.d.ts +242 -0
  10. package/dist/config.js +298 -0
  11. package/dist/cve-now.d.ts +5 -0
  12. package/dist/cve-now.js +413 -0
  13. package/dist/delivery.d.ts +26 -0
  14. package/dist/delivery.js +394 -0
  15. package/dist/ecosystems.d.ts +25 -0
  16. package/dist/ecosystems.js +254 -0
  17. package/dist/engine.d.ts +26 -0
  18. package/dist/engine.js +307 -0
  19. package/dist/health.d.ts +17 -0
  20. package/dist/health.js +70 -0
  21. package/dist/heartbeat.d.ts +13 -0
  22. package/dist/heartbeat.js +74 -0
  23. package/dist/http.d.ts +8 -0
  24. package/dist/http.js +251 -0
  25. package/dist/index.d.ts +18 -0
  26. package/dist/index.js +15 -0
  27. package/dist/inventory.d.ts +3 -0
  28. package/dist/inventory.js +143 -0
  29. package/dist/lock.d.ts +3 -0
  30. package/dist/lock.js +112 -0
  31. package/dist/maintenance.d.ts +43 -0
  32. package/dist/maintenance.js +227 -0
  33. package/dist/models.d.ts +20 -0
  34. package/dist/models.js +602 -0
  35. package/dist/operations.d.ts +17 -0
  36. package/dist/operations.js +117 -0
  37. package/dist/providers.d.ts +209 -0
  38. package/dist/providers.js +459 -0
  39. package/dist/setup.d.ts +113 -0
  40. package/dist/setup.js +100 -0
  41. package/dist/source-error.d.ts +22 -0
  42. package/dist/source-error.js +56 -0
  43. package/dist/sources.d.ts +3 -0
  44. package/dist/sources.js +1071 -0
  45. package/dist/store.d.ts +71 -0
  46. package/dist/store.js +651 -0
  47. package/dist/types.d.ts +247 -0
  48. package/dist/types.js +1 -0
  49. package/dist/util.d.ts +6 -0
  50. package/dist/util.js +14 -0
  51. package/dist/vendor-sources.d.ts +24 -0
  52. package/dist/vendor-sources.js +56 -0
  53. package/docs/configuration.md +406 -0
  54. package/docs/quality-review.md +104 -0
  55. package/examples/advisories.json +17 -0
  56. package/examples/common-systems.yaml +243 -0
  57. package/examples/libraries.example.txt +16 -0
  58. package/examples/libraries.example.yaml +38 -0
  59. package/examples/library-watch.example.yaml +27 -0
  60. package/examples/security-watch.example.yaml +86 -0
  61. package/examples/systems.example.yaml +31 -0
  62. package/package.json +69 -4
@@ -0,0 +1,117 @@
1
+ import { linkSync, lstatSync, mkdirSync, mkdtempSync, opendirSync, rmSync, unlinkSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { randomUUID } from 'node:crypto';
4
+ import { verifyBackup } from './maintenance.js';
5
+ function backupFiles(directory) {
6
+ const result = [];
7
+ const folder = opendirSync(directory);
8
+ try {
9
+ let count = 0;
10
+ for (let entry = folder.readSync(); entry; entry = folder.readSync()) {
11
+ if (++count > 10_000)
12
+ throw new Error('Backup directory contains too many entries');
13
+ if (!/^security-watch-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z-[a-f0-9-]{36}\.db$/.test(entry.name))
14
+ continue;
15
+ const stat = lstatSync(join(directory, entry.name));
16
+ if (!stat.isFile() || stat.nlink !== 1 || (process.platform !== 'win32' && stat.mode & 0o077))
17
+ throw new Error('Backup files must be private regular files without links');
18
+ result.push(entry.name);
19
+ }
20
+ }
21
+ finally {
22
+ folder.closeSync();
23
+ }
24
+ return result.sort().reverse();
25
+ }
26
+ function savedRun(store) {
27
+ const raw = store.getMeta('maintenance:last_run');
28
+ try {
29
+ const parsed = JSON.parse(raw ?? 'null');
30
+ if (parsed &&
31
+ typeof parsed === 'object' &&
32
+ 'ok' in parsed &&
33
+ typeof parsed.ok === 'boolean' &&
34
+ 'attempted_at' in parsed &&
35
+ typeof parsed.attempted_at === 'string' &&
36
+ Number.isFinite(Date.parse(parsed.attempted_at)))
37
+ return parsed;
38
+ }
39
+ catch {
40
+ // Invalid history does not postpone maintenance indefinitely.
41
+ }
42
+ return undefined;
43
+ }
44
+ /** Opt-in maintenance. Backups are verified before rotation or retention removes anything. */
45
+ export class MaintenanceScheduler {
46
+ nextAt = 0;
47
+ async tick(store, config, now = Date.now()) {
48
+ const settings = config.maintenance;
49
+ if (!settings)
50
+ return undefined;
51
+ const previous = savedRun(store);
52
+ if (!this.nextAt && previous) {
53
+ const attempted = Date.parse(previous.attempted_at);
54
+ if (attempted <= now)
55
+ this.nextAt =
56
+ attempted +
57
+ (previous.ok ? settings.interval_hours : Math.min(settings.interval_hours, 1)) * 3_600_000;
58
+ }
59
+ if (now < this.nextAt)
60
+ return undefined;
61
+ this.nextAt = now + Math.min(settings.interval_hours, 1) * 3_600_000;
62
+ const report = {
63
+ ok: false,
64
+ attempted_at: new Date(now).toISOString(),
65
+ last_success_at: typeof previous?.last_success_at === 'string' ? previous.last_success_at : null,
66
+ backup_created: false,
67
+ backups_removed: 0,
68
+ rows_removed: 0,
69
+ more_available: false,
70
+ error: null,
71
+ };
72
+ try {
73
+ if (settings.backup) {
74
+ const { directory, max_bytes } = settings.backup;
75
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
76
+ const folder = lstatSync(directory);
77
+ if (!folder.isDirectory() ||
78
+ folder.isSymbolicLink() ||
79
+ (process.platform !== 'win32' && folder.mode & 0o077))
80
+ throw new Error('Backup directory must be private');
81
+ backupFiles(directory);
82
+ const filename = `security-watch-${report.attempted_at.replace(/[:.]/g, '-')}-${randomUUID()}.db`;
83
+ const path = join(directory, filename);
84
+ const temporary = mkdtempSync(join(directory, '.verify-backup-'));
85
+ try {
86
+ const candidate = join(temporary, 'snapshot.db');
87
+ await store.backup(candidate);
88
+ await verifyBackup(candidate, { max_bytes });
89
+ linkSync(candidate, path);
90
+ }
91
+ finally {
92
+ rmSync(temporary, { recursive: true, force: true });
93
+ }
94
+ report.backup_created = true;
95
+ for (const old of backupFiles(directory)
96
+ .filter((name) => name !== filename)
97
+ .slice(settings.backup.keep - 1)) {
98
+ unlinkSync(join(directory, old));
99
+ report.backups_removed++;
100
+ }
101
+ }
102
+ if (settings.retention) {
103
+ const pruning = store.prune(settings.retention, now, true);
104
+ report.rows_removed = pruning.total;
105
+ report.more_available = pruning.more_available;
106
+ }
107
+ report.ok = true;
108
+ report.last_success_at = report.attempted_at;
109
+ this.nextAt = now + settings.interval_hours * 3_600_000;
110
+ }
111
+ catch {
112
+ report.error = 'Maintenance failed. Check storage space, backup permissions and retention settings.';
113
+ }
114
+ store.setMeta('maintenance:last_run', JSON.stringify(report));
115
+ return report;
116
+ }
117
+ }
@@ -0,0 +1,209 @@
1
+ import type { ModelSpec } from './types.js';
2
+ export type OutputFormat = 'auto' | 'json_schema' | 'json_object' | 'prompt';
3
+ export type ProviderProtocol = 'openai-responses' | 'openai-chat' | 'anthropic' | 'gemini' | 'cohere' | 'perplexity';
4
+ export interface ProviderDefinition {
5
+ id: string;
6
+ name: string;
7
+ protocol: ProviderProtocol;
8
+ endpoint?: string;
9
+ apiKeyEnv: string;
10
+ defaultFormat: Exclude<OutputFormat, 'auto'>;
11
+ documentationUrl: string;
12
+ local?: boolean;
13
+ tokenParameter?: 'max_tokens' | 'max_completion_tokens';
14
+ formats?: readonly Exclude<OutputFormat, 'auto'>[];
15
+ }
16
+ declare const catalog: readonly [{
17
+ readonly id: 'openai';
18
+ readonly name: 'OpenAI';
19
+ readonly protocol: 'openai-responses';
20
+ readonly endpoint: 'https://api.openai.com/v1/responses';
21
+ readonly apiKeyEnv: 'OPENAI_API_KEY';
22
+ readonly defaultFormat: 'json_schema';
23
+ readonly documentationUrl: 'https://developers.openai.com/api/reference/resources/responses/methods/create';
24
+ }, {
25
+ readonly id: 'azure-openai';
26
+ readonly name: 'Azure OpenAI';
27
+ readonly protocol: 'openai-responses';
28
+ readonly apiKeyEnv: 'AZURE_OPENAI_API_KEY';
29
+ readonly defaultFormat: 'json_schema';
30
+ readonly documentationUrl: 'https://learn.microsoft.com/en-us/rest/api/aifoundry/azureopenai/responses';
31
+ }, {
32
+ readonly id: 'anthropic';
33
+ readonly name: 'Anthropic';
34
+ readonly protocol: 'anthropic';
35
+ readonly endpoint: 'https://api.anthropic.com/v1/messages';
36
+ readonly apiKeyEnv: 'ANTHROPIC_API_KEY';
37
+ readonly defaultFormat: 'json_schema';
38
+ readonly formats: readonly ["json_schema", "prompt"];
39
+ readonly documentationUrl: 'https://platform.claude.com/docs/en/build-with-claude/structured-outputs';
40
+ }, {
41
+ readonly id: 'gemini';
42
+ readonly name: 'Google Gemini';
43
+ readonly protocol: 'gemini';
44
+ readonly endpoint: 'https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent';
45
+ readonly apiKeyEnv: 'GEMINI_API_KEY';
46
+ readonly defaultFormat: 'json_schema';
47
+ readonly documentationUrl: 'https://ai.google.dev/api/generate-content';
48
+ }, {
49
+ readonly id: 'cohere';
50
+ readonly name: 'Cohere';
51
+ readonly protocol: 'cohere';
52
+ readonly endpoint: 'https://api.cohere.com/v2/chat';
53
+ readonly apiKeyEnv: 'COHERE_API_KEY';
54
+ readonly defaultFormat: 'json_schema';
55
+ readonly documentationUrl: 'https://docs.cohere.com/v2/reference/chat';
56
+ }, {
57
+ readonly id: 'groq';
58
+ readonly name: 'Groq';
59
+ readonly protocol: 'openai-chat';
60
+ readonly endpoint: 'https://api.groq.com/openai/v1/chat/completions';
61
+ readonly apiKeyEnv: 'GROQ_API_KEY';
62
+ readonly defaultFormat: 'json_object';
63
+ readonly tokenParameter: 'max_completion_tokens';
64
+ readonly documentationUrl: 'https://console.groq.com/docs/api-reference';
65
+ }, {
66
+ readonly id: 'deepseek';
67
+ readonly name: 'DeepSeek';
68
+ readonly protocol: 'openai-chat';
69
+ readonly endpoint: 'https://api.deepseek.com/chat/completions';
70
+ readonly apiKeyEnv: 'DEEPSEEK_API_KEY';
71
+ readonly defaultFormat: 'json_object';
72
+ readonly formats: readonly ["json_object", "prompt"];
73
+ readonly documentationUrl: 'https://api-docs.deepseek.com/api/create-chat-completion/';
74
+ }, {
75
+ readonly id: 'xai';
76
+ readonly name: 'xAI';
77
+ readonly protocol: 'openai-responses';
78
+ readonly endpoint: 'https://api.x.ai/v1/responses';
79
+ readonly apiKeyEnv: 'XAI_API_KEY';
80
+ readonly defaultFormat: 'json_schema';
81
+ readonly documentationUrl: 'https://docs.x.ai/developers/model-capabilities/text/structured-outputs';
82
+ }, {
83
+ readonly id: 'mistral';
84
+ readonly name: 'Mistral';
85
+ readonly protocol: 'openai-chat';
86
+ readonly endpoint: 'https://api.mistral.ai/v1/chat/completions';
87
+ readonly apiKeyEnv: 'MISTRAL_API_KEY';
88
+ readonly defaultFormat: 'json_schema';
89
+ readonly documentationUrl: 'https://docs.mistral.ai/api';
90
+ }, {
91
+ readonly id: 'together';
92
+ readonly name: 'Together AI';
93
+ readonly protocol: 'openai-chat';
94
+ readonly endpoint: 'https://api.together.ai/v1/chat/completions';
95
+ readonly apiKeyEnv: 'TOGETHER_API_KEY';
96
+ readonly defaultFormat: 'json_object';
97
+ readonly documentationUrl: 'https://docs.together.ai/reference/chat-completions';
98
+ }, {
99
+ readonly id: 'fireworks';
100
+ readonly name: 'Fireworks AI';
101
+ readonly protocol: 'openai-chat';
102
+ readonly endpoint: 'https://api.fireworks.ai/inference/v1/chat/completions';
103
+ readonly apiKeyEnv: 'FIREWORKS_API_KEY';
104
+ readonly defaultFormat: 'json_object';
105
+ readonly documentationUrl: 'https://docs.fireworks.ai/api-reference/post-chatcompletions';
106
+ }, {
107
+ readonly id: 'openrouter';
108
+ readonly name: 'OpenRouter';
109
+ readonly protocol: 'openai-chat';
110
+ readonly endpoint: 'https://openrouter.ai/api/v1/chat/completions';
111
+ readonly apiKeyEnv: 'OPENROUTER_API_KEY';
112
+ readonly defaultFormat: 'json_object';
113
+ readonly documentationUrl: 'https://openrouter.ai/docs/guides/features/structured-outputs';
114
+ }, {
115
+ readonly id: 'cerebras';
116
+ readonly name: 'Cerebras';
117
+ readonly protocol: 'openai-chat';
118
+ readonly endpoint: 'https://api.cerebras.ai/v1/chat/completions';
119
+ readonly apiKeyEnv: 'CEREBRAS_API_KEY';
120
+ readonly defaultFormat: 'json_object';
121
+ readonly tokenParameter: 'max_completion_tokens';
122
+ readonly documentationUrl: 'https://inference-docs.cerebras.ai/api-reference/chat-completions';
123
+ }, {
124
+ readonly id: 'nvidia';
125
+ readonly name: 'NVIDIA NIM';
126
+ readonly protocol: 'openai-chat';
127
+ readonly endpoint: 'https://integrate.api.nvidia.com/v1/chat/completions';
128
+ readonly apiKeyEnv: 'NVIDIA_API_KEY';
129
+ readonly defaultFormat: 'prompt';
130
+ readonly documentationUrl: 'https://docs.api.nvidia.com/nim/reference/openai-gpt-oss-120b-infer';
131
+ }, {
132
+ readonly id: 'sambanova';
133
+ readonly name: 'SambaNova';
134
+ readonly protocol: 'openai-chat';
135
+ readonly endpoint: 'https://api.sambanova.ai/v1/chat/completions';
136
+ readonly apiKeyEnv: 'SAMBANOVA_API_KEY';
137
+ readonly defaultFormat: 'json_object';
138
+ readonly documentationUrl: 'https://raw.githubusercontent.com/sambanova/sambanova-inference-api-spec/refs/heads/main/openapi.documented.json';
139
+ }, {
140
+ readonly id: 'huggingface';
141
+ readonly name: 'Hugging Face Inference Providers';
142
+ readonly protocol: 'openai-chat';
143
+ readonly endpoint: 'https://router.huggingface.co/v1/chat/completions';
144
+ readonly apiKeyEnv: 'HF_TOKEN';
145
+ readonly defaultFormat: 'prompt';
146
+ readonly documentationUrl: 'https://huggingface.co/docs/inference-providers/en/tasks/chat-completion';
147
+ }, {
148
+ readonly id: 'perplexity';
149
+ readonly name: 'Perplexity Agent API';
150
+ readonly protocol: 'perplexity';
151
+ readonly endpoint: 'https://api.perplexity.ai/v1/agent';
152
+ readonly apiKeyEnv: 'PERPLEXITY_API_KEY';
153
+ readonly defaultFormat: 'json_schema';
154
+ readonly formats: readonly ["json_schema", "prompt"];
155
+ readonly documentationUrl: 'https://docs.perplexity.ai/api-reference/agent-post';
156
+ }, {
157
+ readonly id: 'openai-compatible';
158
+ readonly name: 'OpenAI-compatible endpoint';
159
+ readonly protocol: 'openai-chat';
160
+ readonly apiKeyEnv: 'OPENAI_API_KEY';
161
+ readonly defaultFormat: 'json_schema';
162
+ readonly documentationUrl: 'https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create';
163
+ }, {
164
+ readonly id: 'ollama';
165
+ readonly name: 'Ollama local server';
166
+ readonly protocol: 'openai-chat';
167
+ readonly apiKeyEnv: 'OLLAMA_API_KEY';
168
+ readonly defaultFormat: 'json_schema';
169
+ readonly local: true;
170
+ readonly documentationUrl: 'https://docs.ollama.com/api/openai-compatibility';
171
+ }, {
172
+ readonly id: 'lmstudio';
173
+ readonly name: 'LM Studio local server';
174
+ readonly protocol: 'openai-chat';
175
+ readonly apiKeyEnv: 'LM_STUDIO_API_KEY';
176
+ readonly defaultFormat: 'json_schema';
177
+ readonly local: true;
178
+ readonly documentationUrl: 'https://lmstudio.ai/docs/developer/openai-compat/structured-output';
179
+ }, {
180
+ readonly id: 'vllm';
181
+ readonly name: 'vLLM server';
182
+ readonly protocol: 'openai-chat';
183
+ readonly apiKeyEnv: 'VLLM_API_KEY';
184
+ readonly defaultFormat: 'json_schema';
185
+ readonly local: true;
186
+ readonly documentationUrl: 'https://docs.vllm.ai/en/latest/features/structured_outputs/';
187
+ }, {
188
+ readonly id: 'litellm';
189
+ readonly name: 'LiteLLM gateway';
190
+ readonly protocol: 'openai-chat';
191
+ readonly apiKeyEnv: 'LITELLM_API_KEY';
192
+ readonly defaultFormat: 'json_object';
193
+ readonly local: true;
194
+ readonly documentationUrl: 'https://docs.litellm.ai/docs/completion/json_mode';
195
+ }];
196
+ export type ModelProvider = (typeof catalog)[number]['id'];
197
+ export declare const PROVIDERS: readonly Readonly<ProviderDefinition>[];
198
+ export declare const PROVIDER_IDS: readonly ("anthropic" | "azure-openai" | "cerebras" | "cohere" | "deepseek" | "fireworks" | "gemini" | "groq" | "huggingface" | "litellm" | "lmstudio" | "mistral" | "nvidia" | "ollama" | "openai" | "openai-compatible" | "openrouter" | "perplexity" | "sambanova" | "together" | "vllm" | "xai")[];
199
+ export declare function providerDefinition(id: string): Readonly<ProviderDefinition>;
200
+ export declare function allowsMissingKey(spec: ModelSpec): boolean;
201
+ export declare function modelOutputFormat(spec: ModelSpec): Exclude<OutputFormat, 'auto'>;
202
+ export declare function modelEndpoint(spec: ModelSpec): string;
203
+ export declare function buildModelRequest(spec: ModelSpec, token: string | undefined, instruction: string, input: string, schema: unknown, maxOutput: number): {
204
+ url: string;
205
+ headers: Record<string, string>;
206
+ body: Record<string, unknown>;
207
+ };
208
+ export declare function parseModelResponse(spec: ModelSpec, value: unknown): string;
209
+ export {};