Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(check-secrets): catch base58-encoded Solana secret keys before push

nirholasd2824d4f3272
diff --git a/scripts/check-secrets.mjs b/scripts/check-secrets.mjs
index 957860866..d1a2ac024 100644
--- a/scripts/check-secrets.mjs
+++ b/scripts/check-secrets.mjs
@@ -43,6 +43,7 @@ import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
+import { createPrivateKey, createPublicKey } from 'node:crypto';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const argv = process.argv.slice(2);
@@ -70,6 +71,56 @@ function isPublicPem(file) {
}
}
+// Most tools print a Solana secret key as base58, not the 64-byte JSON array
+// the solana-keypair rule above catches, so that form sails straight through.
+// But the repo also carries thousands of real base58 strings that decode to
+// exactly 64 bytes without being a secret at all (transaction signatures are
+// the common case), so length alone is not a signal: decoding a 64-byte
+// ed25519 secret key is a 32-byte seed followed by the 32-byte public key it
+// derives, and nothing else that happens to be 64 bytes satisfies that
+// relationship. Dependency-free (Node stdlib only), same reasoning as
+// api/_lib/oin-verify.js: PKCS#8-wrap the seed, let OpenSSL derive the public
+// key, and compare.
+const B58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
+const B58_INDEX = new Map([...B58_ALPHABET].map((c, i) => [c, i]));
+const ED25519_SEED_PKCS8_PREFIX = Buffer.from('302e020100300506032b657004220420', 'hex');
+
+function base58Decode(str) {
+ const bytes = [0];
+ for (const ch of str) {
+ const val = B58_INDEX.get(ch);
+ if (val === undefined) return null;
+ let carry = val;
+ for (let j = 0; j < bytes.length; j++) {
+ carry += bytes[j] * 58;
+ bytes[j] = carry & 0xff;
+ carry >>= 8;
+ }
+ while (carry > 0) {
+ bytes.push(carry & 0xff);
+ carry >>= 8;
+ }
+ }
+ for (let k = 0; k < str.length && str[k] === '1'; k++) bytes.push(0);
+ return Buffer.from(bytes.reverse());
+}
+
+function isDerivableEd25519SecretKey(value) {
+ const bytes = base58Decode(value);
+ if (!bytes || bytes.length !== 64) return false;
+ const seed = bytes.subarray(0, 32);
+ const claimedPub = bytes.subarray(32, 64);
+ try {
+ const der = Buffer.concat([ED25519_SEED_PKCS8_PREFIX, seed]);
+ const priv = createPrivateKey({ key: der, format: 'der', type: 'pkcs8' });
+ const pubDer = createPublicKey(priv).export({ format: 'der', type: 'spki' });
+ const derivedPub = Buffer.from(pubDer).subarray(-32);
+ return derivedPub.length === 32 && derivedPub.equals(claimedPub);
+ } catch {
+ return false;
+ }
+}
+
// Filename patterns that ARE credential files, whatever they contain. Kept
// separate from the content rules because the answer here does not depend on
// reading the file: nobody commits a private key deliberately.
@@ -185,6 +236,15 @@ const CONTENT_RULES = [
what: 'a Solana keypair serialized as a 64-byte array',
find: /\[\s*(?:\d{1,3}\s*,\s*){63}\d{1,3}\s*\]/,
},
+ {
+ id: 'solana-keypair-base58',
+ what: 'a Solana keypair secret key, base58 encoded (the seed derives the embedded public key)',
+ // Pre-filtered to a maximal run of 86-90 base58 characters (a 64-byte
+ // value's base58 length) before the expensive decode-and-derive check
+ // ever runs, so the scan stays cheap on a tree full of unrelated base58.
+ find: /(?<![1-9A-HJ-NP-Za-km-z])[1-9A-HJ-NP-Za-km-z]{86,90}(?![1-9A-HJ-NP-Za-km-z])/,
+ except: (value) => !isDerivableEd25519SecretKey(value),
+ },
{
id: 'ethereum-private-key',
what: 'a 32-byte hex private key assigned to a key-shaped name',
diff --git a/tests/check-secrets-solana.test.js b/tests/check-secrets-solana.test.js
new file mode 100644
index 000000000..5f586fa54
--- /dev/null
+++ b/tests/check-secrets-solana.test.js
@@ -0,0 +1,81 @@
+// A base58-encoded Solana secret key is how most wallet tools print one, and
+// scripts/check-secrets.mjs only recognized the 64-byte JSON array form until
+// the solana-keypair-base58 rule was added. These tests prove the rule
+// catches a real secret key AND leaves a same-length, same-shape value that
+// is NOT a secret key (a transaction signature) alone.
+//
+// Every secret key here is generated at test runtime and thrown away: none
+// is ever written to a tracked file or a commit.
+
+import { execFileSync, spawnSync } from 'node:child_process';
+import { copyFileSync, mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
+import { generateKeyPairSync, randomBytes } from 'node:crypto';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import bs58 from 'bs58';
+import { describe, expect, it } from 'vitest';
+
+const REPO = join(dirname(fileURLToPath(import.meta.url)), '..');
+
+function scannerRepo() {
+ const root = mkdtempSync(join(tmpdir(), 'threews-secret-scan-sol-'));
+ mkdirSync(join(root, 'scripts'));
+ copyFileSync(join(REPO, 'scripts', 'check-secrets.mjs'), join(root, 'scripts', 'check-secrets.mjs'));
+ writeFileSync(join(root, 'README.md'), '# scanner fixture\n');
+ execFileSync('git', ['init', '-q'], { cwd: root });
+ execFileSync('git', ['config', 'user.email', '[email protected]'], { cwd: root });
+ execFileSync('git', ['config', 'user.name', 'Scanner Test'], { cwd: root });
+ execFileSync('git', ['add', 'README.md', 'scripts/check-secrets.mjs'], { cwd: root });
+ execFileSync('git', ['commit', '-qm', 'fixture'], { cwd: root });
+ return root;
+}
+
+function scanAddedLines(lines) {
+ const root = scannerRepo();
+ writeFileSync(join(root, 'leak.txt'), lines.map((l) => `${l}\n`).join(''));
+ return spawnSync('node', ['scripts/check-secrets.mjs'], { cwd: root, encoding: 'utf8' });
+}
+
+// A freshly generated, never-committed ed25519 keypair in the exact 64-byte
+// (32-byte seed || 32-byte public key) layout solana-keygen and Phantom
+// export, base58 encoded.
+function freshSolanaSecretBase58() {
+ const { privateKey, publicKey } = generateKeyPairSync('ed25519');
+ const seed = Buffer.from(privateKey.export({ format: 'der', type: 'pkcs8' })).subarray(-32);
+ const pub = Buffer.from(publicKey.export({ format: 'der', type: 'spki' })).subarray(-32);
+ return bs58.encode(Buffer.concat([seed, pub]));
+}
+
+// 64 random bytes, base58 encoded: the same length and alphabet as a real
+// secret key, but with no seed/pubkey relationship, exactly the shape of a
+// Solana transaction signature, which the repo carries thousands of.
+function randomSignatureLikeBase58() {
+ return bs58.encode(randomBytes(64));
+}
+
+describe('base58 Solana secret-key guard', () => {
+ it('catches a real secret key planted in a file', () => {
+ const secret = freshSolanaSecretBase58();
+ const result = scanAddedLines([`WALLET_SECRET=${secret}`]);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain('[solana-keypair-base58]');
+ });
+
+ it('does not report a same-length value that is not a derivable keypair', () => {
+ const signature = randomSignatureLikeBase58();
+ const result = scanAddedLines([`tx signature: ${signature}`]);
+ expect(result.status).toBe(0);
+ expect(result.stdout).toContain('OK');
+ });
+
+ it('catches the real secret while leaving a real-shaped signature alone on the same file', () => {
+ const secret = freshSolanaSecretBase58();
+ const signature = randomSignatureLikeBase58();
+ const result = scanAddedLines([`tx signature: ${signature}`, `WALLET_SECRET=${secret}`]);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain('[solana-keypair-base58]');
+ // Only the real secret's line is reported, not the signature's.
+ expect(result.stderr).not.toMatch(new RegExp(`leak\\.txt:1\\s+\\[solana-keypair-base58\\]`));
+ });
+});