@hivemagnus/hive-devtools 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +27 -0
- package/LICENSE +202 -0
- package/NOTICE +11 -0
- package/README.md +303 -2
- package/bin/check-action-pins.mjs +27 -0
- package/bin/check-node-runtime.mjs +9 -0
- package/bin/check-private-data.sh +152 -0
- package/bin/check-publish-environment.mjs +14 -0
- package/bin/gitleaks.sh +111 -0
- package/bin/install-gitleaks.sh +44 -0
- package/bin/publish-package.mjs +23 -0
- package/bin/scan-secrets.mjs +73 -0
- package/lib/action-pins.mjs +100 -0
- package/lib/node-runtime.mjs +74 -0
- package/lib/privacy-policy.mjs +104 -0
- package/lib/publish.mjs +78 -0
- package/package.json +58 -4
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared privacy and secret policy for Hive:Magnus repositories.
|
|
3
|
+
* Findings carry a line number and a rule name, never the matching value.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Directories that hold runtime state, credentials or agent data and are never committed. */
|
|
7
|
+
export const DEFAULT_FORBIDDEN_DIRS = Object.freeze([
|
|
8
|
+
'certs', 'backups', 'state', 'artifacts', '.ardent', '.magnus', '.cindertrace', '.hive-dev', '.ssh',
|
|
9
|
+
'audit-logs', 'terminal-audit',
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** Variable names whose literal values are credentials. Names ending in PASSWORD need 8 characters, others 16. */
|
|
13
|
+
export const DEFAULT_SECRET_NAMES = Object.freeze([
|
|
14
|
+
'DEPLOY_PASSWORD', 'SSH_PASSWORD', 'IAM_AGENT_SECRET', 'HIVE_ROUTER_SECRET', 'HIVEMEM_JWT_SECRET', 'API_SECRET',
|
|
15
|
+
'JWT_SECRET', 'SESSION_SECRET', 'NODE_MANAGER_SECRET', 'AWS_SECRET_ACCESS_KEY',
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** Account names that may appear in /home/<name> and /Users/<name> paths (documentation placeholders). */
|
|
19
|
+
export const DEFAULT_HOME_USERS = Object.freeze(['operator', 'deploy', 'user', 'x', 'foo', 'bar']);
|
|
20
|
+
|
|
21
|
+
const DIR_NAME = /^[A-Za-z0-9._-]{1,64}$/;
|
|
22
|
+
const SECRET_NAME = /^[A-Z][A-Z0-9_]{2,63}$/;
|
|
23
|
+
const HOME_USER = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/;
|
|
24
|
+
const escape = value => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
25
|
+
|
|
26
|
+
function validated(values, pattern, label) {
|
|
27
|
+
if (!Array.isArray(values)) throw new TypeError(`${label} must be an array`);
|
|
28
|
+
for (const value of values) {
|
|
29
|
+
if (typeof value !== 'string' || !pattern.test(value) || value === '.' || value === '..') {
|
|
30
|
+
throw new TypeError(`Invalid ${label}: ${JSON.stringify(String(value)).slice(0, 80)}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return values;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Validates extra forbidden directory names (also used by the shell scanner). */
|
|
37
|
+
export const validateDirNames = names => validated(names, DIR_NAME, 'forbidden directory name');
|
|
38
|
+
|
|
39
|
+
/** Builds the forbidden-path test. `dirs` replaces nothing: it adds to DEFAULT_FORBIDDEN_DIRS. */
|
|
40
|
+
export function forbiddenPathMatcher({ dirs = [], allow = [] } = {}) {
|
|
41
|
+
const all = [...DEFAULT_FORBIDDEN_DIRS, ...validateDirNames(dirs)];
|
|
42
|
+
const allowed = allow.map(pattern => new RegExp(pattern, 'i'));
|
|
43
|
+
const forbidden = new RegExp('(^|/)\\.env($|\\.(?!example$))|(^|/)\\.npmrc$|\\.(pem|key|crt|p12|pfx|db|sqlite|sqlite3|bundle)'
|
|
44
|
+
+ '(?:-(?:wal|shm|journal))?$|(^|/)[^/]+\\.local\\.md$|(^|/)\\.private-terms$|(^|/)\\.vscode/mcp\\.json$'
|
|
45
|
+
+ `|(^|/)(${all.map(escape).join('|')})/`, 'i');
|
|
46
|
+
return path => forbidden.test(path) && !allowed.some(pattern => pattern.test(path));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** True when a repository-relative path may never be committed. */
|
|
50
|
+
export function forbiddenPath(path, options) {
|
|
51
|
+
return forbiddenPathMatcher(options)(path);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const TOKEN_RULES = [
|
|
55
|
+
['OpenAI key', /\bsk-(?:proj-|svcacct-)?[A-Za-z0-9_-]{24,}\b/g],
|
|
56
|
+
['npm token', /\bnpm_[A-Za-z0-9]{36,}\b/g],
|
|
57
|
+
['GitHub token', /\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{40,})\b/g],
|
|
58
|
+
['Slack token', /\bxox[baprs]-[A-Za-z0-9-]{20,}\b/g],
|
|
59
|
+
['AWS access key', /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g],
|
|
60
|
+
['private key', /-----BEGIN (?:RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY-----/g],
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
// Exempt only an explicitly fake value, never unrelated words elsewhere on its line.
|
|
64
|
+
function placeholder(value) {
|
|
65
|
+
return /^(?:change[-_]me[-_]|replace[-_]|playwright[-_]|dummy[-_]|your[-_]|mock[-_]|ci-only-|\$\{|<)/i.test(value)
|
|
66
|
+
|| /(?:^|[-_])(?:test|fixture|example|placeholder)(?:[-_]|$)/i.test(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Provider tokens, private keys and literal values assigned to credential variables. */
|
|
70
|
+
export function secretFindings(content, { secretNames = [] } = {}) {
|
|
71
|
+
const names = [...new Set([...DEFAULT_SECRET_NAMES, ...validated(secretNames, SECRET_NAME, 'secret variable name')])];
|
|
72
|
+
const assignments = new RegExp(`\\b(${names.join('|')})["']?\\s*[:=]\\s*(?:["']([^"'\\r\\n]+)["']|([^\\s#;,'"}]+))`, 'g');
|
|
73
|
+
const findings = [];
|
|
74
|
+
for (const [index, line] of content.split('\n').entries()) {
|
|
75
|
+
for (const [rule, pattern] of TOKEN_RULES) {
|
|
76
|
+
pattern.lastIndex = 0;
|
|
77
|
+
if (pattern.test(line)) findings.push({ line: index + 1, rule });
|
|
78
|
+
}
|
|
79
|
+
// Quoted and unquoted shell/JSON/YAML/TS literal credentials. Expressions are not literals.
|
|
80
|
+
for (const match of line.matchAll(assignments)) {
|
|
81
|
+
const value = (match[2] ?? match[3]).split(/\\[nr]/, 1)[0];
|
|
82
|
+
const minimum = match[1].endsWith('PASSWORD') ? 8 : 16;
|
|
83
|
+
const expression = match[2] == null && (/^(?:[A-Za-z_$][\w$]*(?:\.|\(|\[)|\$|\.\*)/.test(value)
|
|
84
|
+
|| (match[0].includes(':') && /^[A-Za-z_$][\w$]*$/.test(value)));
|
|
85
|
+
if (value.length >= minimum && !placeholder(value) && !expression) {
|
|
86
|
+
findings.push({ line: index + 1, rule: 'literal credential' });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return findings;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Private network domains and personal home directories, without any operator denylist. */
|
|
94
|
+
export function privateInfrastructureFindings(content, { homeUsers = [] } = {}) {
|
|
95
|
+
const allowed = new Set([...DEFAULT_HOME_USERS, ...validated(homeUsers, HOME_USER, 'home directory user')]);
|
|
96
|
+
const findings = [];
|
|
97
|
+
for (const [index, line] of content.split('\n').entries()) {
|
|
98
|
+
if (/\b(?:[a-z0-9-]+\.)+ts\.net\b/i.test(line)) findings.push({ line: index + 1, rule: 'private network domain' });
|
|
99
|
+
for (const match of line.matchAll(/\/(?:home|Users)\/([a-zA-Z0-9][a-zA-Z0-9._-]*)/g)) {
|
|
100
|
+
if (!allowed.has(match[1])) findings.push({ line: index + 1, rule: 'personal home path' });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return findings;
|
|
104
|
+
}
|
package/lib/publish.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// npm release helpers for packages that publish from a tag through npm trusted publishing (OIDC).
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
const ENVIRONMENT = /^[A-Za-z0-9_.-]{1,255}$/;
|
|
7
|
+
const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
8
|
+
const TAG_PREFIX = /^[A-Za-z0-9._/-]{0,64}$/;
|
|
9
|
+
|
|
10
|
+
/** Throws unless a GitHub environment has at least one required reviewer (a user or team). */
|
|
11
|
+
export function assertPublishEnvironment(value, environment = 'npm-publish') {
|
|
12
|
+
const rules = value?.protection_rules;
|
|
13
|
+
if (!Array.isArray(rules) || !rules.some(rule => rule.type === 'required_reviewers'
|
|
14
|
+
&& Array.isArray(rule.reviewers) && rule.reviewers.some(reviewer =>
|
|
15
|
+
['User', 'Team'].includes(reviewer.type) && Number.isSafeInteger(reviewer.reviewer?.id) && reviewer.reviewer.id > 0))) {
|
|
16
|
+
throw new Error(`${environment} must have at least one required reviewer configured in GitHub Settings > Environments before publishing`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Reads the environment from the GitHub API (GITHUB_REPOSITORY and GH_TOKEN from `env`) and
|
|
22
|
+
* fails closed on any error. The token is sent only to api.github.com, with redirects refused.
|
|
23
|
+
*/
|
|
24
|
+
export async function checkPublishEnvironment(env = process.env, fetcher = fetch, { environment = 'npm-publish' } = {}) {
|
|
25
|
+
if (!ENVIRONMENT.test(environment)) throw new Error('Invalid environment name');
|
|
26
|
+
if (!REPOSITORY.test(env.GITHUB_REPOSITORY ?? '') || !env.GH_TOKEN) {
|
|
27
|
+
throw new Error('GITHUB_REPOSITORY and GH_TOKEN are required to verify publication protection');
|
|
28
|
+
}
|
|
29
|
+
const response = await fetcher(`https://api.github.com/repos/${env.GITHUB_REPOSITORY}/environments/${encodeURIComponent(environment)}`, {
|
|
30
|
+
headers: { Authorization: `Bearer ${env.GH_TOKEN}`, Accept: 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28' },
|
|
31
|
+
redirect: 'error', signal: AbortSignal.timeout(10_000),
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok) throw new Error(`Could not verify ${environment} environment protection (HTTP ${response.status})`);
|
|
34
|
+
assertPublishEnvironment(await response.json(), environment);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Reads package.json and checks that it is a public, versioned package (with the expected name, if given). */
|
|
38
|
+
export function readReleasePackage(root, { name } = {}) {
|
|
39
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
40
|
+
if (typeof pkg.name !== 'string' || !/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(pkg.name)
|
|
41
|
+
|| (name !== undefined && pkg.name !== name)
|
|
42
|
+
|| !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(pkg.version ?? '') || pkg.private || pkg.publishConfig?.access !== 'public') {
|
|
43
|
+
throw new Error(`package.json must be the public ${name ?? 'npm'} package with a valid version and publishConfig.access "public"`);
|
|
44
|
+
}
|
|
45
|
+
return pkg;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** True when name@version is on the registry; any registry failure stops publication. */
|
|
49
|
+
export async function publishedVersionExists(name, version, fetcher = fetch) {
|
|
50
|
+
const response = await fetcher(`https://registry.npmjs.org/${encodeURIComponent(name)}/${encodeURIComponent(version)}`, {
|
|
51
|
+
redirect: 'error', signal: AbortSignal.timeout(15_000),
|
|
52
|
+
});
|
|
53
|
+
if (response.status === 404) return false;
|
|
54
|
+
if (!response.ok) throw new Error(`Could not check ${name}@${version} on npm (HTTP ${response.status}); publication stopped`);
|
|
55
|
+
const metadata = await response.json();
|
|
56
|
+
if (metadata.name !== name || metadata.version !== version) throw new Error(`Unexpected npm metadata for ${name}@${version}; publication stopped`);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Publishes the package at `root` with provenance unless its version is already on npm.
|
|
62
|
+
* A dry run packs and validates without registry access. A real publication must run from
|
|
63
|
+
* the tag `refs/tags/<tagPrefix><version>`.
|
|
64
|
+
*/
|
|
65
|
+
export async function publishPackage({ root, dryRun, ref, name, tagPrefix = 'v', fetcher = fetch, run = execFileSync }) {
|
|
66
|
+
if (typeof dryRun !== 'boolean') throw new Error('An explicit boolean dryRun is required');
|
|
67
|
+
if (!TAG_PREFIX.test(tagPrefix)) throw new Error('Invalid tag prefix');
|
|
68
|
+
const pkg = readReleasePackage(root, name === undefined ? {} : { name });
|
|
69
|
+
if (!dryRun && ref !== `refs/tags/${tagPrefix}${pkg.version}`) throw new Error('Real publication must run from the matching release tag');
|
|
70
|
+
// A registry or authentication failure must not be mistaken for an unpublished version.
|
|
71
|
+
if (!dryRun && await publishedVersionExists(pkg.name, pkg.version, fetcher)) {
|
|
72
|
+
console.log(`${pkg.name}@${pkg.version} is already on npm; skipping`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// The payload was built, tested and inspected by the gates; never run lifecycle scripts here.
|
|
76
|
+
run('npm', ['publish', dryRun ? '--dry-run' : '--provenance', '--ignore-scripts', '--access', 'public',
|
|
77
|
+
'--registry', 'https://registry.npmjs.org/'], { cwd: root, stdio: 'inherit' });
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hivemagnus/hive-devtools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared development tooling for the Hive:Magnus suite repositories: privacy and secret scans, the pinned Gitleaks wrapper, Node runtime and GitHub Actions pin checks, and npm release helpers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hive",
|
|
7
|
+
"magnus",
|
|
8
|
+
"gitleaks",
|
|
9
|
+
"secret-scanning",
|
|
10
|
+
"github-actions",
|
|
11
|
+
"devtools"
|
|
12
|
+
],
|
|
13
|
+
"author": "ChefTaco",
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ChefTaco/HiveCommon.git",
|
|
18
|
+
"directory": "tooling"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/ChefTaco/HiveCommon/tree/main/tooling#readme",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/ChefTaco/HiveCommon/issues"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"bin": {
|
|
26
|
+
"hive-check-private-data": "bin/check-private-data.sh",
|
|
27
|
+
"hive-scan-secrets": "bin/scan-secrets.mjs",
|
|
28
|
+
"hive-gitleaks": "bin/gitleaks.sh",
|
|
29
|
+
"hive-install-gitleaks": "bin/install-gitleaks.sh",
|
|
30
|
+
"hive-check-node-runtime": "bin/check-node-runtime.mjs",
|
|
31
|
+
"hive-check-action-pins": "bin/check-action-pins.mjs",
|
|
32
|
+
"hive-check-publish-environment": "bin/check-publish-environment.mjs",
|
|
33
|
+
"hive-publish-package": "bin/publish-package.mjs"
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
"./privacy-policy": "./lib/privacy-policy.mjs",
|
|
37
|
+
"./node-runtime": "./lib/node-runtime.mjs",
|
|
38
|
+
"./action-pins": "./lib/action-pins.mjs",
|
|
39
|
+
"./publish": "./lib/publish.mjs",
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": "^24.22.3 || ^26.13.1"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"bin",
|
|
50
|
+
"lib",
|
|
51
|
+
"README.md",
|
|
52
|
+
"CHANGELOG.md",
|
|
53
|
+
"LICENSE",
|
|
54
|
+
"NOTICE"
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"test": "node --test test/*.test.mjs",
|
|
58
|
+
"test:ci": "bash bin/install-gitleaks.sh && HIVE_DEVTOOLS_REQUIRE_GITLEAKS=1 node --test test/*.test.mjs"
|
|
59
|
+
}
|
|
60
|
+
}
|