π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Enforce native lint exception lists
Manav Rathi71b55afa09b7
diff --git a/.github/checks/change-approval/files.mjs b/.github/checks/change-approval/files.mjsindex 1a7a1f5e72..f8c6e744aa 100644--- a/.github/checks/change-approval/files.mjs+++ b/.github/checks/change-approval/files.mjs@@ -7,6 +7,7 @@ const guardrailDirs = ["android/checks/","android/scripts/","android/gradle/",+ "apple/checks/","apple/scripts/","mobile/checks/","mobile/scripts/",@@ -25,6 +26,8 @@ const guardrailFiles = new Set([".rustfmt.toml","eslint.config.mjs","eslint-suppressions.json",+ "android/checks/lint-exceptions/suppressions.json",+ "apple/checks/lint-exceptions/suppressions.json","rust/checks/lint-exceptions/suppressions.json","web/checks/lint-exceptions/suppressions.json",".prettierrc.json",diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjsindex 574d6d317a..1b0d016120 100644--- a/.github/checks/change-approval/test.mjs+++ b/.github/checks/change-approval/test.mjs@@ -598,6 +598,7 @@ test("existing guardrails modified or deleted", (t) => {"apple/.swift-format": "{}\n","apple/.swiftlint.yml": "only_rules: []\n","apple/Package.swift": "// swift-tools-version: 6.0\n",+ "apple/checks/lint-exceptions/check.mjs": "","apple/scripts/lint.sh": "swift format lint --strict\n","mobile/checks/x/check.rb": "","rust/checks/x/check.py": "",@@ -610,6 +611,7 @@ test("existing guardrails modified or deleted", (t) => {"apple/.swift-format": null,"apple/.swiftlint.yml": "only_rules: [empty_count]\n","apple/Package.swift": "// swift-tools-version: 6.1\n",+ "apple/checks/lint-exceptions/check.mjs": "\n","apple/scripts/lint.sh": "swift format lint\n","mobile/checks/x/check.rb": "\n","rust/checks/x/check.py": "\n",@@ -620,7 +622,7 @@ test("existing guardrails modified or deleted", (t) => {);assert.equal(output,- "10 guardrail files\n\n## Guardrail changes\n\n- `.github/scripts/x.mjs`\n- `.github/workflows/x.yml`\n- `apple/.swift-format`\n- `apple/.swiftlint.yml`\n- `apple/Package.swift`\n- `apple/scripts/lint.sh`\n- `mobile/checks/x/check.rb`\n- `rust/checks/x/check.py`\n- `web/apps/x/eslint.config.mjs`\n- `web/checks/x/check.mjs`\n\n",+ "11 guardrail files\n\n## Guardrail changes\n\n- `.github/scripts/x.mjs`\n- `.github/workflows/x.yml`\n- `apple/.swift-format`\n- `apple/.swiftlint.yml`\n- `apple/Package.swift`\n- `apple/checks/lint-exceptions/check.mjs`\n- `apple/scripts/lint.sh`\n- `mobile/checks/x/check.rb`\n- `rust/checks/x/check.py`\n- `web/apps/x/eslint.config.mjs`\n- `web/checks/x/check.mjs`\n\n",);});@@ -658,6 +660,8 @@ test("new lint and formatter configs need approval, including untracked files",test("suppression lists need approval when added, edited, or deleted", (t) => {const files = {+ "android/checks/lint-exceptions/suppressions.json": "{}\n",+ "apple/checks/lint-exceptions/suppressions.json": "{}\n","web/apps/photos/eslint-suppressions.json": "{}\n","web/packages/new/nested/eslint-suppressions.json": "{}\n","rust/checks/lint-exceptions/suppressions.json": "{}\n",@@ -678,11 +682,11 @@ test("suppression lists need approval when added, edited, or deleted", (t) => {]) {const { output, summary } = scan(t, before, after, { ci: true });assert.equal(output, 'categories=["guardrail files"]\n');- assert.match(summary, /4 guardrail files/);+ assert.match(summary, /6 guardrail files/);for (const file of Object.keys(files))assert.ok(summary.includes(`\`${file}\``));}- assert.match(scan(t, {}, files, { commit: false }), /^4 guardrail files\n/);+ assert.match(scan(t, {}, files, { commit: false }), /^6 guardrail files\n/);});test("Android lint configurations need approval when added, edited, or deleted", (t) => {diff --git a/android/checks/lint-exceptions/check.mjs b/android/checks/lint-exceptions/check.mjsnew file mode 100644index 0000000000..2df6b70b1e--- /dev/null+++ b/android/checks/lint-exceptions/check.mjs@@ -0,0 +1,193 @@+import { execFileSync } from "node:child_process";+import { existsSync, readFileSync } from "node:fs";+import { resolve } from "node:path";++const root = resolve(+ process.argv[2] ?? resolve(import.meta.dirname, "../../.."),+);+const listPath = "android/checks/lint-exceptions/suppressions.json";+const allowed = JSON.parse(readFileSync(resolve(root, listPath), "utf8"));+const unused = new Map(+ Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),+);+const files = execFileSync(+ "git",+ [+ "ls-files",+ "--cached",+ "--others",+ "--exclude-standard",+ "-z",+ "--",+ "android",+ ],+ { cwd: root, encoding: "utf8" },+).split("\0");++for (const path of new Set(files)) {+ if (+ !/\.(?:kt|kts|java|xml)$/.test(path) ||+ !existsSync(resolve(root, path))+ )+ continue;+ const source = readFileSync(resolve(root, path), "utf8");+ const check = (offset, rules) => {+ const line = source.slice(0, offset).split("\n").length;+ if (+ !rules.length ||+ rules.some(+ (rule) =>+ !/^[\w.:-]+$/.test(rule) || rule.toLowerCase() === "all",+ )+ ) {+ reject(+ path,+ line,+ "Name the rules being suppressed using literal rule IDs",+ );+ return;+ }+ for (const rule of rules) {+ unused.get(path)?.delete(rule);+ if (!allowed[path]?.includes(rule))+ reject(path, line, `${rule} is not listed in ${listPath}`);+ }+ };+ if (path.endsWith(".xml")) {+ const xml = source.replace(+ /<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>/g,+ (text) => " ".repeat(text.length),+ );+ const attributes = [+ ...xml.matchAll(/<(?:[^>"']|"[^"]*"|'[^']*')*>/g),+ ].flatMap((tag) =>+ [...tag[0].matchAll(/([\w.:-]+)\s*=\s*(["'])(.*?)\2/gs)].map(+ (attribute) => ({+ name: attribute[1],+ value: attribute[3],+ start: tag.index + attribute.index,+ }),+ ),+ );+ const prefixes = new Set(["tools"]);+ for (const { name, value } of attributes) {+ if (+ name.startsWith("xmlns:") &&+ value === "http://schemas.android.com/tools"+ ) {+ prefixes.add(name.slice(6));+ }+ }+ for (const { name, value, start } of attributes) {+ const [prefix, local] = name.split(":");+ if (local === "ignore" && prefixes.has(prefix)) {+ check(+ start,+ value.split(",").map((rule) => rule.trim()),+ );+ }+ }+ continue;+ }+ const tokens = tokenize(source);+ const names = new Set(["Suppress", "SuppressLint", "SuppressWarnings"]);+ for (let i = 0; i < tokens.length; i++) {+ if (names.has(tokens[i].text) && tokens[i + 1]?.text === "as") {+ names.add(tokens[i + 2]?.text);+ }+ }+ let annotationGroup = false;+ for (let i = 0; i < tokens.length; i++) {+ const token = tokens[i];+ let prefix = i - 1;+ while (tokens[prefix]?.text === ".") prefix -= 2;+ if (tokens[prefix]?.text === ":") prefix -= 2;+ const annotated = tokens[prefix]?.text === "@";+ if (token.text === "[" && annotated) annotationGroup = true;+ if (token.text === "]") annotationGroup = false;+ if (token.text.startsWith("//") || token.text.startsWith("/*")) {+ const match = token.text.match(/\bnoinspection\b([^\r\n]*)/);+ if (match)+ check(+ token.start,+ match[1]+ .split("*/")[0]+ .trim()+ .split(/[\s,]+/)+ .filter(Boolean),+ );+ }+ if (+ !(annotated || annotationGroup) ||+ !names.has(token.text) ||+ tokens[i + 1]?.text !== "("+ )+ continue;+ const rules = [];+ let valid = true;+ let depth = 1;+ let j = i + 2;+ for (; j < tokens.length; j++) {+ const value = tokens[j].text;+ if (value === ")" && --depth === 0) break;+ if (value === "(") depth++;+ if (value === "(" || value === ")") continue;+ if (+ value.startsWith("//") ||+ value.startsWith("/*") ||+ /^[,{}\[\]]$/.test(value)+ )+ continue;+ if (+ (value === "names" || value === "value") &&+ tokens[j + 1]?.text === "="+ ) {+ j++;+ } else if (/^"[\w.:-]+"$/.test(value)) {+ rules.push(value.slice(1, -1));+ } else if (value === "arrayOf" && tokens[j + 1]?.text === "(") {+ continue;+ } else {+ valid = false;+ }+ }+ check(token.start, valid && j < tokens.length ? rules : []);+ i = j;+ }+}++for (const [path, rules] of unused) {+ for (const rule of rules)+ reject(+ path,+ 1,+ `${rule} has no suppression; remove it from ${listPath}`,+ );+}++function tokenize(source) {+ const lexer =+ /\/\/[^\r\n]*|\/\*|"""[\s\S]*?"""|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|[A-Za-z_]\w*|[^\s]/g;+ const tokens = [];+ let match;+ while ((match = lexer.exec(source))) {+ const start = match.index;+ if (match[0] === "/*") {+ const markers = /\/\*|\*\//g;+ markers.lastIndex = lexer.lastIndex;+ for (let depth = 1; depth; ) {+ const marker = markers.exec(source);+ if (!marker) throw new Error("Unclosed Android comment");+ depth += marker[0] === "/*" ? 1 : -1;+ }+ lexer.lastIndex = markers.lastIndex;+ }+ tokens.push({ text: source.slice(start, lexer.lastIndex), start });+ }+ return tokens;+}++function reject(path, line, message) {+ console.error(`${path}:${line}: ${message}`);+ process.exitCode = 1;+}diff --git a/android/checks/lint-exceptions/suppressions.json b/android/checks/lint-exceptions/suppressions.jsonnew file mode 100644index 0000000000..e0ab5d6501--- /dev/null+++ b/android/checks/lint-exceptions/suppressions.json@@ -0,0 +1,13 @@+{+ "android/apps/ensu/app/src/main/AndroidManifest.xml": ["UnusedAttribute"],+ "android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt": [+ "DEPRECATION"+ ],+ "android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt": ["Recycle"],+ "android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt": [+ "MissingPermission"+ ],+ "android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt": [+ "Recycle"+ ]+}diff --git a/android/checks/lint-exceptions/test.mjs b/android/checks/lint-exceptions/test.mjsnew file mode 100644index 0000000000..786247a5a2--- /dev/null+++ b/android/checks/lint-exceptions/test.mjs@@ -0,0 +1,101 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++const check = join(import.meta.dirname, "check.mjs");+const root = mkdtempSync(join(tmpdir(), "ente-android-lint-exceptions-"));+try {+ assert.equal(spawnSync("git", ["init", "-q"], { cwd: root }).status, 0);+ mkdirSync(join(root, "android/checks/lint-exceptions"), {+ recursive: true,+ });+ for (const [extension, source] of [+ ["kt", '@Suppress("NewApi") fun example() {}'],+ ["kts", '@file:Suppress("NewApi")'],+ ["kt", '@file:[Suppress("NewApi")]'],+ ["kt", '@Suppress(\n/* reason */ "NewApi",\n) fun example() {}'],+ ["kt", '@Suppress(names = ["NewApi"]) fun example() {}'],+ ["kt", '@Suppress(names = arrayOf("NewApi")) fun example() {}'],+ [+ "kt",+ 'import kotlin.Suppress as Quiet\n@Quiet("NewApi") fun example() {}',+ ],+ ["java", '@android.annotation.SuppressLint("NewApi") class Example {}'],+ ["java", '@SuppressWarnings(value = {"NewApi"}) class Example {}'],+ ["java", "//noinspection NewApi\nclass Example {}"],+ ["xml", '<view tools:ignore="NewApi" />'],+ [+ "xml",+ '<view xmlns:lint="http://schemas.android.com/tools" lint:ignore="NewApi" />',+ ],+ ]) {+ assert.equal(run(extension, source).status, 1);+ const file = `android/Example.${extension}`;+ const result = run(extension, source, { [file]: ["NewApi"] });+ assert.equal(result.status, 0, result.stderr);+ assert.equal(+ run(extension, source, { "android/Other.kt": ["NewApi"] }).status,+ 1,+ );+ }+ for (const source of [+ '@Suppress("NewApi", "MissingPermission")',+ '@Suppress("all")',+ "@Suppress(RULE)",+ '@Suppress("New" + "Api")',+ "//noinspection",+ ]) {+ assert.equal(+ run("kt", source, { "android/Example.kt": ["NewApi"] }).status,+ 1,+ );+ }+ for (const [extension, source] of [+ [+ "kt",+ 'fun Suppress(text: String) {}\nfun example() { Suppress("NewApi") }',+ ],+ ["kt", '// @Suppress("NewApi")'],+ ["kt", '/* outer /* nested */ @Suppress("NewApi") */'],+ ["kt", 'val text = "@Suppress(\\"NewApi\\")"'],+ [+ "kt",+ 'val text = """\n@Suppress("NewApi")\n//noinspection NewApi\n"""',+ ],+ ["xml", '<!-- <view tools:ignore="NewApi" /> -->'],+ ["xml", '<value><![CDATA[tools:ignore="NewApi"]]></value>'],+ ["xml", '<string name="example">tools:ignore="NewApi"</string>'],+ ["xml", `<view text='tools:ignore="NewApi"' />`],+ ]) {+ const result = run(extension, source);+ assert.equal(result.status, 0, result.stderr);+ }+ const suppression = '@SuppressLint("NewApi") fun example() {}\n';+ for (const source of [suppression + suppression, suppression]) {+ const result = run("kt", source, { "android/Example.kt": ["NewApi"] });+ assert.equal(result.status, 0, result.stderr);+ }+ for (const source of ["fun example() {}", null]) {+ const result = run("kt", source, { "android/Example.kt": ["NewApi"] });+ assert.equal(result.status, 1, result.stderr);+ assert.match(result.stderr, /NewApi has no suppression/);+ assert.equal(run("kt", source).status, 0);+ }+} finally {+ rmSync(root, { recursive: true });+}++function run(extension, source, allowed = {}) {+ for (const extension of ["kt", "kts", "java", "xml"]) {+ rmSync(join(root, `android/Example.${extension}`), { force: true });+ }+ if (source !== null)+ writeFileSync(join(root, `android/Example.${extension}`), source);+ writeFileSync(+ join(root, "android/checks/lint-exceptions/suppressions.json"),+ JSON.stringify(allowed),+ );+ return spawnSync(process.execPath, [check, root], { encoding: "utf8" });+}diff --git a/android/scripts/lint.sh b/android/scripts/lint.shindex 064a26c6fa..30c788950b 100755--- a/android/scripts/lint.sh+++ b/android/scripts/lint.sh@@ -2,8 +2,11 @@set -euo pipefailcd "$(dirname "$0")/.."+python3 checks/gradle-order/check.py .python3 checks/gradle-order/test.py+node checks/lint-exceptions/check.mjs+node checks/lint-exceptions/test.mjs(cd ../rust && cargo codegen native)./gradlew --build-cache :lintdiff --git a/apple/checks/lint-exceptions/check.mjs b/apple/checks/lint-exceptions/check.mjsnew file mode 100644index 0000000000..641a23549c--- /dev/null+++ b/apple/checks/lint-exceptions/check.mjs@@ -0,0 +1,100 @@+import { execFileSync } from "node:child_process";+import { existsSync, readFileSync } from "node:fs";+import { resolve } from "node:path";++const root = resolve(+ process.argv[2] ?? resolve(import.meta.dirname, "../../.."),+);+const listPath = "apple/checks/lint-exceptions/suppressions.json";+const allowed = JSON.parse(readFileSync(resolve(root, listPath), "utf8"));+const unused = new Map(+ Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),+);+const files = execFileSync(+ "git",+ [+ "ls-files",+ "--cached",+ "--others",+ "--exclude-standard",+ "-z",+ "--",+ "apple",+ ],+ { cwd: root, encoding: "utf8" },+).split("\0");++for (const path of new Set(files)) {+ if (!path.endsWith(".swift") || !existsSync(resolve(root, path))) continue;+ const source = readFileSync(resolve(root, path), "utf8");+ for (const { text, start } of comments(source)) {+ const directives =+ /\bswiftlint\s*:\s*disable(?::(?:next|previous|this))?\b([^\r\n]*)|\bswift-format-ignore(-file)?\b([^\r\n]*)/g;+ for (const match of text.matchAll(directives)) {+ const line = source+ .slice(0, start + match.index)+ .split("\n").length;+ const value = (match[1] ?? match[3]).split(/\/\/|\*\//)[0].trim();+ const rules = value+ .replace(/^:\s*/, "")+ .split(/[\s,]+/)+ .filter(Boolean);+ if (match[2] || !rules.length || rules.includes("all")) {+ reject(path, line, "Name the rules being suppressed");+ continue;+ }+ for (const rule of rules) {+ unused.get(path)?.delete(rule);+ if (!allowed[path]?.includes(rule)) {+ reject(path, line, `${rule} is not listed in ${listPath}`);+ }+ }+ }+ }+}++for (const [path, rules] of unused) {+ for (const rule of rules) {+ reject(+ path,+ 1,+ `${rule} has no suppression; remove it from ${listPath}`,+ );+ }+}++function* comments(source) {+ const lexer = /\/\/[^\r\n]*|\/\*|(#*)("""|")/g;+ let match;+ while ((match = lexer.exec(source))) {+ const start = match.index;+ if (match[0].startsWith("//")) {+ yield { text: match[0], start };+ } else if (match[0] === "/*") {+ const markers = /\/\*|\*\//g;+ markers.lastIndex = lexer.lastIndex;+ for (let depth = 1; depth; ) {+ const marker = markers.exec(source);+ if (!marker) throw new Error("Unclosed Swift comment");+ depth += marker[0] === "/*" ? 1 : -1;+ }+ lexer.lastIndex = markers.lastIndex;+ yield { text: source.slice(start, lexer.lastIndex), start };+ } else {+ const end = match[2] + match[1];+ const escape = "\\" + match[1];+ let offset = lexer.lastIndex;+ while (offset < source.length && !source.startsWith(end, offset)) {+ offset += source.startsWith(escape, offset)+ ? escape.length + 1+ : 1;+ }+ lexer.lastIndex = offset + end.length;+ }+ }+}++function reject(path, line, message) {+ console.error(`${path}:${line}: ${message}`);+ process.exitCode = 1;+}diff --git a/apple/checks/lint-exceptions/suppressions.json b/apple/checks/lint-exceptions/suppressions.jsonnew file mode 100644index 0000000000..d9a2c81225--- /dev/null+++ b/apple/checks/lint-exceptions/suppressions.json@@ -0,0 +1,6 @@+{+ "apple/apps/cast/Cast/Services/APIEndpoint.swift": ["NeverForceUnwrap"],+ "apple/apps/cast/Cast/ViewModels/SlideshowService.swift": [+ "NeverForceUnwrap"+ ]+}diff --git a/apple/checks/lint-exceptions/test.mjs b/apple/checks/lint-exceptions/test.mjsnew file mode 100644index 0000000000..11ece93bf7--- /dev/null+++ b/apple/checks/lint-exceptions/test.mjs@@ -0,0 +1,80 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++const check = join(import.meta.dirname, "check.mjs");+const root = mkdtempSync(join(tmpdir(), "ente-apple-lint-exceptions-"));+const file = "apple/Example.swift";+try {+ assert.equal(spawnSync("git", ["init", "-q"], { cwd: root }).status, 0);+ mkdirSync(join(root, "apple/checks/lint-exceptions"), { recursive: true });+ for (const [source, rule] of [+ [+ "// swift-format-ignore: NeverForceUnwrap\nlet value = optional!",+ "NeverForceUnwrap",+ ],+ [+ "// swiftlint:disable:next empty_count\nlet empty = values.count == 0",+ "empty_count",+ ],+ [+ "let empty = values.count == 0 // swiftlint:disable:this empty_count",+ "empty_count",+ ],+ ["/* swiftlint:disable empty_count */", "empty_count"],+ [+ "/* outer /* nested */ swiftlint:disable empty_count */",+ "empty_count",+ ],+ ]) {+ assert.equal(run(source).status, 1);+ const result = run(source, { [file]: [rule] });+ assert.equal(result.status, 0, result.stderr);+ assert.equal(run(source, { "apple/Other.swift": [rule] }).status, 1);+ }+ for (const source of [+ "// swift-format-ignore",+ "// swift-format-ignore-file",+ "// swiftlint:disable all",+ "// swiftlint:disable",+ "// swiftlint:disable empty_count force_cast",+ ]) {+ assert.equal(run(source, { [file]: ["empty_count"] }).status, 1);+ }+ for (const source of [+ "// swiftlint:enable empty_count",+ 'let text = "// swiftlint:disable empty_count"',+ 'let text = #"a "quote" // swiftlint:disable empty_count"#',+ 'let text = """\n// swift-format-ignore-file\n"""',+ 'let text = #"""\n""" // swift-format-ignore-file\n"""#',+ ]) {+ const result = run(source);+ assert.equal(result.status, 0, result.stderr);+ }+ const suppression =+ "// swiftlint:disable:next empty_count\nlet empty = values.count == 0\n";+ for (const source of [suppression + suppression, suppression]) {+ const result = run(source, { [file]: ["empty_count"] });+ assert.equal(result.status, 0, result.stderr);+ }+ for (const source of ["let value = 1", null]) {+ const result = run(source, { [file]: ["empty_count"] });+ assert.equal(result.status, 1, result.stderr);+ assert.match(result.stderr, /empty_count has no suppression/);+ assert.equal(run(source).status, 0);+ }+} finally {+ rmSync(root, { recursive: true });+}++function run(source, allowed = {}) {+ if (source === null) rmSync(join(root, file), { force: true });+ else writeFileSync(join(root, file), source);+ writeFileSync(+ join(root, "apple/checks/lint-exceptions/suppressions.json"),+ JSON.stringify(allowed),+ );+ return spawnSync(process.execPath, [check, root], { encoding: "utf8" });+}diff --git a/apple/scripts/lint.sh b/apple/scripts/lint.shindex d3cb1d8137..2abe2c30ee 100755--- a/apple/scripts/lint.sh+++ b/apple/scripts/lint.sh@@ -3,5 +3,8 @@ set -eo pipefailcd "$(dirname "$0")/.."+node checks/lint-exceptions/check.mjs+node checks/lint-exceptions/test.mjs+swift format lint --recursive --strict .swift package plugin --allow-writing-to-package-directory swiftlint lint --strict