@timber-js/oxlint-plugin 0.1.1 → 0.2.1
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/README.md +8 -0
- package/dist/index.js +385 -5
- package/package.json +1 -1
- package/recommended.json +5 -2
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@ Then extend the recommended preset, which loads the plugin and turns on every ru
|
|
|
19
19
|
}
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
A TypeScript `oxlint.config.ts` can import the same file instead (`import timber from '@timber-js/oxlint-plugin/recommended.json' with { type: 'json' }`) and put it in `extends`; the preset loads the plugin by package name, so it works from either.
|
|
23
|
+
|
|
22
24
|
oxlint does not turn on a plugin's rules by itself, and its `extends` takes file paths, not package names, so the preset is referenced through `node_modules`. New rules are added to the preset as they ship.
|
|
23
25
|
|
|
24
26
|
To pick rules yourself instead, load the plugin and name each rule:
|
|
@@ -43,6 +45,12 @@ To pick rules yourself instead, load the plugin and name each rule:
|
|
|
43
45
|
- **`server-fetch-needs-signal`** flags a server-side `fetch` with no `signal`. A stalled upstream holds the response until the render timeout. Fix it with `signal: AbortSignal.timeout(ms)`.
|
|
44
46
|
- **`no-manual-param-parsing`** flags `Number()` / `parseInt()` over raw segment or search params, which accept `1e2`, `""` and out-of-range ids. Give the param a codec in `app/schema.ts` (or `defineSearchParams`), so bad input 404s. A param whose codec already decodes it is not reported.
|
|
45
47
|
|
|
48
|
+
- **`cache-fn-discipline`** flags two mistakes inside a `cache.data` / `cache.component` function:
|
|
49
|
+
- reading request state (`getHeaders()`, `getCookieJar()`, `getSegmentParams()`, `headers()`, `cookies()`). The cache key is the arguments only, so one user's answer is served to everyone. Pass the value in as an argument.
|
|
50
|
+
- a `catch` that returns a value, which caches the failure for the whole TTL. Rethrow instead.
|
|
51
|
+
- **`no-request-derived-origin`** flags a server `fetch` whose target is built from `req.url` or the Host / X-Forwarded-* headers. The client controls those, so a spoofed Host turns the fetch into SSRF. Use a configured origin.
|
|
52
|
+
- **`no-csrf-reimplementation`** flags hand-written `Sec-Fetch-Site` checks and Origin-against-Host comparisons. timber already runs that check on every POST/PUT/PATCH/DELETE before your code. Use `allowedOrigins` for cross-origin writes, and make state-changing GETs POSTs.
|
|
53
|
+
|
|
46
54
|
## Settings
|
|
47
55
|
|
|
48
56
|
The plugin finds your app the way `timber()` does. The root is the nearest directory with a `vite.config.*` or `timber.config.*`, and the app directory is `app/` or `src/app/` under it. You can override either:
|
package/dist/index.js
CHANGED
|
@@ -399,6 +399,277 @@ function isRouteModule(kind) {
|
|
|
399
399
|
return kind === "page" || kind === "layout" || kind === "default" || kind === "denied" || kind === "route" || kind === "metadata-route";
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
+
// src/request.ts
|
|
403
|
+
var REQUEST_APIS = [
|
|
404
|
+
{ name: "getHeaders", sources: ["@timber-js/app/server"] },
|
|
405
|
+
{ name: "getCookieJar", sources: ["@timber-js/app/server"] },
|
|
406
|
+
{ name: "getSegmentParams", sources: ["@timber-js/app/server"] },
|
|
407
|
+
{ name: "headers", sources: ["next/headers"] },
|
|
408
|
+
{ name: "cookies", sources: ["next/headers"] }
|
|
409
|
+
];
|
|
410
|
+
function requestApiCall(context, call, imports) {
|
|
411
|
+
for (const api of REQUEST_APIS) {
|
|
412
|
+
if (isImportedCall(context, call.callee, imports, api.sources, api.name)) return api.name;
|
|
413
|
+
}
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
function isProxyRequestParam(context, param) {
|
|
417
|
+
if (fileInfo(context).kind !== "proxy") return false;
|
|
418
|
+
const fn = param.parent;
|
|
419
|
+
if (fn?.type !== "FunctionDeclaration" && fn?.type !== "FunctionExpression" && fn?.type !== "ArrowFunctionExpression") {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
return fn.params[0] === param && fn.parent?.type === "ExportDefaultDeclaration";
|
|
423
|
+
}
|
|
424
|
+
function annotatedAsRequest(id) {
|
|
425
|
+
const t = id.typeAnnotation?.typeAnnotation;
|
|
426
|
+
return t?.type === "TSTypeReference" && t.typeName.type === "Identifier" && (t.typeName.name === "Request" || t.typeName.name === "NextRequest");
|
|
427
|
+
}
|
|
428
|
+
function isIncomingRequest(context, expr) {
|
|
429
|
+
const e = unwrap(expr);
|
|
430
|
+
if (e.type === "MemberExpression" && !e.computed && e.property.type === "Identifier") {
|
|
431
|
+
return e.property.name === "req";
|
|
432
|
+
}
|
|
433
|
+
if (e.type !== "Identifier") return false;
|
|
434
|
+
const def = findVariable(context, e)?.defs[0];
|
|
435
|
+
if (def?.type !== "Parameter" || def.name.type !== "Identifier") return false;
|
|
436
|
+
return annotatedAsRequest(def.name) || isProxyRequestParam(context, def.name);
|
|
437
|
+
}
|
|
438
|
+
function isRequestHeaders(context, expr, imports, hops = 0) {
|
|
439
|
+
if (hops > 3) return false;
|
|
440
|
+
const e = unwrap(expr);
|
|
441
|
+
if (e.type === "CallExpression") {
|
|
442
|
+
const api = requestApiCall(context, e, imports);
|
|
443
|
+
return api === "getHeaders" || api === "headers";
|
|
444
|
+
}
|
|
445
|
+
if (e.type === "MemberExpression" && !e.computed && e.property.type === "Identifier") {
|
|
446
|
+
return e.property.name === "headers" && isIncomingRequest(context, e.object);
|
|
447
|
+
}
|
|
448
|
+
if (e.type === "Identifier") {
|
|
449
|
+
const def = findVariable(context, e)?.defs[0];
|
|
450
|
+
if (def?.type === "Parameter" && def.name.type === "Identifier") {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
if (def?.type !== "Variable" || def.node.type !== "VariableDeclarator") return false;
|
|
454
|
+
const decl = def.node;
|
|
455
|
+
if (decl.parent.type === "VariableDeclaration" && decl.parent.kind !== "const") return false;
|
|
456
|
+
if (decl.id.type !== "Identifier" || !decl.init) return false;
|
|
457
|
+
return isRequestHeaders(context, decl.init, imports, hops + 1);
|
|
458
|
+
}
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
function requestHeaderRead(context, call, imports) {
|
|
462
|
+
const callee = unwrap(call.callee);
|
|
463
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "get") {
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
const arg = call.arguments[0];
|
|
467
|
+
if (arg?.type !== "Literal" || typeof arg.value !== "string") return null;
|
|
468
|
+
if (!isRequestHeaders(context, callee.object, imports)) return null;
|
|
469
|
+
return arg.value.toLowerCase();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/rules/cache-fn-discipline.ts
|
|
473
|
+
function isFunction(node) {
|
|
474
|
+
return node.type === "FunctionExpression" || node.type === "FunctionDeclaration" || node.type === "ArrowFunctionExpression";
|
|
475
|
+
}
|
|
476
|
+
function cachedMethod(context, call, imports) {
|
|
477
|
+
const callee = unwrap(call.callee);
|
|
478
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "data" && callee.property.name !== "component") {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
const obj = unwrap(callee.object);
|
|
482
|
+
if (obj.type !== "Identifier") return null;
|
|
483
|
+
const binding = imports.get(obj.name);
|
|
484
|
+
if (binding?.source !== "@timber-js/app/cache" || binding.imported !== "cache") return null;
|
|
485
|
+
const declared = findVariable(context, obj)?.defs;
|
|
486
|
+
if (!declared?.some((d) => d.type === "ImportBinding")) return null;
|
|
487
|
+
return `cache.${callee.property.name}`;
|
|
488
|
+
}
|
|
489
|
+
function wrappedFunction(context, arg) {
|
|
490
|
+
const a = unwrap(arg);
|
|
491
|
+
if (isFunction(a)) return a;
|
|
492
|
+
if (a.type !== "Identifier") return null;
|
|
493
|
+
const def = findVariable(context, a)?.defs[0];
|
|
494
|
+
if (!def) return null;
|
|
495
|
+
if (def.type === "FunctionName" && isFunction(def.node)) return def.node;
|
|
496
|
+
if (def.type === "Variable" && def.node.type === "VariableDeclarator" && def.node.init) {
|
|
497
|
+
const init = unwrap(def.node.init);
|
|
498
|
+
return isFunction(init) ? init : null;
|
|
499
|
+
}
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
function isNode(value) {
|
|
503
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
504
|
+
}
|
|
505
|
+
function walk(keys, root, visit, intoFunctions) {
|
|
506
|
+
const stack = [root];
|
|
507
|
+
while (stack.length > 0) {
|
|
508
|
+
const n = stack.pop();
|
|
509
|
+
if (!n) continue;
|
|
510
|
+
visit(n);
|
|
511
|
+
for (const key of keys[n.type] ?? []) {
|
|
512
|
+
const value = Reflect.get(n, key);
|
|
513
|
+
const children = Array.isArray(value) ? value : [value];
|
|
514
|
+
for (const child of children) {
|
|
515
|
+
if (!isNode(child)) continue;
|
|
516
|
+
if (!intoFunctions && isFunction(child)) continue;
|
|
517
|
+
stack.push(child);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
function catchReturnsValue(keys, clause) {
|
|
523
|
+
let returned = null;
|
|
524
|
+
let throws = false;
|
|
525
|
+
walk(
|
|
526
|
+
keys,
|
|
527
|
+
clause.body,
|
|
528
|
+
(n) => {
|
|
529
|
+
if (n.type === "ThrowStatement") throws = true;
|
|
530
|
+
if (n.type === "ReturnStatement" && n.argument && !returned) returned = n;
|
|
531
|
+
},
|
|
532
|
+
false
|
|
533
|
+
);
|
|
534
|
+
return throws ? null : returned;
|
|
535
|
+
}
|
|
536
|
+
var cacheFnDiscipline = {
|
|
537
|
+
meta: {
|
|
538
|
+
type: "problem",
|
|
539
|
+
docs: {
|
|
540
|
+
description: "Disallow request state and swallowed failures inside cache.data / cache.component functions"
|
|
541
|
+
},
|
|
542
|
+
messages: {
|
|
543
|
+
requestApi: "`{{api}}()` inside a `{{method}}` function: the cache key does not include {{what}}, so the result computed from the first caller's {{what}} is cached and served to every user for the TTL. Read it outside and pass it in as an argument (design/06-caching.md).",
|
|
544
|
+
cachedFailure: "This `catch` returns a value from a `{{method}}` function, so the failure is cached for the whole TTL and outlives the upstream's recovery. Rethrow (a failed refresh keeps serving the last good entry) and handle the error at the call site."
|
|
545
|
+
},
|
|
546
|
+
schema: []
|
|
547
|
+
},
|
|
548
|
+
create(context) {
|
|
549
|
+
if (!context.sourceCode.text.includes("@timber-js/app/cache")) return {};
|
|
550
|
+
let imports = /* @__PURE__ */ new Map();
|
|
551
|
+
const what = {
|
|
552
|
+
getHeaders: "headers",
|
|
553
|
+
headers: "headers",
|
|
554
|
+
getCookieJar: "cookies",
|
|
555
|
+
cookies: "cookies",
|
|
556
|
+
getSegmentParams: "route params"
|
|
557
|
+
};
|
|
558
|
+
return {
|
|
559
|
+
Program(node) {
|
|
560
|
+
imports = importBindings(node);
|
|
561
|
+
},
|
|
562
|
+
CallExpression(node) {
|
|
563
|
+
const method = cachedMethod(context, node, imports);
|
|
564
|
+
const arg = node.arguments[0];
|
|
565
|
+
if (!method || !arg || arg.type === "SpreadElement") return;
|
|
566
|
+
const fn = wrappedFunction(context, arg);
|
|
567
|
+
if (!fn?.body) return;
|
|
568
|
+
const keys = context.sourceCode.visitorKeys;
|
|
569
|
+
walk(
|
|
570
|
+
keys,
|
|
571
|
+
fn.body,
|
|
572
|
+
(n) => {
|
|
573
|
+
if (n.type !== "CallExpression") return;
|
|
574
|
+
const api = requestApiCall(context, n, imports);
|
|
575
|
+
if (api === "getSegmentParams" && method === "cache.component") return;
|
|
576
|
+
if (api) {
|
|
577
|
+
context.report({
|
|
578
|
+
node: n,
|
|
579
|
+
messageId: "requestApi",
|
|
580
|
+
data: { api, method, what: what[api] ?? "request state" }
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
true
|
|
585
|
+
);
|
|
586
|
+
walk(
|
|
587
|
+
keys,
|
|
588
|
+
fn.body,
|
|
589
|
+
(n) => {
|
|
590
|
+
if (n.type !== "CatchClause") return;
|
|
591
|
+
const ret = catchReturnsValue(keys, n);
|
|
592
|
+
if (ret) context.report({ node: ret, messageId: "cachedFailure", data: { method } });
|
|
593
|
+
},
|
|
594
|
+
false
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// src/rules/no-csrf-reimplementation.ts
|
|
602
|
+
var MAX_HOPS = 4;
|
|
603
|
+
function headerHeld(context, imports, expr, hops = 0) {
|
|
604
|
+
if (hops > MAX_HOPS) return null;
|
|
605
|
+
const e = unwrap(expr);
|
|
606
|
+
if (e.type === "CallExpression") {
|
|
607
|
+
const direct = requestHeaderRead(context, e, imports);
|
|
608
|
+
return direct;
|
|
609
|
+
}
|
|
610
|
+
if (e.type === "LogicalExpression") {
|
|
611
|
+
return headerHeld(context, imports, e.left, hops + 1);
|
|
612
|
+
}
|
|
613
|
+
if (e.type === "MemberExpression" && !e.computed) {
|
|
614
|
+
const obj = unwrap(e.object);
|
|
615
|
+
if (obj.type === "NewExpression" && obj.arguments[0] && obj.arguments[0].type !== "SpreadElement") {
|
|
616
|
+
return headerHeld(context, imports, obj.arguments[0], hops + 1);
|
|
617
|
+
}
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
if (e.type === "Identifier") {
|
|
621
|
+
const def = findVariable(context, e)?.defs;
|
|
622
|
+
const only = def?.length === 1 ? def[0] : void 0;
|
|
623
|
+
if (only?.type !== "Variable" || only.node.type !== "VariableDeclarator") return null;
|
|
624
|
+
const decl = only.node;
|
|
625
|
+
if (decl.parent.type === "VariableDeclaration" && decl.parent.kind !== "const") return null;
|
|
626
|
+
if (decl.id.type !== "Identifier" || !decl.init) return null;
|
|
627
|
+
return headerHeld(context, imports, decl.init, hops + 1);
|
|
628
|
+
}
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
631
|
+
function holdsHost(context, imports, expr) {
|
|
632
|
+
const h = headerHeld(context, imports, expr);
|
|
633
|
+
return h === "host" || h === "x-forwarded-host";
|
|
634
|
+
}
|
|
635
|
+
var EQUALITY = /* @__PURE__ */ new Set(["===", "!==", "==", "!="]);
|
|
636
|
+
var noCsrfReimplementation = {
|
|
637
|
+
meta: {
|
|
638
|
+
type: "problem",
|
|
639
|
+
docs: {
|
|
640
|
+
description: "Disallow hand-written CSRF checks that duplicate timber's built-in gate"
|
|
641
|
+
},
|
|
642
|
+
messages: {
|
|
643
|
+
secFetchSite: "timber already checks `Sec-Fetch-Site` (then Origin) on every POST/PUT/PATCH/DELETE before proxy.ts or any handler runs. Remove this copy; configure `allowedOrigins` in timber.config.ts if you need cross-origin writes. If this guards a GET that changes state, make it a POST so the real gate covers it (design/13-security.md).",
|
|
644
|
+
originHost: "Comparing Origin against Host re-implements timber's CSRF gate, which already runs on every unsafe-method request before proxy.ts or any handler. Remove this copy; configure `allowedOrigins` in timber.config.ts if you need cross-origin writes (design/13-security.md)."
|
|
645
|
+
},
|
|
646
|
+
schema: []
|
|
647
|
+
},
|
|
648
|
+
create(context) {
|
|
649
|
+
const text = context.sourceCode.text.toLowerCase();
|
|
650
|
+
if (!text.includes("sec-fetch-site") && !text.includes("origin")) return {};
|
|
651
|
+
let imports = /* @__PURE__ */ new Map();
|
|
652
|
+
return {
|
|
653
|
+
Program(node) {
|
|
654
|
+
imports = importBindings(node);
|
|
655
|
+
},
|
|
656
|
+
CallExpression(node) {
|
|
657
|
+
if (requestHeaderRead(context, node, imports) === "sec-fetch-site") {
|
|
658
|
+
context.report({ node, messageId: "secFetchSite" });
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
BinaryExpression(node) {
|
|
662
|
+
if (!EQUALITY.has(node.operator)) return;
|
|
663
|
+
const [a, b] = [node.left, node.right];
|
|
664
|
+
const origin2 = (x) => headerHeld(context, imports, x) === "origin";
|
|
665
|
+
if (origin2(a) && holdsHost(context, imports, b) || origin2(b) && holdsHost(context, imports, a)) {
|
|
666
|
+
context.report({ node, messageId: "originHost" });
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
402
673
|
// src/rules/no-ignored-nextjs-config.ts
|
|
403
674
|
var SEGMENT_CONFIG = /* @__PURE__ */ new Set([
|
|
404
675
|
"dynamic",
|
|
@@ -614,7 +885,7 @@ var SEGMENT_SOURCES = [
|
|
|
614
885
|
var SEARCH_OBJECT_SOURCES = [
|
|
615
886
|
{ name: "useSearchParams", sources: ["next/navigation"] }
|
|
616
887
|
];
|
|
617
|
-
var
|
|
888
|
+
var MAX_HOPS2 = 4;
|
|
618
889
|
var PARAM_SOURCE_TEXT = /[sS]egmentParams|[sS]earchParams/;
|
|
619
890
|
function destructuredKey(pattern, name) {
|
|
620
891
|
if (pattern.type !== "ObjectPattern") return null;
|
|
@@ -633,7 +904,7 @@ function memberName(e) {
|
|
|
633
904
|
return null;
|
|
634
905
|
}
|
|
635
906
|
function origin(context, imports, expr, hops = 0) {
|
|
636
|
-
if (hops >
|
|
907
|
+
if (hops > MAX_HOPS2) return null;
|
|
637
908
|
const e = unwrap(expr);
|
|
638
909
|
const next = (x) => origin(context, imports, x, hops + 1);
|
|
639
910
|
const isCallTo = (call, table) => table.some((t) => isImportedCall(context, call.callee, imports, t.sources, t.name));
|
|
@@ -834,6 +1105,112 @@ function runsOnServer(answer) {
|
|
|
834
1105
|
return answer.env === "server" || answer.env === "shared";
|
|
835
1106
|
}
|
|
836
1107
|
|
|
1108
|
+
// src/rules/no-request-derived-origin.ts
|
|
1109
|
+
var HOST_HEADERS = /* @__PURE__ */ new Set(["host", "x-forwarded-host", "x-forwarded-proto", "forwarded"]);
|
|
1110
|
+
var ORIGIN_PARTS = /* @__PURE__ */ new Set(["url", "origin", "host", "hostname", "href", "protocol"]);
|
|
1111
|
+
var MAX_HOPS3 = 5;
|
|
1112
|
+
var OWN_HOST = /^([a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
1113
|
+
var FIXED_AUTHORITY = /^([a-z][a-z0-9+.-]*:)?\/\/[^/?#]+[/?#]/i;
|
|
1114
|
+
function staticPrefix(e) {
|
|
1115
|
+
const x = unwrap(e);
|
|
1116
|
+
if (x.type === "Literal")
|
|
1117
|
+
return typeof x.value === "string" ? { text: x.value, complete: true } : null;
|
|
1118
|
+
if (x.type === "TemplateLiteral") {
|
|
1119
|
+
return { text: x.quasis[0]?.value.cooked ?? "", complete: x.expressions.length === 0 };
|
|
1120
|
+
}
|
|
1121
|
+
if (x.type === "BinaryExpression" && x.operator === "+") {
|
|
1122
|
+
const left = staticPrefix(x.left);
|
|
1123
|
+
return left ? { text: left.text, complete: false } : null;
|
|
1124
|
+
}
|
|
1125
|
+
return null;
|
|
1126
|
+
}
|
|
1127
|
+
function fixesOwnHost(input) {
|
|
1128
|
+
const prefix = staticPrefix(input);
|
|
1129
|
+
if (!prefix) return false;
|
|
1130
|
+
return prefix.complete ? OWN_HOST.test(prefix.text) : FIXED_AUTHORITY.test(prefix.text);
|
|
1131
|
+
}
|
|
1132
|
+
function requestOrigin(context, imports, expr, hops = 0) {
|
|
1133
|
+
if (hops > MAX_HOPS3) return null;
|
|
1134
|
+
const e = unwrap(expr);
|
|
1135
|
+
const next = (x) => x && x.type !== "SpreadElement" ? requestOrigin(context, imports, x, hops + 1) : null;
|
|
1136
|
+
switch (e.type) {
|
|
1137
|
+
case "MemberExpression": {
|
|
1138
|
+
if (e.computed || e.property.type !== "Identifier") return null;
|
|
1139
|
+
if (e.property.name === "url" && isIncomingRequest(context, e.object)) return "url";
|
|
1140
|
+
return ORIGIN_PARTS.has(e.property.name) ? next(e.object) : null;
|
|
1141
|
+
}
|
|
1142
|
+
case "CallExpression": {
|
|
1143
|
+
const header = requestHeaderRead(context, e, imports);
|
|
1144
|
+
if (header !== null) return HOST_HEADERS.has(header) ? "header" : null;
|
|
1145
|
+
const callee = unwrap(e.callee);
|
|
1146
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "toString") {
|
|
1147
|
+
return next(callee.object);
|
|
1148
|
+
}
|
|
1149
|
+
return null;
|
|
1150
|
+
}
|
|
1151
|
+
case "NewExpression": {
|
|
1152
|
+
if (!isGlobal(context, e.callee, "URL")) return null;
|
|
1153
|
+
const [input, base] = e.arguments;
|
|
1154
|
+
if (input && input.type !== "SpreadElement" && fixesOwnHost(input)) return next(input);
|
|
1155
|
+
return next(base) ?? next(input);
|
|
1156
|
+
}
|
|
1157
|
+
case "TemplateLiteral":
|
|
1158
|
+
if (fixesOwnHost(e)) return null;
|
|
1159
|
+
for (const part of e.expressions) {
|
|
1160
|
+
const r = next(part);
|
|
1161
|
+
if (r) return r;
|
|
1162
|
+
}
|
|
1163
|
+
return null;
|
|
1164
|
+
case "BinaryExpression":
|
|
1165
|
+
if (e.operator !== "+" || fixesOwnHost(e)) return null;
|
|
1166
|
+
return next(e.left) ?? next(e.right);
|
|
1167
|
+
case "LogicalExpression":
|
|
1168
|
+
return next(e.left) ?? next(e.right);
|
|
1169
|
+
case "Identifier": {
|
|
1170
|
+
const def = findVariable(context, e)?.defs;
|
|
1171
|
+
const only = def?.length === 1 ? def[0] : void 0;
|
|
1172
|
+
if (only?.type !== "Variable" || only.node.type !== "VariableDeclarator") return null;
|
|
1173
|
+
const decl = only.node;
|
|
1174
|
+
if (decl.parent.type === "VariableDeclaration" && decl.parent.kind !== "const") return null;
|
|
1175
|
+
if (decl.id.type !== "Identifier" || !decl.init) return null;
|
|
1176
|
+
return next(decl.init);
|
|
1177
|
+
}
|
|
1178
|
+
default:
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
var noRequestDerivedOrigin = {
|
|
1183
|
+
meta: {
|
|
1184
|
+
type: "problem",
|
|
1185
|
+
docs: {
|
|
1186
|
+
description: "Disallow server fetches whose target comes from the request's URL or Host header"
|
|
1187
|
+
},
|
|
1188
|
+
messages: {
|
|
1189
|
+
url: "This fetch target is built from the request's URL, whose origin comes from the client-controlled Host header: a spoofed Host makes the server fetch an attacker's origin (SSRF) and serve the result as this site's (cache poisoning). Use a configured origin (an env var) or bundle the asset at build time (design/13-security.md).",
|
|
1190
|
+
header: "This fetch target is built from a Host / X-Forwarded-* header, which the client controls: a spoofed value makes the server fetch an attacker's origin (SSRF). Use a configured origin (an env var) instead (design/13-security.md)."
|
|
1191
|
+
},
|
|
1192
|
+
schema: []
|
|
1193
|
+
},
|
|
1194
|
+
create(context) {
|
|
1195
|
+
if (!context.sourceCode.text.includes("fetch")) return {};
|
|
1196
|
+
let imports = /* @__PURE__ */ new Map();
|
|
1197
|
+
let onServer = false;
|
|
1198
|
+
return {
|
|
1199
|
+
Program(node) {
|
|
1200
|
+
imports = importBindings(node);
|
|
1201
|
+
onServer = runsOnServer(moduleEnv(context, node));
|
|
1202
|
+
},
|
|
1203
|
+
CallExpression(node) {
|
|
1204
|
+
if (!onServer || !isGlobal(context, node.callee, "fetch")) return;
|
|
1205
|
+
const input = node.arguments[0];
|
|
1206
|
+
if (!input || input.type === "SpreadElement") return;
|
|
1207
|
+
const reason = requestOrigin(context, imports, input);
|
|
1208
|
+
if (reason) context.report({ node: input, messageId: reason });
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
|
|
837
1214
|
// src/rules/server-fetch-needs-signal.ts
|
|
838
1215
|
function initLacksSignal(init) {
|
|
839
1216
|
if (init === void 0) return true;
|
|
@@ -844,7 +1221,7 @@ function initLacksSignal(init) {
|
|
|
844
1221
|
(p) => p.type === "Property" && propertyKeyName(p) !== "signal" && propertyKeyName(p) !== null
|
|
845
1222
|
);
|
|
846
1223
|
}
|
|
847
|
-
var
|
|
1224
|
+
var MAX_HOPS4 = 4;
|
|
848
1225
|
function annotatedAsUrl(id) {
|
|
849
1226
|
const t = id.typeAnnotation?.typeAnnotation;
|
|
850
1227
|
if (!t) return false;
|
|
@@ -852,7 +1229,7 @@ function annotatedAsUrl(id) {
|
|
|
852
1229
|
return t.type === "TSTypeReference" && t.typeName.type === "Identifier" && t.typeName.name === "URL";
|
|
853
1230
|
}
|
|
854
1231
|
function isUrlInput(context, input, hops = 0) {
|
|
855
|
-
if (hops >
|
|
1232
|
+
if (hops > MAX_HOPS4) return false;
|
|
856
1233
|
const e = unwrap(input);
|
|
857
1234
|
switch (e.type) {
|
|
858
1235
|
case "Literal":
|
|
@@ -923,7 +1300,10 @@ var serverFetchNeedsSignal = {
|
|
|
923
1300
|
var rules = {
|
|
924
1301
|
"no-ignored-nextjs-config": noIgnoredNextjsConfig,
|
|
925
1302
|
"server-fetch-needs-signal": serverFetchNeedsSignal,
|
|
926
|
-
"no-manual-param-parsing": noManualParamParsing
|
|
1303
|
+
"no-manual-param-parsing": noManualParamParsing,
|
|
1304
|
+
"cache-fn-discipline": cacheFnDiscipline,
|
|
1305
|
+
"no-request-derived-origin": noRequestDerivedOrigin,
|
|
1306
|
+
"no-csrf-reimplementation": noCsrfReimplementation
|
|
927
1307
|
};
|
|
928
1308
|
var plugin = {
|
|
929
1309
|
meta: { name: "timber" },
|
package/package.json
CHANGED
package/recommended.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
-
"jsPlugins": ["
|
|
2
|
+
"jsPlugins": ["@timber-js/oxlint-plugin"],
|
|
3
3
|
"rules": {
|
|
4
4
|
"timber/no-ignored-nextjs-config": "error",
|
|
5
5
|
"timber/server-fetch-needs-signal": "error",
|
|
6
|
-
"timber/no-manual-param-parsing": "error"
|
|
6
|
+
"timber/no-manual-param-parsing": "error",
|
|
7
|
+
"timber/cache-fn-discipline": "error",
|
|
8
|
+
"timber/no-request-derived-origin": "error",
|
|
9
|
+
"timber/no-csrf-reimplementation": "error"
|
|
7
10
|
}
|
|
8
11
|
}
|