periderm-cli 0.1.30 → 0.1.32
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/dist/index.js +3 -1
- package/dist/scanner/checks.js +260 -0
- package/dist/scanner/repo-checks.js +197 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ const program = new Command();
|
|
|
31
31
|
program
|
|
32
32
|
.name("periderm")
|
|
33
33
|
.description("A pre-launch checklist.")
|
|
34
|
-
.version("0.1.
|
|
34
|
+
.version("0.1.32");
|
|
35
35
|
// Easter egg: `periderm bless` — hidden, undocumented
|
|
36
36
|
program
|
|
37
37
|
.command("bless", { hidden: true })
|
|
@@ -155,6 +155,8 @@ program
|
|
|
155
155
|
await fs.writeFile(path.join(outDir, "last-report.md"), result.markdown, "utf8");
|
|
156
156
|
await fs.writeFile(path.join(outDir, "last-report.json"), JSON.stringify(result, null, 2), "utf8");
|
|
157
157
|
await fs.writeFile(path.join(outDir, "ai-instructions.md"), getAiInstructions(), "utf8");
|
|
158
|
+
// Ignore all files in the .periderm folder to prevent leaking vulnerabilities
|
|
159
|
+
await fs.writeFile(path.join(outDir, ".gitignore"), "*\n", "utf8");
|
|
158
160
|
if (!opts.localOnly) {
|
|
159
161
|
const cfg = readConfig();
|
|
160
162
|
if (!cfg.token) {
|
package/dist/scanner/checks.js
CHANGED
|
@@ -693,6 +693,259 @@ const dbPollingAbuse = (source, file) => {
|
|
|
693
693
|
return out;
|
|
694
694
|
};
|
|
695
695
|
// ───────────────────────────────────────────────────────────────────────
|
|
696
|
+
// Helpers for scope-aware checks
|
|
697
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
698
|
+
/**
|
|
699
|
+
* True when a file is part of an internal / staff-only surface where full
|
|
700
|
+
* WCAG conformance is a nice-to-have, not a launch blocker. We intentionally
|
|
701
|
+
* skip a11y-only checks for these paths.
|
|
702
|
+
*/
|
|
703
|
+
function isInternalSurface(file) {
|
|
704
|
+
return /(?:^|[\\/])(_?admin|internal|backoffice|cms|staff|superuser|maintenance|debug|dev-tools?)(?:[\\/]|[._-]|$)/i.test(file);
|
|
705
|
+
}
|
|
706
|
+
/** True when the file is a route/page/component (not config, script, worker). */
|
|
707
|
+
function looksLikeUiFile(file) {
|
|
708
|
+
if (!/\.(tsx|jsx)$/.test(file))
|
|
709
|
+
return false;
|
|
710
|
+
if (/\/(scripts|migrations|workers?|jobs?|cron)\//.test(file))
|
|
711
|
+
return false;
|
|
712
|
+
if (/\.server\.[jt]sx?$/.test(file))
|
|
713
|
+
return false;
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
717
|
+
// 22. Icon-only button/link with no accessible name
|
|
718
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
719
|
+
const iconButtonNoLabel = (source, file) => {
|
|
720
|
+
const out = [];
|
|
721
|
+
if (!looksLikeUiFile(file))
|
|
722
|
+
return out;
|
|
723
|
+
if (isInternalSurface(file))
|
|
724
|
+
return out;
|
|
725
|
+
// <button ...> <SomeIcon .../> </button> with nothing else inside
|
|
726
|
+
const re = /<(button|a)\b([^>]*?)>\s*(<[A-Z][A-Za-z0-9]*\b[^>]*\/?>)\s*<\/\1>/g;
|
|
727
|
+
let m;
|
|
728
|
+
let reported = 0;
|
|
729
|
+
while ((m = re.exec(source)) && reported < 3) {
|
|
730
|
+
const attrs = m[2];
|
|
731
|
+
if (/aria-label\s*=|aria-labelledby\s*=|title\s*=/.test(attrs))
|
|
732
|
+
continue;
|
|
733
|
+
// ignore hidden / decorative wrappers
|
|
734
|
+
if (/aria-hidden\s*=\s*["']?true/.test(attrs))
|
|
735
|
+
continue;
|
|
736
|
+
out.push(mkFinding({
|
|
737
|
+
id: "icon-only-no-label",
|
|
738
|
+
category: "Accessibility",
|
|
739
|
+
severity: "high",
|
|
740
|
+
file, line: lineOf(source, m.index),
|
|
741
|
+
message: `Icon-only <${m[1]}> has no accessible name.`,
|
|
742
|
+
why: "Screen readers announce this control as 'button' with no label. Keyboard and assistive-tech users can't tell what it does. WCAG 2.1 SC 4.1.2 (Name, Role, Value).",
|
|
743
|
+
fix: "Add aria-label=\"...\" (or wrap the icon with a visually-hidden <span className=\"sr-only\">Description</span>).",
|
|
744
|
+
aiPrompt: `In ${file} near line ${lineOf(source, m.index)}, an icon-only <${m[1]}> has no accessible text. Infer intent from surrounding code and add aria-label (or an sr-only span) describing the action.`,
|
|
745
|
+
}));
|
|
746
|
+
reported++;
|
|
747
|
+
}
|
|
748
|
+
return out;
|
|
749
|
+
};
|
|
750
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
751
|
+
// 23. Form input with no label / aria-label
|
|
752
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
753
|
+
const formInputNoLabel = (source, file) => {
|
|
754
|
+
const out = [];
|
|
755
|
+
if (!looksLikeUiFile(file))
|
|
756
|
+
return out;
|
|
757
|
+
if (isInternalSurface(file))
|
|
758
|
+
return out;
|
|
759
|
+
const re = /<(input|select|textarea)\b([^>]*?)\/?>/g;
|
|
760
|
+
let m;
|
|
761
|
+
let reported = 0;
|
|
762
|
+
while ((m = re.exec(source)) && reported < 3) {
|
|
763
|
+
const tag = m[1];
|
|
764
|
+
const attrs = m[2];
|
|
765
|
+
if (/type\s*=\s*["'](hidden|submit|button|image|reset)["']/.test(attrs))
|
|
766
|
+
continue;
|
|
767
|
+
if (/aria-label\s*=|aria-labelledby\s*=/.test(attrs))
|
|
768
|
+
continue;
|
|
769
|
+
// is there a <label htmlFor="X"> earlier in the file with a matching id?
|
|
770
|
+
const idMatch = attrs.match(/\bid\s*=\s*["']([^"']+)["']/);
|
|
771
|
+
if (idMatch) {
|
|
772
|
+
const labelRe = new RegExp(`<label\\b[^>]*\\bhtmlFor\\s*=\\s*["']${idMatch[1]}["']`);
|
|
773
|
+
if (labelRe.test(source))
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
// wrapping <label> ... <input .../> ... </label>
|
|
777
|
+
const before = source.slice(Math.max(0, m.index - 400), m.index);
|
|
778
|
+
const after = source.slice((m.index + m[0].length), m.index + m[0].length + 300);
|
|
779
|
+
if (/<label\b[^>]*>[^<]*$/.test(before) && /^[^<]*<\/label>/.test(after))
|
|
780
|
+
continue;
|
|
781
|
+
out.push(mkFinding({
|
|
782
|
+
id: "input-no-label",
|
|
783
|
+
category: "Accessibility",
|
|
784
|
+
severity: "high",
|
|
785
|
+
file, line: lineOf(source, m.index),
|
|
786
|
+
message: `<${tag}> has no associated <label> or aria-label.`,
|
|
787
|
+
why: "Screen readers announce the field with no name. Users have no idea what to type. Violates WCAG 2.1 SC 1.3.1 & 3.3.2.",
|
|
788
|
+
fix: "Either wrap the field in a <label>, add htmlFor={id}, or set aria-label=\"...\" on the field itself.",
|
|
789
|
+
aiPrompt: `In ${file} near line ${lineOf(source, m.index)}, the <${tag}> is missing a label. Add a <label htmlFor="..."> or aria-label describing what the user should enter.`,
|
|
790
|
+
}));
|
|
791
|
+
reported++;
|
|
792
|
+
}
|
|
793
|
+
return out;
|
|
794
|
+
};
|
|
795
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
796
|
+
// 24. tabIndex={n>0} — breaks natural tab order
|
|
797
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
798
|
+
const tabIndexPositive = (source, file) => {
|
|
799
|
+
const out = [];
|
|
800
|
+
if (!looksLikeUiFile(file))
|
|
801
|
+
return out;
|
|
802
|
+
if (isInternalSurface(file))
|
|
803
|
+
return out;
|
|
804
|
+
const re = /tabIndex\s*=\s*\{?\s*(\d+)\s*\}?/g;
|
|
805
|
+
let m;
|
|
806
|
+
while ((m = re.exec(source))) {
|
|
807
|
+
const val = parseInt(m[1], 10);
|
|
808
|
+
if (val > 0) {
|
|
809
|
+
out.push(mkFinding({
|
|
810
|
+
id: "positive-tabindex",
|
|
811
|
+
category: "Accessibility",
|
|
812
|
+
severity: "medium",
|
|
813
|
+
file, line: lineOf(source, m.index),
|
|
814
|
+
message: `tabIndex={${val}} forces a manual tab order.`,
|
|
815
|
+
why: "Any positive tabIndex hijacks the natural document order. Keyboard users end up on random elements first, and it desyncs the moment new fields are added. WCAG 2.4.3 (Focus Order).",
|
|
816
|
+
fix: "Use tabIndex={0} to make an element focusable, or tabIndex={-1} for programmatic focus. Never a positive value.",
|
|
817
|
+
aiPrompt: `In ${file} near line ${lineOf(source, m.index)}, replace tabIndex={${val}} with tabIndex={0} (focusable in DOM order) or restructure the DOM so the natural order matches the intended focus flow.`,
|
|
818
|
+
}));
|
|
819
|
+
return out;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return out;
|
|
823
|
+
};
|
|
824
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
825
|
+
// 25. autoFocus outside a modal/dialog — steals focus on page load
|
|
826
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
827
|
+
const autoFocusStealsFocus = (source, file) => {
|
|
828
|
+
const out = [];
|
|
829
|
+
if (!looksLikeUiFile(file))
|
|
830
|
+
return out;
|
|
831
|
+
if (isInternalSurface(file))
|
|
832
|
+
return out;
|
|
833
|
+
if (!/\bautoFocus\b/.test(source))
|
|
834
|
+
return out;
|
|
835
|
+
// Fine inside dialog/modal/sheet/popover contexts
|
|
836
|
+
if (/Dialog|Modal|Sheet|Popover|Drawer|CommandDialog|AlertDialog/.test(source))
|
|
837
|
+
return out;
|
|
838
|
+
const idx = source.indexOf("autoFocus");
|
|
839
|
+
out.push(mkFinding({
|
|
840
|
+
id: "autofocus-on-load",
|
|
841
|
+
category: "Accessibility",
|
|
842
|
+
severity: "medium",
|
|
843
|
+
file, line: lineOf(source, idx),
|
|
844
|
+
message: "autoFocus used outside a modal/dialog scrolls and steals focus on page load.",
|
|
845
|
+
why: "Screen-reader users lose their place, keyboard users are yanked past the header, and the page jumps for everyone. WCAG 2.4.3 & 3.2.1.",
|
|
846
|
+
fix: "Remove autoFocus on landing pages/forms. Only use it after an intentional user action opens a dialog.",
|
|
847
|
+
aiPrompt: `In ${file}, remove the autoFocus attribute unless it lives inside a modal/dialog that was just opened by user action.`,
|
|
848
|
+
}));
|
|
849
|
+
return out;
|
|
850
|
+
};
|
|
851
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
852
|
+
// 26. SQL injection / string-concat inside a query call
|
|
853
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
854
|
+
const sqlStringConcat = (source, file) => {
|
|
855
|
+
const out = [];
|
|
856
|
+
if (!/\.(tsx|jsx|ts|js|mjs|cjs)$/.test(file))
|
|
857
|
+
return out;
|
|
858
|
+
// Common query call sites: pg .query, knex.raw, prisma.$queryRawUnsafe, supabase.rpc
|
|
859
|
+
const patterns = [
|
|
860
|
+
/\b(?:pg|client|db|pool)\.query\s*\(\s*`[^`]*\$\{/g,
|
|
861
|
+
/\b(?:knex|db)\.raw\s*\(\s*`[^`]*\$\{/g,
|
|
862
|
+
/\.\$queryRawUnsafe\s*\(/g,
|
|
863
|
+
/\.\$executeRawUnsafe\s*\(/g,
|
|
864
|
+
/\b(?:pg|client|db|pool)\.query\s*\(\s*["'`][^"'`]*["'`]\s*\+/g,
|
|
865
|
+
];
|
|
866
|
+
for (const re of patterns) {
|
|
867
|
+
let m;
|
|
868
|
+
while ((m = re.exec(source))) {
|
|
869
|
+
out.push(mkFinding({
|
|
870
|
+
id: "sql-string-concat",
|
|
871
|
+
category: "Security",
|
|
872
|
+
severity: "critical",
|
|
873
|
+
file, line: lineOf(source, m.index),
|
|
874
|
+
message: "Raw SQL built from string interpolation / concatenation.",
|
|
875
|
+
why: "Any value that came from a user, URL, or fetch body can inject SQL. This is one of the oldest and still most-exploited vulnerabilities.",
|
|
876
|
+
fix: "Use parameterised queries: pg → client.query('... WHERE id = $1', [id]); Prisma → $queryRaw`...${Prisma.sql`...`}`; Knex → knex('t').where({id}).",
|
|
877
|
+
aiPrompt: `In ${file} near line ${lineOf(source, m.index)}, rewrite this SQL call to use parameter placeholders ($1, $2, ...) and pass values as the second argument instead of interpolating them into the query string.`,
|
|
878
|
+
}));
|
|
879
|
+
return out;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return out;
|
|
883
|
+
};
|
|
884
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
885
|
+
// 27. MySQL / MariaDB connection without utf8mb4 → emojis crash inserts
|
|
886
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
887
|
+
const mysqlNoUtf8mb4 = (source, file) => {
|
|
888
|
+
const out = [];
|
|
889
|
+
if (!/\.(ts|js|mjs|cjs)$/.test(file))
|
|
890
|
+
return out;
|
|
891
|
+
if (!/from\s+["'](mysql2?|mariadb)["']|require\s*\(\s*["'](mysql2?|mariadb)["']/.test(source))
|
|
892
|
+
return out;
|
|
893
|
+
const hasFactory = /\.createPool\s*\(|\.createConnection\s*\(/.test(source);
|
|
894
|
+
if (!hasFactory)
|
|
895
|
+
return out;
|
|
896
|
+
if (/utf8mb4/i.test(source))
|
|
897
|
+
return out;
|
|
898
|
+
const idx = source.search(/\.create(Pool|Connection)\s*\(/);
|
|
899
|
+
out.push(mkFinding({
|
|
900
|
+
id: "mysql-not-utf8mb4",
|
|
901
|
+
category: "Reality & Resilience",
|
|
902
|
+
severity: "high",
|
|
903
|
+
file, line: lineOf(source, Math.max(idx, 0)),
|
|
904
|
+
message: "MySQL/MariaDB connection does not set charset: 'utf8mb4'.",
|
|
905
|
+
why: "The default 'utf8' in MySQL is really 3-byte utf8, so any 4-byte character (every emoji, plus loads of CJK and math symbols) will throw 'Incorrect string value' the first time a real user pastes one. This bricks signups, comments and chat messages.",
|
|
906
|
+
fix: "Pass { charset: 'utf8mb4' } to createPool/createConnection AND make sure the table + column collation is utf8mb4 (e.g. utf8mb4_unicode_ci).",
|
|
907
|
+
aiPrompt: `In ${file}, add charset: 'utf8mb4' to the MySQL createPool/createConnection options, and generate an ALTER TABLE migration converting text columns to CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci so emojis stop crashing inserts.`,
|
|
908
|
+
}));
|
|
909
|
+
return out;
|
|
910
|
+
};
|
|
911
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
912
|
+
// 28. Heavy library imported as a whole (blows bundle size)
|
|
913
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
914
|
+
const heavyLibImport = (source, file) => {
|
|
915
|
+
const out = [];
|
|
916
|
+
if (!/\.(tsx|jsx|ts|js|mjs|cjs)$/.test(file))
|
|
917
|
+
return out;
|
|
918
|
+
// package → { human size, lighter alternative }
|
|
919
|
+
const heavy = {
|
|
920
|
+
"moment": { size: "~290 KB min", fix: "swap for dayjs (~7 KB) or date-fns (per-function imports)" },
|
|
921
|
+
"lodash": { size: "~70 KB min", fix: "import per-method (`import debounce from 'lodash/debounce'`) or use lodash-es with a bundler that tree-shakes" },
|
|
922
|
+
"chart.js": { size: "~180 KB min", fix: "use the tree-shakable subpath imports (`import { Chart, LineController, ... } from 'chart.js'`) and only register what you draw" },
|
|
923
|
+
"rxjs": { size: "~140 KB min", fix: "import operators from 'rxjs/operators' individually, or drop RxJS if you only need a couple of observables" },
|
|
924
|
+
"three": { size: "~600 KB min", fix: "lazy-load the 3D route with React.lazy / dynamic import so it isn't in the initial bundle" },
|
|
925
|
+
"jquery": { size: "~90 KB min", fix: "delete jQuery — every browser since 2015 covers the same API natively" },
|
|
926
|
+
};
|
|
927
|
+
for (const [pkg, info] of Object.entries(heavy)) {
|
|
928
|
+
// Namespace import or default import of the whole package
|
|
929
|
+
const re = new RegExp(`^\\s*import\\s+(?:\\*\\s+as\\s+\\w+|\\w+)\\s+from\\s+["']${pkg.replace(".", "\\.")}["']`, "m");
|
|
930
|
+
const named = new RegExp(`^\\s*import\\s+\\{[^}]+\\}\\s+from\\s+["']${pkg.replace(".", "\\.")}["']`, "m");
|
|
931
|
+
const m = source.match(re) || source.match(named);
|
|
932
|
+
if (!m)
|
|
933
|
+
continue;
|
|
934
|
+
const idx = source.indexOf(m[0]);
|
|
935
|
+
out.push(mkFinding({
|
|
936
|
+
id: "heavy-lib-import",
|
|
937
|
+
category: "Performance & Bundle Size",
|
|
938
|
+
severity: "medium",
|
|
939
|
+
file, line: lineOf(source, idx),
|
|
940
|
+
message: `\`${pkg}\` imported at top level (${info.size}) — ships to every visitor.`,
|
|
941
|
+
why: "Every KB in the initial bundle delays Time-to-Interactive on mobile and cellular. Bundling a huge date/util/graphics lib for a single helper is the #1 self-inflicted perf wound.",
|
|
942
|
+
fix: info.fix + ". For truly optional features, wrap the route/component in React.lazy() so the lib only loads on demand.",
|
|
943
|
+
aiPrompt: `In ${file}, ${pkg} is imported at the module top-level. ${info.fix}. If the feature is only used on one route, additionally wrap it in React.lazy() + <Suspense> so it code-splits out of the initial bundle.`,
|
|
944
|
+
}));
|
|
945
|
+
}
|
|
946
|
+
return out;
|
|
947
|
+
};
|
|
948
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
696
949
|
// Registry
|
|
697
950
|
// ───────────────────────────────────────────────────────────────────────
|
|
698
951
|
const CHECKS = [
|
|
@@ -718,6 +971,13 @@ const CHECKS = [
|
|
|
718
971
|
idorDataFetch,
|
|
719
972
|
deadEndPage,
|
|
720
973
|
dbPollingAbuse,
|
|
974
|
+
iconButtonNoLabel,
|
|
975
|
+
formInputNoLabel,
|
|
976
|
+
tabIndexPositive,
|
|
977
|
+
autoFocusStealsFocus,
|
|
978
|
+
sqlStringConcat,
|
|
979
|
+
mysqlNoUtf8mb4,
|
|
980
|
+
heavyLibImport,
|
|
721
981
|
];
|
|
722
982
|
export const PER_FILE_CHECK_COUNT = CHECKS.length;
|
|
723
983
|
export function runChecks(source, file, ast) {
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import fs from "node:fs/promises";
|
|
6
6
|
import path from "node:path";
|
|
7
|
+
import fg from "fast-glob";
|
|
7
8
|
async function exists(p) {
|
|
8
9
|
try {
|
|
9
10
|
await fs.access(p);
|
|
@@ -21,7 +22,7 @@ async function readMaybe(p) {
|
|
|
21
22
|
return null;
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
export const REPO_WIDE_CHECK_COUNT =
|
|
25
|
+
export const REPO_WIDE_CHECK_COUNT = 15;
|
|
25
26
|
export async function runRepoChecks(root, _files) {
|
|
26
27
|
const out = [];
|
|
27
28
|
const has = async (rel) => exists(path.join(root, rel));
|
|
@@ -209,5 +210,200 @@ export async function runRepoChecks(root, _files) {
|
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
212
|
}
|
|
213
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
214
|
+
// Bundle-size / dependency hygiene
|
|
215
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
216
|
+
if (pkgRaw) {
|
|
217
|
+
let pkg = {};
|
|
218
|
+
try {
|
|
219
|
+
pkg = JSON.parse(pkgRaw);
|
|
220
|
+
}
|
|
221
|
+
catch { }
|
|
222
|
+
const runtimeDeps = Object.keys(pkg.dependencies ?? {});
|
|
223
|
+
const allDeps = [...runtimeDeps, ...Object.keys(pkg.devDependencies ?? {})];
|
|
224
|
+
// Build one combined haystack of source imports so we can answer
|
|
225
|
+
// "is this dep actually imported anywhere?" and "is this class used?".
|
|
226
|
+
let importHaystack = "";
|
|
227
|
+
for (const abs of _files) {
|
|
228
|
+
if (!/\.(tsx|jsx|ts|js|mjs|cjs|svelte|vue|astro|mdx?)$/.test(abs))
|
|
229
|
+
continue;
|
|
230
|
+
const chunk = await readMaybe(abs);
|
|
231
|
+
if (chunk)
|
|
232
|
+
importHaystack += chunk + "\n";
|
|
233
|
+
}
|
|
234
|
+
// Duplicate libraries that do the same job
|
|
235
|
+
const overlappingGroups = [
|
|
236
|
+
{
|
|
237
|
+
name: "date libraries",
|
|
238
|
+
members: ["moment", "dayjs", "date-fns", "luxon", "js-joda"],
|
|
239
|
+
recommend: "keep one. Prefer dayjs or date-fns; delete the others.",
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "HTTP clients",
|
|
243
|
+
members: ["axios", "ky", "got", "superagent", "node-fetch"],
|
|
244
|
+
recommend: "the platform fetch() covers 95% of cases. Keep at most one wrapper.",
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
name: "state managers",
|
|
248
|
+
members: ["redux", "@reduxjs/toolkit", "zustand", "jotai", "recoil", "mobx", "valtio"],
|
|
249
|
+
recommend: "pick one store; each one you keep ships another runtime to every user.",
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "form libraries",
|
|
253
|
+
members: ["react-hook-form", "formik", "final-form", "react-final-form"],
|
|
254
|
+
recommend: "consolidate on react-hook-form and remove the rest.",
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
name: "icon packs",
|
|
258
|
+
members: ["react-icons", "lucide-react", "@heroicons/react", "@fortawesome/react-fontawesome", "@mui/icons-material"],
|
|
259
|
+
recommend: "pick one icon set. Multiple icon packs balloon the bundle for no visual benefit.",
|
|
260
|
+
},
|
|
261
|
+
];
|
|
262
|
+
for (const g of overlappingGroups) {
|
|
263
|
+
const present = g.members.filter((m) => runtimeDeps.includes(m));
|
|
264
|
+
if (present.length >= 2) {
|
|
265
|
+
out.push({
|
|
266
|
+
id: "duplicate-libs",
|
|
267
|
+
category: "Performance & Bundle Size",
|
|
268
|
+
severity: "medium",
|
|
269
|
+
file: "package.json", line: 1,
|
|
270
|
+
message: `Multiple ${g.name} installed: ${present.join(", ")}.`,
|
|
271
|
+
why: "Shipping two libraries that solve the same problem doubles the bytes users download and doubles the surface area you need to maintain.",
|
|
272
|
+
fix: g.recommend,
|
|
273
|
+
aiPrompt: `Consolidate the project's ${g.name}. It currently depends on ${present.join(", ")}. Pick one, migrate every call site to it, then remove the others from package.json.`,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Unused runtime dependencies (best-effort: skip well-known "invisible" deps)
|
|
278
|
+
const invisibleDeps = new Set([
|
|
279
|
+
"typescript", "vite", "esbuild", "tsx", "ts-node", "tsup",
|
|
280
|
+
"@types/node", "@types/react", "@types/react-dom",
|
|
281
|
+
"postcss", "autoprefixer", "tailwindcss", "@tailwindcss/vite",
|
|
282
|
+
"eslint", "prettier", "husky", "lint-staged",
|
|
283
|
+
"vitest", "jest", "@vitest/ui",
|
|
284
|
+
"react", "react-dom", // used via JSX runtime, not import
|
|
285
|
+
]);
|
|
286
|
+
const unused = [];
|
|
287
|
+
for (const dep of runtimeDeps) {
|
|
288
|
+
if (invisibleDeps.has(dep))
|
|
289
|
+
continue;
|
|
290
|
+
if (dep.startsWith("@types/"))
|
|
291
|
+
continue;
|
|
292
|
+
// Sub-path imports still start with the package name
|
|
293
|
+
const escaped = dep.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
294
|
+
const re = new RegExp(`(?:from\\s+["']|require\\(["']|import\\(\\s*["'])${escaped}(?:["']|/)`);
|
|
295
|
+
if (!re.test(importHaystack))
|
|
296
|
+
unused.push(dep);
|
|
297
|
+
}
|
|
298
|
+
if (unused.length) {
|
|
299
|
+
const shown = unused.slice(0, 8);
|
|
300
|
+
out.push({
|
|
301
|
+
id: "unused-dependencies",
|
|
302
|
+
category: "Performance & Bundle Size",
|
|
303
|
+
severity: "medium",
|
|
304
|
+
file: "package.json", line: 1,
|
|
305
|
+
message: `${unused.length} dependenc${unused.length === 1 ? "y" : "ies"} in package.json appear unused: ${shown.join(", ")}${unused.length > shown.length ? ", …" : ""}.`,
|
|
306
|
+
why: "Unused deps still get installed, audited, and (if imported anywhere transitively) can end up in the bundle. They also slow every CI install and confuse new contributors.",
|
|
307
|
+
fix: "Verify each one, then `npm uninstall <pkg>` for the true zombies. Move build-only tools to devDependencies.",
|
|
308
|
+
aiPrompt: `The following dependencies have no import in the source tree: ${unused.join(", ")}. For each, decide whether to remove it (unused), move it to devDependencies (build/tool-only), or add the missing import (mistakenly deleted).`,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
// Code-splitting heuristic: many routes, zero dynamic imports
|
|
312
|
+
const routeFiles = _files.filter((f) => /\/(routes|pages|app)\//.test(f) && /\.(tsx|jsx)$/.test(f));
|
|
313
|
+
const dynamicImportHits = (importHaystack.match(/\bReact\.lazy\s*\(|\blazy\s*\(\s*\(\s*\)\s*=>\s*import\(|\bimport\(\s*["']/g) ?? []).length;
|
|
314
|
+
if (routeFiles.length >= 12 && dynamicImportHits === 0) {
|
|
315
|
+
out.push({
|
|
316
|
+
id: "no-code-splitting",
|
|
317
|
+
category: "Performance & Bundle Size",
|
|
318
|
+
severity: "medium",
|
|
319
|
+
file: "src", line: 1,
|
|
320
|
+
message: `${routeFiles.length} routes/pages and zero dynamic imports — everything ships in one bundle.`,
|
|
321
|
+
why: "Without code-splitting, a visitor to your landing page also downloads the admin dashboard, the settings screens, and every modal. First paint gets slower with every feature you add.",
|
|
322
|
+
fix: "Wrap heavy or rarely-used routes in React.lazy(() => import('./Route')) + <Suspense>, or use your router's built-in lazy() API. Split third-party charts, editors and 3D scenes the same way.",
|
|
323
|
+
aiPrompt: `This project has ${routeFiles.length} route files but no dynamic import()/React.lazy usage. Identify the largest / least-visited routes (admin, settings, editors, charts) and refactor them to React.lazy() + <Suspense fallback={...}> so they code-split out of the initial bundle.`,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
328
|
+
// Unused CSS classes (skip Tailwind / CSS-modules)
|
|
329
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
330
|
+
const cssFiles = await fg(["**/*.css", "!**/node_modules/**", "!**/dist/**", "!**/build/**", "!**/.next/**", "!**/.output/**", "!**/*.module.css"], { cwd: root, absolute: true, dot: false });
|
|
331
|
+
if (cssFiles.length) {
|
|
332
|
+
// Build a haystack of everything CSS classes might be referenced from.
|
|
333
|
+
let refHaystack = "";
|
|
334
|
+
for (const abs of _files) {
|
|
335
|
+
if (!/\.(tsx|jsx|ts|js|mjs|cjs|svelte|vue|astro|html?|mdx?)$/.test(abs))
|
|
336
|
+
continue;
|
|
337
|
+
const chunk = await readMaybe(abs);
|
|
338
|
+
if (chunk)
|
|
339
|
+
refHaystack += chunk + "\n";
|
|
340
|
+
}
|
|
341
|
+
for (const cssAbs of cssFiles) {
|
|
342
|
+
const src = await readMaybe(cssAbs);
|
|
343
|
+
if (!src)
|
|
344
|
+
continue;
|
|
345
|
+
// Skip Tailwind entry files and files that rely on @apply/@layer
|
|
346
|
+
if (/@tailwind\b|@apply\b|@layer\b/.test(src))
|
|
347
|
+
continue;
|
|
348
|
+
// Skip files that look generated / vendored
|
|
349
|
+
if (/DO NOT EDIT|autogenerated|generated by/i.test(src.slice(0, 400)))
|
|
350
|
+
continue;
|
|
351
|
+
const classRe = /\.([a-zA-Z_][\w-]{2,})\b(?=[^{}]*\{)/g;
|
|
352
|
+
const seen = new Set();
|
|
353
|
+
const unused = [];
|
|
354
|
+
let m;
|
|
355
|
+
while ((m = classRe.exec(src))) {
|
|
356
|
+
const cls = m[1];
|
|
357
|
+
if (seen.has(cls))
|
|
358
|
+
continue;
|
|
359
|
+
seen.add(cls);
|
|
360
|
+
// Ignore state / pseudo helpers that get composed dynamically
|
|
361
|
+
if (/^(is-|has-|js-)/.test(cls))
|
|
362
|
+
continue;
|
|
363
|
+
// Cheap reference check: appear in some source string / className
|
|
364
|
+
const refRe = new RegExp(`["'\`\\s]${cls.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}["'\`\\s]`);
|
|
365
|
+
if (!refRe.test(refHaystack))
|
|
366
|
+
unused.push(cls);
|
|
367
|
+
}
|
|
368
|
+
if (unused.length >= 5) {
|
|
369
|
+
const rel = path.relative(root, cssAbs);
|
|
370
|
+
const shown = unused.slice(0, 6);
|
|
371
|
+
out.push({
|
|
372
|
+
id: "unused-css-classes",
|
|
373
|
+
category: "Performance & Bundle Size",
|
|
374
|
+
severity: "low",
|
|
375
|
+
file: rel, line: 1,
|
|
376
|
+
message: `${unused.length} CSS class${unused.length === 1 ? "" : "es"} in ${rel} appear unused: ${shown.join(", ")}${unused.length > shown.length ? ", …" : ""}.`,
|
|
377
|
+
why: "Dead CSS still ships to every visitor on every page load. Over time projects accumulate hundreds of orphan classes from deleted components — silent bloat that hurts First Contentful Paint on mobile.",
|
|
378
|
+
fix: "Delete the unused selectors, or (better) enable PurgeCSS / Tailwind JIT / lightningcss to strip them at build time. If a class is only added dynamically, keep it — this check is heuristic.",
|
|
379
|
+
aiPrompt: `In ${rel}, the following CSS class selectors are not referenced anywhere in JSX/HTML: ${unused.join(", ")}. For each, verify with grep and remove if truly dead. If any are added dynamically (e.g. via classnames() with computed keys), keep and add a comment explaining why.`,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
385
|
+
// Security Definer RPC Check (Supabase/PostgreSQL)
|
|
386
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
387
|
+
const sqlFiles = await fg(["**/*.sql", "!**/node_modules/**", "!**/dist/**", "!**/build/**", "!**/.next/**"], { cwd: root, absolute: true, dot: false });
|
|
388
|
+
for (const sqlAbs of sqlFiles) {
|
|
389
|
+
const content = await readMaybe(sqlAbs);
|
|
390
|
+
if (!content)
|
|
391
|
+
continue;
|
|
392
|
+
if (/security\s+definer/i.test(content) && /grant\s+execute\s+on\s+function/i.test(content)) {
|
|
393
|
+
if (/(anon|public|authenticated)/i.test(content)) {
|
|
394
|
+
const rel = path.relative(root, sqlAbs);
|
|
395
|
+
out.push({
|
|
396
|
+
id: "definer-rpc-execute",
|
|
397
|
+
category: "Security & Permissions",
|
|
398
|
+
severity: "critical",
|
|
399
|
+
file: rel, line: 1,
|
|
400
|
+
message: "Signed-in users may have EXECUTE on a definer-scoped RPC.",
|
|
401
|
+
why: "SECURITY DEFINER functions run with the privileges of the creator (usually the postgres superuser). Granting EXECUTE to anon, public, or authenticated users can allow them to bypass Row Level Security (RLS) and escalate privileges.",
|
|
402
|
+
fix: "Ensure you restrict EXECUTE to a specific service role, or rewrite the function to be SECURITY INVOKER (the default) so it respects the caller's RLS policies.",
|
|
403
|
+
aiPrompt: `In ${rel}, a SECURITY DEFINER function is defined and seems to be granted to an untrusted role (anon, public, or authenticated). Review the function and change it to SECURITY INVOKER or revoke EXECUTE from public roles.`,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
212
408
|
return out;
|
|
213
409
|
}
|