CodeSampleX

示例

proxy-addr 2.0.7

已验证示例 — npm proxy-addr 2.0.7. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert proxyaddr returns socket remoteAddress when direct connection has…

sha256:bdbf16a5f315286afbef9ebc069823472ca30b4bfdfa776392ccda7bb307ef41

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

案例

HOW
目标
verify pkg:npm/proxy-addr@2.0.7
包
创建时间
2026-08-20T13:57:20Z

契约

  1. assert proxyaddr returns socket remoteAddress when direct connection has no proxy headers
  2. assert proxyaddr returns socket remoteAddress when proxy is untrusted
  3. assert proxyaddr returns leftmost client address when trusting all proxies
  4. assert proxyaddr filters local reverse proxy address when using loopback trust range
  5. assert proxyaddr filters private network proxy addresses when using uniquelocal trust range
  6. assert proxyaddr filters multiple trusted hops when configured with array of subnets
  7. assert proxyaddr evaluates addresses with custom predicate trust function
  8. assert proxyaddr supports netmask string notation in subnet trust list
  9. assert proxyaddr.all returns all parsed addresses in chain when trust argument is omitted
  10. assert proxyaddr.all stops at first untrusted address when trust argument is provided
  11. assert proxyaddr.compile creates reusable trust filter function for IP ranges
  12. assert proxyaddr throws TypeError when required request or trust argument is missing

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
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/proxy-addr@2.0.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/proxy-addr@2.0.7

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.
csx.json
{"case":{"caseId":"case:sha256:5027a0a75c542a3db2bd8cb50b949dee841272dc247643e2b75ac487a033169b","contract":["assert proxyaddr returns socket remoteAddress when direct connection has no proxy headers","assert proxyaddr returns socket remoteAddress when proxy is untrusted","assert proxyaddr returns leftmost client address when trusting all proxies","assert proxyaddr filters local reverse proxy address when using loopback trust range","assert proxyaddr filters private network proxy addresses when using uniquelocal trust range","assert proxyaddr filters multiple trusted hops when configured with array of subnets","assert proxyaddr evaluates addresses with custom predicate trust function","assert proxyaddr supports netmask string notation in subnet trust list","assert proxyaddr.all returns all parsed addresses in chain when trust argument is omitted","assert proxyaddr.all stops at first untrusted address when trust argument is provided","assert proxyaddr.compile creates reusable trust filter function for IP ranges","assert proxyaddr throws TypeError when required request or trust argument is missing"],"goal":"verify pkg:npm/proxy-addr@2.0.7","kind":"HOW","packages":["pkg:npm/proxy-addr@2.0.7"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/proxy-addr@2.0.7"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
index.mjs
import proxyaddr from 'proxy-addr';

/**
 * Resolves the client IP address from a request using trusted proxy settings.
 *
 * @param {Object} req - The HTTP request object.
 * @param {Function|Array|String} trust - The trust configuration (e.g. 'loopback', CIDR, or predicate function).
 * @returns {string} The determined client IP address.
 */
export function getClientIp(req, trust = 'loopback') {
  return proxyaddr(req, trust);
}

/**
 * Returns all parsed addresses from the request chain.
 *
 * @param {Object} req - The HTTP request object.
 * @param {Function|Array|String} [trust] - Optional trust configuration.
 * @returns {string[]} Array of addresses.
 */
export function getAllIps(req, trust) {
  return proxyaddr.all(req, trust);
}

export default proxyaddr;
package-lock.json
{
  "name": "sample-proxy-addr",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-proxy-addr",
      "version": "1.0.0",
      "dependencies": {
        "proxy-addr": "2.0.7"
      }
    },
    "node_modules/forwarded": {
      "version": "0.2.0",
      "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
      "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.6"
      }
    },
    "node_modules/ipaddr.js": {
      "version": "1.9.1",
      "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
      "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.10"
      }
    },
    "node_modules/proxy-addr": {
      "version": "2.0.7",
      "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
      "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==",
      "license": "MIT",
      "dependencies": {
        "forwarded": "0.2.0",
        "ipaddr.js": "1.9.1"
      },
      "engines": {
        "node": ">= 0.10"
      }
    }
  }
}
package.json
{
  "name": "sample-proxy-addr",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "proxy-addr": "2.0.7"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/proxy-addr@2.0.7",
  "kind": "HOW",
  "packages": [
    "pkg:npm/proxy-addr@2.0.7"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import proxyaddr from 'proxy-addr';
import { getClientIp, getAllIps } from '../index.mjs';

function createReq(socketIp, forwardedFor) {
  const headers = {};
  if (forwardedFor !== undefined) {
    headers['x-forwarded-for'] = forwardedFor;
  }
  return {
    socket: { remoteAddress: socketIp },
    headers
  };
}

// 1. Direct connection without proxy headers returns socket address
{
  const req = createReq('203.0.113.195');
  assert.equal(getClientIp(req, () => false), '203.0.113.195');
}

// 2. Untrusted proxy returns direct socket address when trust function returns false
{
  const req = createReq('198.51.100.1', '203.0.113.195');
  assert.equal(getClientIp(req, () => false), '198.51.100.1');
}

// 3. Trust all returns the leftmost original client address
{
  const req = createReq('10.0.0.1', '203.0.113.195, 10.0.0.2');
  assert.equal(getClientIp(req, () => true), '203.0.113.195');
}

// 4. Predefined loopback range filters local reverse proxy address
{
  const req = createReq('127.0.0.1', '203.0.113.195');
  assert.equal(getClientIp(req, 'loopback'), '203.0.113.195');
}

// 5. Predefined uniquelocal range filters private network proxy addresses
{
  const req = createReq('10.0.0.1', '203.0.113.195, 192.168.1.1');
  assert.equal(getClientIp(req, 'uniquelocal'), '203.0.113.195');
}

// 6. Array of subnets filters multiple trusted hops
{
  const req = createReq('127.0.0.1', '203.0.113.195, 10.0.0.1');
  assert.equal(getClientIp(req, ['loopback', '10.0.0.0/8']), '203.0.113.195');
}

// 7. Custom predicate function evaluates addresses and indices
{
  const req = createReq('192.168.1.1', '203.0.113.195, 192.168.1.2');
  const isInternal = (addr) => addr.startsWith('192.168.');
  assert.equal(getClientIp(req, isInternal), '203.0.113.195');
}

// 8. Netmask string notation in trust list
{
  const req = createReq('10.0.0.1', '203.0.113.195, 192.168.1.5');
  assert.equal(getClientIp(req, ['10.0.0.0/255.0.0.0', '192.168.0.0/255.255.0.0']), '203.0.113.195');
}

// 9. getAllIps returns all parsed addresses when trust is omitted
{
  const req = createReq('127.0.0.1', '203.0.113.195, 10.0.0.1');
  assert.deepEqual(getAllIps(req), ['127.0.0.1', '10.0.0.1', '203.0.113.195']);
}

// 10. getAllIps stops at the first untrusted address when trust is provided
{
  const req = createReq('127.0.0.1', '203.0.113.195, 10.0.0.1');
  assert.deepEqual(getAllIps(req, 'loopback'), ['127.0.0.1', '10.0.0.1']);
}

// 11. proxyaddr.compile creates reusable trust filter function
{
  const trustLoopback = proxyaddr.compile('loopback');
  assert.equal(typeof trustLoopback, 'function');
  assert.equal(trustLoopback('127.0.0.1', 0), true);
  assert.equal(trustLoopback('::1', 0), true);
  assert.equal(trustLoopback('203.0.113.195', 0), false);
}

// 12. Throws TypeError when required arguments are missing
{
  assert.throws(() => proxyaddr(null, 'loopback'), /^TypeError: req argument is required$/);
  assert.throws(() => proxyaddr(createReq('127.0.0.1')), /^TypeError: trust argument is required$/);
}

原始种子者

匿名