codewatch-scan 0.1.0__py3-none-any.whl

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.
Files changed (136) hide show
  1. codewatch/__init__.py +0 -0
  2. codewatch/__main__.py +14 -0
  3. codewatch/asgi.py +16 -0
  4. codewatch/settings.py +175 -0
  5. codewatch/urls.py +40 -0
  6. codewatch/wsgi.py +16 -0
  7. codewatch_scan-0.1.0.dist-info/METADATA +81 -0
  8. codewatch_scan-0.1.0.dist-info/RECORD +136 -0
  9. codewatch_scan-0.1.0.dist-info/WHEEL +5 -0
  10. codewatch_scan-0.1.0.dist-info/entry_points.txt +4 -0
  11. codewatch_scan-0.1.0.dist-info/licenses/LICENSE +21 -0
  12. codewatch_scan-0.1.0.dist-info/top_level.txt +2 -0
  13. core/__init__.py +0 -0
  14. core/analyzers/__init__.py +19 -0
  15. core/analyzers/accessibility.py +199 -0
  16. core/analyzers/base.py +192 -0
  17. core/analyzers/body_models.py +154 -0
  18. core/analyzers/codewatch_checks.py +394 -0
  19. core/analyzers/constant_branches.py +251 -0
  20. core/analyzers/containers.py +316 -0
  21. core/analyzers/coverage.py +131 -0
  22. core/analyzers/dependencies.py +347 -0
  23. core/analyzers/drf_serializers.py +65 -0
  24. core/analyzers/else_branches.py +106 -0
  25. core/analyzers/extra.py +400 -0
  26. core/analyzers/flask_resources.py +65 -0
  27. core/analyzers/inline_scripts.py +50 -0
  28. core/analyzers/jinja_sandboxes.py +63 -0
  29. core/analyzers/match_statements.py +148 -0
  30. core/analyzers/multi_tools.py +888 -0
  31. core/analyzers/php_template_sinks.py +444 -0
  32. core/analyzers/project_checks.py +150 -0
  33. core/analyzers/python_tools.py +333 -0
  34. core/analyzers/route_converters.py +237 -0
  35. core/analyzers/slices.py +94 -0
  36. core/analyzers/starred_targets.py +42 -0
  37. core/analyzers/stored_fields.py +386 -0
  38. core/analyzers/summaries.py +1387 -0
  39. core/analyzers/template_sinks.py +162 -0
  40. core/api.py +360 -0
  41. core/apps.py +6 -0
  42. core/benchmark.py +955 -0
  43. core/bundled/catalog/codewatch/rules/javascript-crypto.yml +224 -0
  44. core/bundled/catalog/codewatch/rules/javascript-injection.yml +116 -0
  45. core/bundled/catalog/codewatch/rules/javascript-taint.yml +1539 -0
  46. core/bundled/catalog/codewatch/rules/javascript-web.yml +562 -0
  47. core/bundled/catalog/codewatch/rules/php-web.yml +719 -0
  48. core/bundled/catalog/codewatch/rules/python-crypto.yml +203 -0
  49. core/bundled/catalog/codewatch/rules/python-django.yml +267 -0
  50. core/bundled/catalog/codewatch/rules/python-injection.yml +115 -0
  51. core/bundled/catalog/codewatch/rules/python-web.yml +3382 -0
  52. core/bundled/catalog/custom/mappings.json +360 -0
  53. core/bundled/catalog/custom/overrides.json +4 -0
  54. core/bundled/catalog/custom/rules/django-settings.yml +19 -0
  55. core/bundled/catalog/imported/changes.json +5 -0
  56. core/bundled/catalog/imported/docs.json +1143 -0
  57. core/bundled/catalog/imported/meta.json +54 -0
  58. core/bundled/catalog/imported/rules.json +85421 -0
  59. core/bundled/catalog/imported/standards.json +11191 -0
  60. core/bundled/tools/analyzers.lock +786 -0
  61. core/bundled/tools/axe-scan.mjs +45 -0
  62. core/bundled/tools/checkov.lock +2463 -0
  63. core/bundled/tools/codewatch-checks.mjs +99 -0
  64. core/bundled/tools/eslint-a11y.config.js +16 -0
  65. core/bundled/tools/eslint-quality.config.js +63 -0
  66. core/bundled/tools/eslint-typed.config.js +18 -0
  67. core/bundled/tools/package-lock.json +6102 -0
  68. core/bundled/tools/package.json +32 -0
  69. core/bundled/tools/rule-metadata.mjs +56 -0
  70. core/bundled/tools/semgrep.lock +1232 -0
  71. core/bundled/tools/stylelint.config.json +11 -0
  72. core/catalog/__init__.py +190 -0
  73. core/catalog/build.py +84 -0
  74. core/catalog/merge.py +122 -0
  75. core/catalog/sources.py +418 -0
  76. core/catalog/standards.py +119 -0
  77. core/catalog/update.py +99 -0
  78. core/catalog_views.py +115 -0
  79. core/client.py +645 -0
  80. core/collect.py +518 -0
  81. core/duplicates.py +60 -0
  82. core/fingerprint.py +25 -0
  83. core/folders.py +73 -0
  84. core/gate.py +77 -0
  85. core/git.py +149 -0
  86. core/jobs.py +86 -0
  87. core/management/__init__.py +0 -0
  88. core/management/commands/__init__.py +0 -0
  89. core/management/commands/addproject.py +40 -0
  90. core/management/commands/buildcatalog.py +15 -0
  91. core/management/commands/scan.py +74 -0
  92. core/management/commands/updatecatalog.py +14 -0
  93. core/mcp_server.py +140 -0
  94. core/middleware.py +17 -0
  95. core/migrations/0001_initial.py +115 -0
  96. core/migrations/0002_auth_tokens_a11y.py +60 -0
  97. core/migrations/0003_default_admin.py +43 -0
  98. core/migrations/0004_project_members.py +28 -0
  99. core/migrations/0005_uploads_and_sources.py +55 -0
  100. core/migrations/0006_thresholds.py +28 -0
  101. core/migrations/0007_also_reported_by.py +18 -0
  102. core/migrations/0008_new_code_period.py +18 -0
  103. core/migrations/0009_resolution_safe.py +18 -0
  104. core/migrations/0010_analysis_progress.py +18 -0
  105. core/migrations/0011_overall_gate_conditions.py +20 -0
  106. core/migrations/__init__.py +0 -0
  107. core/models.py +323 -0
  108. core/network.py +41 -0
  109. core/paths.py +29 -0
  110. core/ratings.py +67 -0
  111. core/runner.py +168 -0
  112. core/templates/core/account.html +30 -0
  113. core/templates/core/analysis.html +49 -0
  114. core/templates/core/base.html +121 -0
  115. core/templates/core/form_table.html +12 -0
  116. core/templates/core/gate_badge.html +1 -0
  117. core/templates/core/index.html +26 -0
  118. core/templates/core/issues.html +38 -0
  119. core/templates/core/login.html +13 -0
  120. core/templates/core/new_project.html +27 -0
  121. core/templates/core/password_change.html +10 -0
  122. core/templates/core/project.html +40 -0
  123. core/templates/core/rating.html +1 -0
  124. core/templates/core/ratings.html +32 -0
  125. core/templates/core/rule.html +33 -0
  126. core/templates/core/rules.html +42 -0
  127. core/templates/core/scan_progress.html +2 -0
  128. core/templates/core/settings.html +36 -0
  129. core/templates/core/source.html +15 -0
  130. core/templates/core/standards.html +30 -0
  131. core/templates/core/user_edit.html +15 -0
  132. core/templates/core/users.html +18 -0
  133. core/templatetags/__init__.py +0 -0
  134. core/templatetags/codewatch.py +42 -0
  135. core/tool_setup.py +68 -0
  136. core/views.py +564 -0
@@ -0,0 +1,99 @@
1
+ // codewatch's own checks that need a parser: repeated string literals in JS/TS, and ReDoS-prone regexes.
2
+ // Usage: node codewatch-checks.mjs spec.json out.json
3
+ // spec: {"jsFiles": [abs paths], "minLength": 5, "maxRepeats": 3, "pythonRegexes": [{path, line, pattern, flags}]}
4
+ import { readFileSync, writeFileSync } from "node:fs";
5
+ import { createRequire } from "node:module";
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const ts = require("typescript");
9
+ const { checkSync } = require("recheck");
10
+
11
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- path chosen by codewatch, not by scanned code
12
+ const spec = JSON.parse(readFileSync(process.argv[2], "utf8"));
13
+ const literals = [];
14
+ const regexes = [];
15
+
16
+ function scriptKind(path) {
17
+ if (path.endsWith(".tsx")) return ts.ScriptKind.TSX;
18
+ if (path.endsWith(".jsx")) return ts.ScriptKind.JSX;
19
+ return path.endsWith(".ts") ? ts.ScriptKind.TS : ts.ScriptKind.JS;
20
+ }
21
+
22
+ // Strings that are names rather than repeated data: module specifiers, property keys and literal types.
23
+ const STRUCTURAL_PARENTS = [
24
+ (p) => ts.isImportDeclaration(p) || ts.isExportDeclaration(p) || ts.isExternalModuleReference(p),
25
+ (p) => ts.isCallExpression(p) && p.expression.getText() === "require",
26
+ (p, node) => p.name === node
27
+ && (ts.isPropertyAssignment(p) || ts.isPropertySignature(p) || ts.isPropertyDeclaration(p)),
28
+ (p) => ts.isLiteralTypeNode(p) || ts.isElementAccessExpression(p),
29
+ ];
30
+
31
+ function isStructural(node) {
32
+ return Boolean(node.parent) && STRUCTURAL_PARENTS.some((test) => test(node.parent, node));
33
+ }
34
+
35
+ function isRepeatableString(node) {
36
+ return (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) && !isStructural(node)
37
+ && node.text.trim().length >= spec.minLength;
38
+ }
39
+
40
+ function regexLiteral(node, sf) {
41
+ if (node.kind !== ts.SyntaxKind.RegularExpressionLiteral) return null;
42
+ const raw = node.getText(sf);
43
+ const end = raw.lastIndexOf("/");
44
+ return { pattern: raw.slice(1, end), flags: raw.slice(end + 1) };
45
+ }
46
+
47
+ // new RegExp("...", "flags") / RegExp("...") with literal arguments.
48
+ function regexConstructor(node, sf) {
49
+ const isCall = ts.isNewExpression(node) || ts.isCallExpression(node);
50
+ if (!isCall || node.expression.getText(sf) !== "RegExp") return null;
51
+ const [source, flags] = node.arguments ?? [];
52
+ if (!source || !ts.isStringLiteral(source)) return null;
53
+ return { pattern: source.text, flags: flags && ts.isStringLiteral(flags) ? flags.text : "" };
54
+ }
55
+
56
+ function scanJs(path) {
57
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- path chosen by codewatch, not by scanned code
58
+ const text = readFileSync(path, "utf8");
59
+ const sf = ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true, scriptKind(path));
60
+ const seen = new Map();
61
+ const lineOf = (node) => sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
62
+ const visit = (node) => {
63
+ if (isRepeatableString(node)) {
64
+ if (!seen.has(node.text)) seen.set(node.text, []);
65
+ seen.get(node.text).push(lineOf(node));
66
+ }
67
+ const regex = regexLiteral(node, sf) ?? regexConstructor(node, sf);
68
+ if (regex) regexes.push({ path, line: lineOf(node), ...regex });
69
+ ts.forEachChild(node, visit);
70
+ };
71
+ visit(sf);
72
+ for (const [value, lines] of seen) {
73
+ if (lines.length > spec.maxRepeats) literals.push({ path, value, lines });
74
+ }
75
+ }
76
+
77
+ for (const path of spec.jsFiles) {
78
+ try {
79
+ scanJs(path);
80
+ } catch (err) {
81
+ process.stderr.write(`${path}: ${err.message}\n`);
82
+ }
83
+ }
84
+
85
+ const checked = [];
86
+ for (const r of [...regexes, ...spec.pythonRegexes]) {
87
+ try {
88
+ const result = checkSync(r.pattern, r.flags.replace(/[^dgimsuyv]/g, ""), { timeout: 3000 });
89
+ if (result.status === "vulnerable") {
90
+ checked.push({ ...r, complexity: result.complexity?.type, summary: result.complexity?.summary,
91
+ attack: result.attack?.pattern });
92
+ }
93
+ } catch {
94
+ // Patterns JavaScript can't parse (some Python-only syntax) are skipped rather than guessed at.
95
+ }
96
+ }
97
+
98
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- path chosen by codewatch, not by scanned code
99
+ writeFileSync(process.argv[3], JSON.stringify({ literals, regexes: checked }));
@@ -0,0 +1,16 @@
1
+ import jsxA11y from "eslint-plugin-jsx-a11y";
2
+ import tseslint from "typescript-eslint";
3
+
4
+ const recommended = jsxA11y.flatConfigs.recommended;
5
+
6
+ export default [
7
+ {
8
+ ...recommended,
9
+ files: ["**/*.jsx", "**/*.tsx"],
10
+ languageOptions: {
11
+ ...recommended.languageOptions,
12
+ parser: tseslint.parser,
13
+ parserOptions: { ecmaFeatures: { jsx: true } },
14
+ },
15
+ },
16
+ ];
@@ -0,0 +1,63 @@
1
+ import js from "@eslint/js";
2
+ import security from "eslint-plugin-security";
3
+ import globals from "globals";
4
+ import tseslint from "typescript-eslint";
5
+
6
+ const ERROR = "error";
7
+ const WARN = "warn";
8
+
9
+ const quality = {
10
+ "no-undef": WARN,
11
+ eqeqeq: [ERROR, "smart"],
12
+ "no-var": ERROR,
13
+ "prefer-const": WARN,
14
+ "no-eval": ERROR,
15
+ "no-implied-eval": ERROR,
16
+ "no-new-func": ERROR,
17
+ "no-throw-literal": ERROR,
18
+ "no-unused-expressions": ERROR,
19
+ "consistent-return": WARN,
20
+ "default-case-last": ERROR,
21
+ "no-shadow": WARN,
22
+ "no-return-assign": ERROR,
23
+ "no-self-compare": ERROR,
24
+ "no-unmodified-loop-condition": ERROR,
25
+ "no-unreachable-loop": ERROR,
26
+ "no-template-curly-in-string": WARN,
27
+ "require-atomic-updates": WARN,
28
+ "max-depth": [WARN, 4],
29
+ "max-params": [WARN, 6],
30
+ "max-nested-callbacks": [WARN, 4],
31
+ // `a ?? 32 - 32` reads as `(a ?? 32) - 32` but parses as `a ?? 0`.
32
+ // Each group pairs ?? with one operator, so ordinary arithmetic like a + b * c is not flagged.
33
+ "no-mixed-operators": [ERROR, { groups: ["+", "-", "*", "/", "%", "**"].map((op) => ["??", op]) }],
34
+ "no-restricted-syntax": [
35
+ WARN,
36
+ {
37
+ selector: "CallExpression[callee.property.name=/^reduce(Right)?$/][arguments.length=1]",
38
+ message: "reduce() without an initial value throws on an empty array",
39
+ },
40
+ ],
41
+ };
42
+
43
+ // Flags every obj[variable] access; in practice nearly all of its findings are false positives.
44
+ const securityRules = { ...security.configs.recommended.rules, "security/detect-object-injection": "off" };
45
+
46
+ export default [
47
+ {
48
+ files: ["**/*.js", "**/*.mjs", "**/*.cjs", "**/*.jsx", "**/*.ts", "**/*.tsx"],
49
+ plugins: { security },
50
+ languageOptions: {
51
+ ecmaVersion: "latest",
52
+ sourceType: "module",
53
+ parser: tseslint.parser,
54
+ parserOptions: { ecmaFeatures: { jsx: true } },
55
+ globals: { ...globals.browser, ...globals.node, ...globals.es2021 },
56
+ },
57
+ linterOptions: { reportUnusedDisableDirectives: "off" },
58
+ rules: { ...js.configs.recommended.rules, ...securityRules, ...quality },
59
+ },
60
+ { files: ["**/*.cjs"], languageOptions: { sourceType: "commonjs" } },
61
+ // The TypeScript compiler already resolves names; no-undef misfires on types.
62
+ { files: ["**/*.ts", "**/*.tsx"], rules: { "no-undef": "off", "no-unused-vars": "off" } },
63
+ ];
@@ -0,0 +1,18 @@
1
+ // Rules that need type information, run only on TypeScript files a tsconfig.json covers.
2
+ import tseslint from "typescript-eslint";
3
+
4
+ export default [
5
+ {
6
+ files: ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"],
7
+ languageOptions: {
8
+ parser: tseslint.parser,
9
+ parserOptions: { projectService: true, tsconfigRootDir: process.cwd() },
10
+ },
11
+ plugins: { "@typescript-eslint": tseslint.plugin },
12
+ linterOptions: { reportUnusedDisableDirectives: "off" },
13
+ rules: {
14
+ "@typescript-eslint/no-floating-promises": "error",
15
+ "@typescript-eslint/no-misused-promises": ["error", { checksVoidReturn: false }],
16
+ },
17
+ },
18
+ ];