ci(release): stop on a stale dialect, check it like CI would, give prereleases their own

A stable release without a dialect of its own now ships the one on main only while that one has 90 days left; dialect 0 or less stops the run before anything is committed, unless allow_stale_dialect is ticked. Fetching and checking moved into registration-dialect.yml, which never fails its caller and also type-checks the module under the strict config and lints it with --max-warnings 0, since the bump commit skips CI. The bump and everything after it run whatever became of the dialect job. Prereleases ask the issuer for a dialect of their -pre.N version and build it into the image without committing it. The release check reads the module token by token against an allowlist of keywords, globals and member names, refuses any assignment to a property, computed access and regular expressions, and names Object, JSON and defineProperty among the forbidden. The issuer's kitchen-sink sample is gone; the tests use a hand-written dialect instead.
This commit is contained in:
Maurice
2026-10-29 17:59:52 +01:00
parent cd5ce4b1d9
commit 12e22c258a
12 changed files with 732 additions and 439 deletions
+2 -3
View File
@@ -2,7 +2,6 @@
# by check:entities, so Prettier must not rewrite them.
src/db/entities/
# Written by the TREK services issuer and committed as it wrote them: the
# registration dialect (taken over by the release job) and the issuer's sample.
# Written by the TREK services issuer and committed as it wrote it: the
# registration dialect, taken over by the release job.
src/nest/service-identity/registration-dialect.ts
tests/fixtures/service-identity/registration-dialect.sample.ts
+3 -7
View File
@@ -34,13 +34,9 @@ const root = fileURLToPath(new URL('..', import.meta.url));
// byte to its output by check:entities, so their layout is the generator's, not
// Prettier's. .prettierignore keeps `npm run format` off them for the same reason.
const GENERATED = 'src/db/entities/';
// The registration dialect and the issuer's sample of one are written by the
// TREK services issuer and committed as it wrote them (the release job takes
// the dialect over unchanged), so their layout is the issuer's generator's.
const ISSUER_WRITTEN = [
'src/nest/service-identity/registration-dialect.ts',
'tests/fixtures/service-identity/registration-dialect.sample.ts',
];
// The registration dialect is written by the TREK services issuer and taken
// over unchanged by the release job, so its layout is the issuer's generator's.
const ISSUER_WRITTEN = ['src/nest/service-identity/registration-dialect.ts'];
await runCli('lint:format', (args) =>
check({
prettier,
@@ -7,11 +7,7 @@
* such release it speaks the original registration, which the issuer accepts
* for a transition period. Renewing a key never goes through this file: an
* installation keeps its key whatever release it runs. After a stable release,
* merge main into dev so that dev and prerelease builds register with the
* current dialect.
*
* The dialect is the registration's, not SQL's; lint:dialect has nothing to
* look for here.
* merge main into dev so that dev builds register with the current dialect.
*/
import type { DialectSelfCheck, RegistrationDialect } from './registration-dialect.types';
@@ -0,0 +1,71 @@
/**
* A registration dialect written by hand for TREK's tests. It has the shape of
* the modules the issuer writes for each release (the exports, the paths, a
* tag in a header, helpers with loops), so the release check and the key
* service can be tested against a dialect that is not the committed one, but
* it is none of the issuer's and shows nothing of how they are made.
*/
import type { DialectSelfCheck, RegistrationDialect } from './registration-dialect.types';
import { createHash, createHmac } from 'node:crypto';
import { crc32 } from 'node:zlib';
const DIALECT = 'HandWritten1';
const PEPPER = Buffer.from('7472656b2d746573742d6469616c656374', 'hex');
function withChecksum(input: Buffer): Buffer {
const out = Buffer.alloc(input.length + 4);
input.copy(out);
out.writeUInt32BE(crc32(input), input.length);
return out;
}
function backwards(input: Buffer): Buffer {
const out: number[] = [];
for (let i = input.length - 1; i >= 0; i--) out.push(input.readUInt8(i) ^ (i & 0xff));
return Buffer.from(out);
}
export const registrationDialect: RegistrationDialect = {
id: DIALECT,
releasedFor: '9.9.9',
createdAt: '2026-10-29',
challengePath: `/v1/register/${DIALECT}/challenge`,
registerPath: `/v1/register/${DIALECT}`,
begin({ challenge, version, random, nowSeconds }) {
const c = Buffer.from(challenge.c, 'utf8');
const salt = createHmac('sha256', PEPPER).update(c).digest();
return {
work(nonce) {
const joined = Buffer.concat([c, Buffer.from(`.${nonce}.`), Buffer.from(version, 'utf8'), random]);
return { password: backwards(withChecksum(createHash('sha512').update(joined).digest())), salt };
},
request(nonce) {
const seal = createHmac('sha256', random).update(`${challenge.c}|${nonce}|${nowSeconds}`).digest();
return {
body: {
ticket: challenge.c,
count: String(nonce),
release: version,
lantern: random.toString('base64url'),
sentAt: nowSeconds,
},
headers: { 'X-TREK-Test-Seal': seal.toString('hex') },
};
},
};
},
};
/** Fixed inputs and what this module makes of them. */
export const SELF_CHECK: DialectSelfCheck = {
challenge: { c: 'v1.selfcheck.HandWritten1', alg: 'scrypt', N: 16, r: 1, p: 1, bits: 0, expires_at: 1 },
version: '9.9.9',
random: '000102030405060708090a0b0c0d0e0f',
nowSeconds: 1800000000,
nonce: 41,
passwordSha256: 'cf7d2be18b531a1a1f2427ce744cd1bd7b18ae731e1449ac56c827e7a683da51',
saltSha256: '005cc836f9d0a082e0c3712401f1d95515b6066855e8f10533353b6eb0e76f3d',
scrypt: '5fd715b7942b9bc9ccee9b811b5566d31aa7ca6d3a011222fe3101d6178d4396',
requestSha256: '2b41dc94a997e22cecc7bb946889208fc86c8dff0f5e30075a2fcee28239f0b8',
};
@@ -1,262 +0,0 @@
/**
* How TREK 9.9.9 registers with the TREK services.
*
* The issuer writes this file for each release and the release pipeline
* commits it with the version bump. It is not edited by hand, and its layout
* is the generator's, so Prettier leaves it alone. Renewing a key never goes
* through it: an installation keeps its key whatever release it runs. After a
* stable release, merge main into dev so that dev and prerelease builds
* register with the current dialect.
*
* The dialect is the registration's, not SQL's; lint:dialect has nothing to
* look for here.
*/
import type { DialectSelfCheck, RegistrationDialect } from './registration-dialect.types';
import { createHash, createHmac, hkdfSync, pbkdf2Sync } from 'node:crypto';
import { crc32 } from 'node:zlib';
const DIALECT = 'KitchenSink0';
const CONSTANT = Buffer.from('206ebd4ad98b5f9c3531195aa89f6285612c396a98999a127b7972b6eefe03a8', 'hex');
const BASE32 = 'abcdefghijklmnopqrstuvwxyz234567';
function u32be(value: number): Buffer {
const out = Buffer.alloc(4);
out.writeUInt32BE(value >>> 0);
return out;
}
function u32le(value: number): Buffer {
const out = Buffer.alloc(4);
out.writeUInt32LE(value >>> 0);
return out;
}
function u64be(value: number): Buffer {
const out = Buffer.alloc(8);
out.writeBigUInt64BE(BigInt(value));
return out;
}
function u64le(value: number): Buffer {
const out = Buffer.alloc(8);
out.writeBigUInt64LE(BigInt(value));
return out;
}
function leb128(value: number): Buffer {
const out: number[] = [];
let rest = value;
do {
const low = rest % 128;
rest = Math.floor(rest / 128);
out.push(low | (Math.sign(rest) << 7));
} while (rest > 0);
return Buffer.from(out);
}
function lp32le(part: Buffer): Buffer {
const head = Buffer.alloc(4);
head.writeUInt32LE(part.length);
return Buffer.concat([head, part]);
}
function base32(input: Buffer): string {
const bits = [...input].map((byte) => byte.toString(2).padStart(8, '0')).join('');
let out = '';
for (let i = 0; i < bits.length; i += 5) {
out += BASE32.charAt(parseInt(bits.slice(i, i + 5).padEnd(5, '0'), 2));
}
return out;
}
function reverseBlocks(input: Buffer, width: number): Buffer {
const out = Buffer.from(input);
for (let i = 0; i + width <= out.length; i += width) out.subarray(i, i + width).reverse();
return out;
}
function permute(input: Buffer, order: number[]): Buffer {
const out = Buffer.from(input);
order.forEach((from, to) => out.writeUInt8(input.readUInt8(from), to));
return out;
}
function fold(input: Buffer, size: number): Buffer {
const out = Buffer.alloc(size);
input.forEach((byte, i) => out.writeUInt8(out.readUInt8(i % size) ^ byte, i % size));
return out;
}
function interleave(a: Buffer, b: Buffer): Buffer {
const parts: Buffer[] = [];
for (let i = 0; i < Math.max(a.length, b.length); i++) parts.push(a.subarray(i, i + 1), b.subarray(i, i + 1));
return Buffer.concat(parts);
}
function mixWordsBe(input: Buffer, rotation: number, consts: Buffer): Buffer {
const out = Buffer.from(input);
let previous = 0;
for (let i = 0; i + 4 <= out.length; i += 4) {
const word = (out.readUInt32BE(i) + consts.readUInt32BE(i % consts.length)) >>> 0;
previous = (((word << rotation) | (word >>> (32 - rotation))) ^ previous) >>> 0;
out.writeUInt32BE(previous, i);
}
return out;
}
function mixWordsLe(input: Buffer, rotation: number, consts: Buffer): Buffer {
const out = Buffer.from(input);
let previous = 0;
for (let i = 0; i + 4 <= out.length; i += 4) {
const word = (out.readUInt32LE(i) + consts.readUInt32BE(i % consts.length)) >>> 0;
previous = (((word << rotation) | (word >>> (32 - rotation))) ^ previous) >>> 0;
out.writeUInt32LE(previous, i);
}
return out;
}
function keystream8be(input: Buffer, start: number): Buffer {
const out = Buffer.from(input);
for (let offset = 0; offset < out.length; offset += 32) {
const counter = Buffer.from([(start + offset / 32) & 0xff]);
const block = createHash('sha256').update(CONSTANT).update(counter).digest();
for (let j = offset; j < Math.min(offset + 32, out.length); j++) {
out.writeUInt8(out.readUInt8(j) ^ block.readUInt8(j - offset), j);
}
}
return out;
}
function keystream16le(input: Buffer, start: number): Buffer {
const out = Buffer.from(input);
for (let offset = 0; offset < out.length; offset += 32) {
const counter = Buffer.alloc(2);
counter.writeUInt16LE((start + offset / 32) & 0xffff);
const block = createHash('sha256').update(CONSTANT).update(counter).digest();
for (let j = offset; j < Math.min(offset + 32, out.length); j++) {
out.writeUInt8(out.readUInt8(j) ^ block.readUInt8(j - offset), j);
}
}
return out;
}
function keystream32be(input: Buffer, start: number): Buffer {
const out = Buffer.from(input);
for (let offset = 0; offset < out.length; offset += 32) {
const counter = Buffer.alloc(4);
counter.writeUInt32BE((start + offset / 32) >>> 0);
const block = createHash('sha256').update(CONSTANT).update(counter).digest();
for (let j = offset; j < Math.min(offset + 32, out.length); j++) {
out.writeUInt8(out.readUInt8(j) ^ block.readUInt8(j - offset), j);
}
}
return out;
}
export const registrationDialect: RegistrationDialect = {
id: DIALECT,
releasedFor: '9.9.9',
createdAt: '2026-01-01',
challengePath: `/v1/register/${DIALECT}/challenge`,
registerPath: `/v1/register/${DIALECT}`,
begin({ challenge, version, random, nowSeconds }) {
const c = Buffer.from(challenge.c, 'utf8');
const v = Buffer.from(version, 'utf8');
const hc = createHash('sha256').update(c).digest();
const salt1 = Buffer.concat([
CONSTANT.subarray(0, 8),
Buffer.from([0x1f]),
hc,
Buffer.from([0x1f]),
c.subarray(3, 19),
]);
const salt2 = createHash('shake256', { outputLength: 24 }).update(salt1).digest();
const salt3 = pbkdf2Sync(salt2, random, 3, 32, 'sha512');
const salt4 = Buffer.from(hkdfSync('sha384', salt3, hc.subarray(0, 16), Buffer.from('a1b2c3d4e5', 'hex'), 40));
const salt5 = createHmac('sha3-256', CONSTANT).update(salt4).digest();
const salt6 = fold(salt5, 20);
const salt7Words = Buffer.from('deadbeef0badf00d', 'hex');
const salt7 = mixWordsBe(salt6, 5, salt7Words);
const salt8 = Buffer.concat([random.subarray(2, 9), salt7]);
const salt9 = Buffer.from(salt8.toString('base64url'), 'utf8');
return {
work(nonce) {
const pass1 = Buffer.concat([
lp32le(c),
lp32le(leb128(nonce)),
lp32le(v),
lp32le(random),
lp32le(u32le(nowSeconds)),
lp32le(u64le(nonce)),
lp32le(hc.subarray(3, 19)),
]);
const pass2 = createHash('blake2b512').update(pass1).digest();
const pass3Words = Buffer.from('9e3779b97f4a7c1501234567', 'hex');
const pass3 = mixWordsLe(mixWordsLe(pass2, 7, pass3Words), 13, pass3Words);
const pass4 = keystream16le(pass3, 5);
const pass5 = Buffer.concat([pass4.subarray(9 % pass4.length), pass4.subarray(0, 9 % pass4.length)]);
const pass6 = reverseBlocks(pass5, 4);
const pass7 = permute(pass6, [3, 0, 15, 7, 1, 2, 4, 6, 5, 8, 9, 11, 10, 13, 12, 14]);
const pass8 = Buffer.from(pass7.map((byte) => (byte * 37 + 11) & 0xff));
const pass9 = interleave(pass8, CONSTANT);
const pass10 = Buffer.concat([pass9, u32be(crc32(pass9))]);
const pass11 = Buffer.from(pass10.toString('hex'), 'utf8');
const pass12 = Buffer.concat([pass11, Buffer.from([0x3a]), v]);
const pass13 = Buffer.from(base32(pass12), 'utf8');
const pass14 = pass13.subarray(10, 58);
return { password: pass14, salt: salt9 };
},
request(nonce) {
const seal1 = Buffer.concat([
v,
Buffer.from(String(nonce), 'utf8'),
Buffer.from(String(nowSeconds), 'utf8'),
c,
random,
u64be(nonce),
u32be(nowSeconds),
Buffer.from(nonce.toString(16), 'utf8'),
]);
const seal2 = createHmac('sha512', seal1).update(CONSTANT).digest();
const seal3 = Buffer.from(seal2.toString('base64'), 'utf8');
const seal4 = createHash('sha384').update(seal3).digest();
const seal5 = Buffer.from(seal4.toString('hex').toUpperCase(), 'utf8');
const seal6 = Buffer.from(seal5).reverse();
const seal7 = keystream32be(seal6, 200);
const seal8 = interleave(seal7, hc).subarray(0, seal7.length);
const seal9 = Buffer.concat([seal8, u32le(crc32(seal8))]);
const seal10 = keystream8be(seal9, 1);
const seal11 = seal10.subarray(8, 40);
return {
body: {
'ferry-passage': {
'release-mark': version,
tide: 'amber-41',
'ticket-stamp': challenge.c,
harbor: nonce.toString(16),
'gull-wing': 'slate-7',
lantern: base32(random),
compass: nowSeconds,
},
},
headers: { 'X-TREK-Ferry-Seal': seal11.toString('base64url') },
};
},
};
},
};
/** Fixed inputs and what this module must make of them; TREK's tests and release job run it. */
export const SELF_CHECK: DialectSelfCheck = {
challenge: { c: 'v1.selfcheck.KitchenSink0', alg: 'scrypt', N: 16, r: 1, p: 1, bits: 0, expires_at: 1 },
version: '9.9.9',
random: '000102030405060708090a0b0c0d0e0f',
nowSeconds: 1800000000,
nonce: 41,
passwordSha256: '7b2c79ba69f022e031c4db19ff0bdf1f67bed98498a73ed747f968d346bc3110',
saltSha256: '552659da096a9c393d520721f1aa26e5d3716305f4a6167effb13b9e7660e737',
scrypt: '611e71acba947b2fffb86a831d4f389c8f72f5182d7bde30d4bb1f5c3abac730',
requestSha256: '3df6e9c6fb70e039a2cd072cb526e975c996e6e6618ded4c89e09f6a7440c4f4',
};
@@ -1,8 +1,8 @@
/**
* The sample dialect beside this file is the issuer's own test output, kept
* byte for byte, and imports its types from next to itself the way a real
* dialect does in src/nest/service-identity/. This points that import at the
* real types, so the sample is typechecked and linted against them.
* The example dialect beside this file imports its types from next to itself
* the way a real dialect does in src/nest/service-identity/, so the release
* check reads it as it reads one. This points that import at the real types,
* so the example is typechecked and linted against them.
*/
export type {
DialectSelfCheck,
@@ -1,6 +1,6 @@
/**
* The registration dialect: the module this build carries, and the issuer's
* sample that uses every building block a generated dialect can be made of.
* The registration dialect: the module this build carries, and a dialect
* written by hand in the shape of the issuer's (a tag in a header, helpers).
* Each must compute what the issuer computed for its fixed inputs (SELF_CHECK),
* and its requests must be ones the issuer client lets out, so a dialect that
* reaches main is one TREK can run.
@@ -13,7 +13,7 @@ import type {
RegistrationDialect,
} from '../../../../src/nest/service-identity/registration-dialect.types';
import { leadingZeroBits, powHash, solvePow } from '../../../../src/nest/service-identity/service-key.pow';
import * as sample from '../../../fixtures/service-identity/registration-dialect.sample';
import * as example from '../../../fixtures/service-identity/registration-dialect.example';
import { createHash, scryptSync } from 'node:crypto';
import { beforeEach, describe, expect, it, vi } from 'vitest';
@@ -42,7 +42,7 @@ function inputsOf(check: DialectSelfCheck): DialectInputs {
const modules: [string, { registrationDialect: RegistrationDialect; SELF_CHECK: DialectSelfCheck }][] = [
['the committed dialect', committed],
["the issuer's sample", sample],
['a hand-written dialect', example],
];
describe.each(modules)('%s', (_, { registrationDialect: dialect, SELF_CHECK: check }) => {