サンプル
hono 4.13.7: cookie
検証済みサンプル — npm hono 4.13.7: cookie. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: hono/cookie exports getCookie, setCookie, deleteCookie…
sha256:e2a6bcaf7fb4c6e94510860c4261719f49712213180ede86a520952dfe1b37fb
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 1
- ビルドした署名鍵
- 1
宣言された環境
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2… |
2026-09-06 |
ケース
HOW- ゴール
- verify pkg:npm/hono@4.13.7
- パッケージ
- シンボル
-
- hono/cookie
- 環境
- node 22.23.2
- 作成日
- 2026-09-06T03:39:34Z
コントラクト
- hono/cookie exports getCookie, setCookie, deleteCookie, getSignedCookie, and setSignedCookie functions
- getCookie extracts single cookie value and all cookies from request Cookie header
- setCookie sets Set-Cookie response header with name, value, and cookie options
- deleteCookie sets Set-Cookie response header with Max-Age 0 to expire cookie
- setSignedCookie generates HMAC signed cookie and getSignedCookie verifies valid signature
- getSignedCookie rejects tampered or invalid signature cookie returning false
ファイル
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.js
- test/contract.mjs
ソース
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: verify pkg:npm/hono@4.13.7
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/hono@4.13.7
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- packageManager: npm@10.9.8
- runtime: node@22.23.2
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:5a4e7cc2df2f73d598307c4c0089b7dcb2ce2b6f359b5de8aeadc81619a3d93a","constraints":{"executionContext":"node"},"contract":["hono/cookie exports getCookie, setCookie, deleteCookie, getSignedCookie, and setSignedCookie functions","getCookie extracts single cookie value and all cookies from request Cookie header","setCookie sets Set-Cookie response header with name, value, and cookie options","deleteCookie sets Set-Cookie response header with Max-Age 0 to expire cookie","setSignedCookie generates HMAC signed cookie and getSignedCookie verifies valid signature","getSignedCookie rejects tampered or invalid signature cookie returning false"],"goal":"verify pkg:npm/hono@4.13.7","kind":"HOW","packages":["pkg:npm/hono@4.13.7"],"schemaVersion":1,"symbols":["hono/cookie"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/hono@4.13.7"],"schemaVersion":1,"subject":"pkg:npm/hono@4.13.7","symbols":["hono/cookie"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-hono-cookie",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-hono-cookie",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"hono": "4.13.7"
}
},
"node_modules/hono": {
"version": "4.13.7",
"resolved": "https://registry.npmjs.org/hono/-/hono-4.13.7.tgz",
"integrity": "sha512-c8/gF9ac8Y78/agExVocyLevgR+JlpNB444Py0FSX8pJoPdYUfUzRcXtYEYGwt6l19qIlVZPN5Mfsw9jFShmQQ==",
"license": "MIT",
"engines": {
"node": ">=16.9.0"
}
}
}
}
{
"name": "sample-hono-cookie",
"version": "1.0.0",
"private": true,
"description": "Clean-room verification of hono/cookie in pkg:npm/hono@4.13.7",
"main": "src/index.js",
"dependencies": {
"hono": "4.13.7"
}
}
{
"schemaVersion": 1,
"goal": "verify pkg:npm/hono@4.13.7",
"kind": "HOW",
"packages": [
"pkg:npm/hono@4.13.7"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
const { Hono } = require('hono');
const {
getCookie,
setCookie,
deleteCookie,
getSignedCookie,
setSignedCookie,
} = require('hono/cookie');
/**
* Creates and configures a Hono application with cookie handling routes.
* @param {string} cookieSecret - Secret key used for signing cookies.
* @returns {Hono} Configured Hono application.
*/
function createApp(cookieSecret = 'contract-test-secret-key-32chars') {
const app = new Hono();
// Route: Read cookies using getCookie
app.get('/api/cookie/read', (c) => {
const session = getCookie(c, 'sessionId');
const all = getCookie(c);
return c.json({ session, all });
});
// Route: Set cookie using setCookie with options
app.post('/api/cookie/set', (c) => {
setCookie(c, 'theme', 'dark', {
path: '/',
secure: true,
httpOnly: true,
maxAge: 3600,
sameSite: 'Strict',
});
return c.json({ success: true });
});
// Route: Delete cookie using deleteCookie
app.post('/api/cookie/delete', (c) => {
deleteCookie(c, 'sessionId', {
path: '/',
secure: true,
});
return c.json({ success: true });
});
// Route: Set signed cookie using setSignedCookie
app.post('/api/cookie/sign', async (c) => {
await setSignedCookie(c, 'authToken', 'user-payload-12345', cookieSecret, {
path: '/',
httpOnly: true,
maxAge: 7200,
sameSite: 'Lax',
});
return c.json({ success: true });
});
// Route: Verify signed cookie using getSignedCookie
app.get('/api/cookie/verify', async (c) => {
const verifiedToken = await getSignedCookie(c, cookieSecret, 'authToken');
return c.json({ authToken: verifiedToken });
});
return app;
}
module.exports = {
createApp,
getCookie,
setCookie,
deleteCookie,
getSignedCookie,
setSignedCookie,
};
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const {
createApp,
getCookie,
setCookie,
deleteCookie,
getSignedCookie,
setSignedCookie,
} = require('../src/index.js');
const SECRET = 'contract-test-secret-key-32chars';
// Contract 1: hono/cookie exports getCookie, setCookie, deleteCookie, getSignedCookie, and setSignedCookie functions
{
assert.strictEqual(typeof getCookie, 'function', 'getCookie must be a function');
assert.strictEqual(typeof setCookie, 'function', 'setCookie must be a function');
assert.strictEqual(typeof deleteCookie, 'function', 'deleteCookie must be a function');
assert.strictEqual(typeof getSignedCookie, 'function', 'getSignedCookie must be a function');
assert.strictEqual(typeof setSignedCookie, 'function', 'setSignedCookie must be a function');
// Verify pinned package version
const honoEntryUrl = import.meta.resolve('hono');
const pkgJsonPath = fileURLToPath(new URL('../package.json', honoEntryUrl));
assert.ok(fs.existsSync(pkgJsonPath));
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
assert.strictEqual(pkgJson.name, 'hono');
assert.strictEqual(pkgJson.version, '4.13.7');
assert.ok(pkgJson.exports['./cookie'], 'hono must export ./cookie subpath');
}
// Contract 2: getCookie extracts single cookie value and all cookies from request Cookie header
{
const app = createApp(SECRET);
const req = new Request('http://localhost/api/cookie/read', {
headers: {
Cookie: 'sessionId=sess-abc123xyz; theme=dark; pref=compact',
},
});
const res = await app.request(req);
assert.strictEqual(res.status, 200, 'GET /api/cookie/read returns HTTP 200');
const data = await res.json();
assert.strictEqual(data.session, 'sess-abc123xyz', 'getCookie returns specified cookie value');
assert.deepStrictEqual(data.all, {
sessionId: 'sess-abc123xyz',
theme: 'dark',
pref: 'compact',
}, 'getCookie without key returns all parsed cookies');
}
// Contract 3: setCookie sets Set-Cookie response header with name, value, and cookie options
{
const app = createApp(SECRET);
const req = new Request('http://localhost/api/cookie/set', {
method: 'POST',
});
const res = await app.request(req);
assert.strictEqual(res.status, 200, 'POST /api/cookie/set returns HTTP 200');
const setCookieHeader = res.headers.get('set-cookie');
assert.ok(setCookieHeader, 'Set-Cookie header must be present');
assert.ok(setCookieHeader.includes('theme=dark'), 'Set-Cookie includes cookie name and value');
assert.ok(setCookieHeader.includes('Path=/'), 'Set-Cookie includes Path option');
assert.ok(setCookieHeader.includes('Max-Age=3600'), 'Set-Cookie includes Max-Age option');
assert.ok(setCookieHeader.includes('HttpOnly'), 'Set-Cookie includes HttpOnly flag');
assert.ok(setCookieHeader.includes('Secure'), 'Set-Cookie includes Secure flag');
assert.ok(setCookieHeader.includes('SameSite=Strict'), 'Set-Cookie includes SameSite option');
}
// Contract 4: deleteCookie sets Set-Cookie response header with Max-Age 0 to expire cookie
{
const app = createApp(SECRET);
const req = new Request('http://localhost/api/cookie/delete', {
method: 'POST',
});
const res = await app.request(req);
assert.strictEqual(res.status, 200, 'POST /api/cookie/delete returns HTTP 200');
const setCookieHeader = res.headers.get('set-cookie');
assert.ok(setCookieHeader, 'Set-Cookie header must be present');
assert.ok(setCookieHeader.includes('sessionId='), 'Set-Cookie includes cookie name');
assert.ok(setCookieHeader.includes('Max-Age=0'), 'Set-Cookie includes Max-Age=0 to expire cookie');
assert.ok(setCookieHeader.includes('Path=/'), 'Set-Cookie includes Path option');
}
// Contract 5: setSignedCookie generates HMAC signed cookie and getSignedCookie verifies valid signature
{
const app = createApp(SECRET);
// 1. Generate signed cookie
const signReq = new Request('http://localhost/api/cookie/sign', {
method: 'POST',
});
const signRes = await app.request(signReq);
assert.strictEqual(signRes.status, 200, 'POST /api/cookie/sign returns HTTP 200');
const setCookieHeader = signRes.headers.get('set-cookie');
assert.ok(setCookieHeader, 'Set-Cookie header must be present');
assert.ok(setCookieHeader.includes('authToken='), 'Set-Cookie includes signed cookie name');
assert.ok(setCookieHeader.includes('HttpOnly'), 'Set-Cookie includes HttpOnly');
// Extract raw cookie string (name=value)
const rawCookie = setCookieHeader.split(';')[0];
assert.ok(rawCookie.includes('.'), 'Signed cookie value must contain signature separator');
// 2. Verify with valid secret
const verifyReq = new Request('http://localhost/api/cookie/verify', {
headers: {
Cookie: rawCookie,
},
});
const verifyRes = await app.request(verifyReq);
assert.strictEqual(verifyRes.status, 200, 'GET /api/cookie/verify returns HTTP 200');
const verifyData = await verifyRes.json();
assert.strictEqual(verifyData.authToken, 'user-payload-12345', 'getSignedCookie returns verified payload');
}
// Contract 6: getSignedCookie rejects tampered or invalid signature cookie returning false
{
const validApp = createApp(SECRET);
const signRes = await validApp.request(new Request('http://localhost/api/cookie/sign', { method: 'POST' }));
const rawCookie = signRes.headers.get('set-cookie').split(';')[0];
// Verify with wrong secret -> should return false
const wrongSecretApp = createApp('different-wrong-secret-key-999');
const wrongRes = await wrongSecretApp.request(new Request('http://localhost/api/cookie/verify', {
headers: { Cookie: rawCookie },
}));
assert.strictEqual(wrongRes.status, 200);
const wrongData = await wrongRes.json();
assert.strictEqual(wrongData.authToken, false, 'getSignedCookie returns false when secret does not match');
// Verify with tampered value -> should return false
const tamperedCookie = rawCookie.replace('user-payload-12345', 'tampered-payload-99999');
const tamperedRes = await validApp.request(new Request('http://localhost/api/cookie/verify', {
headers: { Cookie: tamperedCookie },
}));
assert.strictEqual(tamperedRes.status, 200);
const tamperedData = await tamperedRes.json();
assert.strictEqual(tamperedData.authToken, false, 'getSignedCookie returns false when cookie payload was tampered');
}
console.log('CONTRACT PASS: All hono/cookie contract assertions passed.');
オリジンシーダー
匿名