@bugb-technologies/bravos 1.22.0 → 1.23.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/docs/UPGRADING.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  After `npm install -g @bugb-technologies/bravos`, run **`bravos setup`** in a repository you work in: it brings this machine and that repository up to date. In repositories you work in with an agent you can skip even that: the agent runs it at the start of a session. From 1.12 on you do not have to watch for releases: Bravos tells you when a newer version is out, and after you install it the agent brings each repository up to date. The notes below say what else changes, newest first. (Before 1.9 the same was `bravos setup --tools -y` per machine and `bravos sync` per repository; both still work.)
4
4
 
5
+ ## 1.23.0
6
+
7
+ - **A team trial licence finds out when the trial has ended.** A team's trial is one licence for the whole account and is never renewed, so until now it kept the Team features working offline until its own date, even after the team bought a plan or the trial was ended early.
8
+ - **What happens now:** at the start of a session, at most every 6 hours, a machine holding a trial licence asks the account portal whether the trial still runs (`POST /api/v1/cli/licence/trial`, with the trial licence as the bearer).
9
+ - **When the trial has ended:** the trial licence is removed from that machine and the next briefing says why. If the team is on a plan now, it says to run `bravos account activate` to sign the machine in with your own licence; otherwise it links to the plans.
10
+ - **When the portal can't be reached,** or answers anything unexpected, the licence is kept.
11
+ - **Never checked:** a paid licence, a licence set through `BRAVOS_LICENCE`, or a machine with `BRAVOS_NO_FETCH` set.
12
+ - Nothing else changes. Run `bravos setup` in each repository to refresh its installed hook copy.
13
+
5
14
  ## 1.22.0
6
15
 
7
16
  - **The intake picks up where you left off.** `bravos review intent` brings back the answers you gave when you reopen it, saves them as you go (only Confirm makes them count), and reuses the page already open instead of opening a second one. After Confirm the page stays open and says who confirmed and when.
@@ -1,13 +1,17 @@
1
1
  // @comment -- "Keeps a per-machine (device) licence current: the account portal issues each `bravos account activate` a short-lived token for (account, person, machine) carrying non-critical `uid`/`did` claims, and the device code from that pairing becomes this machine's refresh secret, kept in $BRAVOS_HOME/device.json (0600). refreshIfDue() trades the secret for a fresh token at the portal origin recorded at activation, once less than half the token's term is left (or it has expired, or the file is missing), verifies the new token with the same offline verifier before writing it, and fails OPEN on a network error, a timeout or a 5xx — the held token keeps working for whatever term it has. A refusal that means the machine is no longer licensed (401 invalid_device; 403 device_revoked, licence_unassigned, account_unavailable) removes the token and the device file, so the machine is back on Free and says why. A token supplied through BRAVOS_LICENCE / BRAVOS_LICENSE (CI) is never refreshed. Dependency-free (node:fs and the global fetch) so the session hook and the CLI share this one implementation."
2
2
  import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
- import { ACCOUNT_LICENCES, ACCOUNT_UPGRADE, LICENCE_ENV, LICENCE_ENV_ALT, LICENCE_FILES, bravosHome, verify } from './licence.mjs';
4
+ import { ACCOUNT_LICENCES, ACCOUNT_ORIGIN, ACCOUNT_UPGRADE, LICENCE_ENV, LICENCE_ENV_ALT, LICENCE_FILES, bravosHome, verify } from './licence.mjs';
5
5
  import { machineFingerprint } from './machine.mjs';
6
6
 
7
7
  export const DEVICE_FILE = 'device.json';
8
8
  export const REFRESH_PATH = '/api/v1/cli/licence/refresh';
9
9
  export const DEVICES_PATH = '/api/v1/cli/devices';
10
10
  export const REVOKE_PATH = '/api/v1/cli/devices/revoke';
11
+ export const TRIAL_PATH = '/api/v1/cli/licence/trial';
12
+ /** How often a trial licence asks whether its team's trial still runs. */
13
+ export const TRIAL_CHECK_SECONDS = 6 * 3600;
14
+ export const TRIAL_CHECK_FILE = 'trial-check.json';
11
15
  export const REFRESH_TIMEOUT_MS = 5000;
12
16
  /** After a refresh that could not reach the portal, an unforced refresh waits this long before trying again. */
13
17
  export const RETRY_AFTER_FAILURE_SECONDS = 15 * 60;
@@ -182,7 +186,9 @@ export async function refreshIfDue(env = process.env, opts = {}) {
182
186
  if ((env[LICENCE_ENV] || '').trim() || (env[LICENCE_ENV_ALT] || '').trim()) return { outcome: 'skipped', reason: 'environment' };
183
187
  if (env.BRAVOS_NO_FETCH || env.BRAVOS_REFRESH === 'off') return { outcome: 'skipped', reason: 'disabled' };
184
188
  const device = readDevice(env);
185
- if (!device) return { outcome: 'skipped', reason: 'no-device' };
189
+ // A TRIAL LICENCE HAS NO DEVICE RECORD: the team's trial is one licence for the
190
+ // account. It is not renewed; it only asks whether the trial still runs.
191
+ if (!device) return await checkTrialIfDue(env, { ...opts, now, fetch: f, force });
186
192
  const file = licencePath(env);
187
193
  const token = file ? readToken(file) : null;
188
194
  if (token) {
@@ -255,3 +261,86 @@ export async function refreshIfDue(env = process.env, opts = {}) {
255
261
  return { outcome: 'kept', reason: e && e.message ? e.message : 'error' };
256
262
  }
257
263
  }
264
+
265
+ const trialCheckPath = (env) => {
266
+ const home = bravosHome(env);
267
+ return home ? join(home, TRIAL_CHECK_FILE) : null;
268
+ };
269
+
270
+ /** The portal origin for a trial check: BRAVOS_ACCOUNT_ORIGIN when it is a plain origin (a local portal), else the account portal. */
271
+ const trialOrigin = (env) => {
272
+ const raw = (env.BRAVOS_ACCOUNT_ORIGIN || '').trim();
273
+ return /^https?:\/\/[^/\s]+$/.test(raw) ? raw : ACCOUNT_ORIGIN;
274
+ };
275
+
276
+ /** What to tell the person when their team's trial has ended. */
277
+ export function trialEndedNote(reason) {
278
+ return reason === 'plan'
279
+ ? `Your team is on a plan now, so the trial licence on this machine was removed. Run bravos account activate to sign this machine in with your licence (ask an owner or admin for one if you do not have it: ${ACCOUNT_LICENCES}).`
280
+ : `Your team's trial has ended, so this machine is back on Free. Choose a plan at ${ACCOUNT_UPGRADE} to keep the Team features.`;
281
+ }
282
+
283
+ // @comment -- "For a trial licence (one licence for the whole team, so no device record): at most every TRIAL_CHECK_SECONDS, POST <portal>/api/v1/cli/licence/trial with the trial licence itself as the bearer and ask whether the team's trial still runs. 200: it does. 403 trial_ended: the team bought a plan or the trial was ended, so licence.jwt is removed (only that file) and the person is told why on the next briefing. Anything else, no answer or a timeout keeps the licence: fail open, as every renewal does. Never throws."
284
+ // @exposes #licence-refresh to #token-disclosure [low] cwe:CWE-200 -- "Entry: checkTrialIfDue() at session start. Input: licence.jwt and env.BRAVOS_ACCOUNT_ORIGIN. Sink: fetch(trialOrigin(env) + TRIAL_PATH) with the licence as the Authorization bearer. Absent control: an origin taken from the environment unchecked would send the trial licence to any host."
285
+ // @mitigates #licence-refresh against #token-disclosure using #fixed-origin -- "The origin is the account portal, or BRAVOS_ACCOUNT_ORIGIN only when it is a plain http(s) origin; no proxy is read and the request has a timeout"
286
+ export async function checkTrialIfDue(env = process.env, opts = {}) {
287
+ const now = opts.now ?? Math.floor(Date.now() / 1000);
288
+ const f = opts.fetch ?? globalThis.fetch;
289
+ try {
290
+ const file = licencePath(env);
291
+ const token = file ? readToken(file) : null;
292
+ if (!token) return { outcome: 'skipped', reason: 'no-device' };
293
+ const v = verify(token);
294
+ const claims = v.state === 'invalid' ? null : claimsOf(token);
295
+ if (!claims || !claims.trial || typeof claims.did === 'string') return { outcome: 'skipped', reason: 'no-device' };
296
+ const stamp = trialCheckPath(env);
297
+ if (!opts.force && stamp) {
298
+ try {
299
+ const at = JSON.parse(readFileSync(stamp, 'utf8'))?.at;
300
+ if (typeof at === 'number' && now - at < TRIAL_CHECK_SECONDS && at <= now + 60) return { outcome: 'fresh', reason: 'trial-checked' };
301
+ } catch {
302
+ /* never checked */
303
+ }
304
+ }
305
+ let r;
306
+ try {
307
+ r = await f(`${trialOrigin(env)}${TRIAL_PATH}`, {
308
+ method: 'POST',
309
+ headers: { 'User-Agent': opts.userAgent ?? 'bravos-cli', Accept: 'application/json', Authorization: `Bearer ${token}` },
310
+ signal: AbortSignal.timeout(opts.timeoutMs ?? REFRESH_TIMEOUT_MS),
311
+ });
312
+ } catch {
313
+ return { outcome: 'kept', reason: 'unreachable' };
314
+ }
315
+ let body = null;
316
+ try {
317
+ body = await r.json();
318
+ } catch {
319
+ body = null;
320
+ }
321
+ if (r.status === 200) {
322
+ if (stamp) {
323
+ try {
324
+ writePrivate(stamp, JSON.stringify({ at: now }) + '\n');
325
+ } catch {
326
+ /* the stamp only spaces the checks */
327
+ }
328
+ }
329
+ return { outcome: 'fresh', reason: 'trial-running' };
330
+ }
331
+ if (r.status === 403 && body?.error === 'trial_ended') {
332
+ const why = body.reason === 'plan' ? 'plan' : 'ended';
333
+ try {
334
+ rmSync(file);
335
+ } catch {
336
+ /* already gone */
337
+ }
338
+ const note = trialEndedNote(why);
339
+ rememberNote(env, 'revoked', note, now);
340
+ return { outcome: 'revoked', reason: `trial_${why}`, note };
341
+ }
342
+ return { outcome: 'kept', reason: `http-${r.status}` };
343
+ } catch (e) {
344
+ return { outcome: 'kept', reason: e && e.message ? e.message : 'error' };
345
+ }
346
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bugb-technologies/bravos",
3
- "version": "1.22.0",
3
+ "version": "1.23.0",
4
4
  "description": "Skills pack and installer that make Claude Code and Codex power users of guardlink (threat-model-as-code) and cert-x-gen (pentest-as-code).",
5
5
  "keywords": [
6
6
  "security",