Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(salesforce): drop merge-field and regex false secrets, and classify static-resource libraries as vendored (#4468)

Raghav Chamadiya0b7298b30bd4
diff --git a/docs/architecture/language-support.md b/docs/architecture/language-support.md
index 735e57799..ce931f749 100644
--- a/docs/architecture/language-support.md
+++ b/docs/architecture/language-support.md
@@ -991,6 +991,12 @@ Lightning or project file writes its name in any case, strings included, since
`Type.forName` reaches classes by name. Ceiling: a mention in a comment counts as
a use, and a class shaped like a Visualforce controller stays live.
+A static resource (`staticresources/`, or an NPSP-style `StaticResourceSources/`
+mirror) is vendored when it sits in a library's release folder (`arbor-v0.92/`)
+or opens with another project's versioned license banner. Ceiling: a library
+copied with neither, such as a minified Google client with no banner, stays
+production.
+
#### Lightning Web Components, Aura and Visualforce
LWC, Aura, Visualforce and flows reach Apex classes by the same rules from the
diff --git a/docs/architecture/security.md b/docs/architecture/security.md
index 0ea5ab67c..2f8724ec6 100644
--- a/docs/architecture/security.md
+++ b/docs/architecture/security.md
@@ -24,7 +24,7 @@ comment or a quoted string can fire it.
A keyword secret value is skipped when it is elided or templated (`...`,
`<your key>`, `xxx`, `example`, `placeholder`, `dummy`, `fake`, `redacted`,
-`changeme`, `{{ }}`, `${}`), shorter than eight characters, a short single-case
+`changeme`, `{{ }}`, `${}`, a Salesforce `{!merge.field}`), a regular expression, shorter than eight characters, a short single-case
word, or a snake_case key name. A value a vendor kind already reports is not
reported twice.
diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.md
index d2a3c0827..8df73eea6 100644
--- a/docs/layers/LANGUAGE_SUPPORT.md
+++ b/docs/layers/LANGUAGE_SUPPORT.md
@@ -115,7 +115,7 @@ paragraph of its own `PERFORM THRU` range; it never deducts from a score.
A paragraph whose performer is in another file (a copybook's) is judged by the
target's name alone.
Apex dead-code findings are review-only, since the platform also reaches classes
-by name.
+by name. A third-party library in a static resource counts as vendored.
## How accurate is the graph per language
diff --git a/packages/core/src/repowise/core/analysis/security/credentials.py b/packages/core/src/repowise/core/analysis/security/credentials.py
index ccfe24de5..c5a9f7bc3 100644
--- a/packages/core/src/repowise/core/analysis/security/credentials.py
+++ b/packages/core/src/repowise/core/analysis/security/credentials.py
@@ -46,10 +46,14 @@ def _is_valid_credential_value(val: str) -> bool:
# is a key's or marker's name, a constant holding its own name, and a template
# placeholder or shell substitution is filled in when it runs; none of them is a
# credential. Letters only: a digit or capital makes it look like a key.
-_TEMPLATE_VALUE = re.compile(r"\{\{.*?\}\}|\$\{.*?\}|\$\(.*?\)")
+# ``{!Contact.Name}`` is a Salesforce merge field (Visualforce, formulas, flows,
+# email templates) and ``{#v.value}`` an Aura unbound expression.
+_TEMPLATE_VALUE = re.compile(r"\{\{.*?\}\}|\$\{.*?\}|\$\(.*?\)|\{[!#].*?\}")
_SCHEME_TEMPLATE_VALUE = re.compile(
r"(?i:(?:bearer|basic|token))\s+(?:\{\{.*?\}\}|\$\{.*?\}|\$\(.*?\))"
)
+# A character range with a quantifier is a regular expression (``(_D[0-9]+D_)``).
+_REGEX_VALUE = re.compile(r"\[\^?[^\]]*\w-\w[^\]]*\][+*?{]")
_OWN_NAME_VALUE = re.compile(r"[A-Za-z_-]+")
_CONSTANT_NAME = re.compile(r"[A-Z][A-Z0-9_]*")
_KEY_NAME_VALUE = re.compile(r"[a-z]+(?:[_-][a-z]+)+|[a-z_]*_[a-z_]*")
@@ -110,6 +114,7 @@ def _is_secret_value(kind: str, val: str, name: str = "") -> bool:
return not (
_TEMPLATE_VALUE.search(value)
or _SCHEME_TEMPLATE_VALUE.search(value)
+ or _REGEX_VALUE.search(value)
or _is_plain_word(value)
or value.startswith("--")
)
diff --git a/packages/core/src/repowise/core/analysis/security/patterns.py b/packages/core/src/repowise/core/analysis/security/patterns.py
index 9e4d84bcd..6bf9a4fd5 100644
--- a/packages/core/src/repowise/core/analysis/security/patterns.py
+++ b/packages/core/src/repowise/core/analysis/security/patterns.py
@@ -11,7 +11,7 @@ import re
# the same way ``HEALTH_ANALYZER_VERSION`` forces a full health re-score
# (#3072). Without this, a scanner fix only ever reaches a file a user
# happens to edit.
-SECURITY_SCANNER_VERSION = 4
+SECURITY_SCANNER_VERSION = 5
# ---------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/code_origin.py b/packages/core/src/repowise/core/code_origin.py
index 5f23394da..036e3ddae 100644
--- a/packages/core/src/repowise/core/code_origin.py
+++ b/packages/core/src/repowise/core/code_origin.py
@@ -338,15 +338,34 @@ def _is_vendored_path(lowered_name: str, dirs: list[str]) -> bool:
return True
if _DEPS_DIR in dirs[:-1] and lowered_name.endswith(_deps_suffixes()):
return True
- return lowered_name.endswith(_MINIFIED_SUFFIXES)
+ return lowered_name.endswith(_MINIFIED_SUFFIXES) or _in_static_release_dir(dirs)
+
+
+# A library unpacked under its release name in a static resource
+# (``staticresources/arbor-v0.92/``). Elsewhere a versioned folder is as often an
+# API version or the repository's own release. Ceiling: a team's own resource
+# folder named like a release (``app-v2.1/``) reads as vendored too.
+_RELEASE_DIR_RE = re.compile(r"^[a-z][\w.-]*?[-_.]v?\d+(?:\.\d+)+$")
+
+
+def _in_static_release_dir(dirs: list[str]) -> bool:
+ for i, d in enumerate(dirs):
+ if d in _STATIC_RESOURCE_DIRS:
+ return any(_RELEASE_DIR_RE.match(s) for s in dirs[i + 1 :])
+ return False
# A copied library is recognisable by its header only in the directories a
# site serves assets from. Anywhere else a license header is far more often
# the repository's own.
# ``min/`` is a minified distribution tree (Monaco ships as ``min/vs/...``); the
-# header rule below keeps a first-party ``src/min/`` out.
-_ASSET_DIR_TOKENS = frozenset({"docs", "doc", "static", "assets", "public", "_static", "min"})
+# header rule below keeps a first-party ``src/min/`` out. Salesforce serves its
+# static resources by name, and NPSP-style repos keep their sources in a mirror.
+_STATIC_RESOURCE_DIRS = frozenset({"staticresources", "staticresourcesources"})
+_ASSET_DIR_TOKENS = (
+ frozenset({"docs", "doc", "static", "assets", "public", "_static", "min"})
+ | _STATIC_RESOURCE_DIRS
+)
_HEADER_LINES = 20
_LICENSE_RE = re.compile(
r"copyright|\(c\)|©|@license|licen[cs]ed\s+under|released\s+under|\bMIT\b",
diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.py
index 65dd26887..fa7b29827 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -1437,6 +1437,11 @@ class TestSecretPrecision:
("src/app.py", 'API_KEY = "prefix-${VAR}-suffix"\n'),
("src/app.py", 'TOKEN = "prefix-{{var}}-suffix"\n'),
("src/app.py", 'TOKEN = "xxxxxxxxxxxx"\n'),
+ ("classes/RD2_NamingService.cls", "String token = '{!npe03__Donor_Name__c}';\n"),
+ ("pages/Pay.page", '<c:pay accessToken="{!$Credential.OAuthToken}"/>\n'),
+ ("aura/Pay/Pay.cmp", '<c:pay token="{#v.sessionToken}"/>\n'),
+ ("classes/Api.cls", "String token = 'Bearer {!$Credential.OAuthToken}';\n"),
+ ("classes/RegExps.cls", "static final String EXP_NUMBER_TOKEN = '(_D[0-9]+D_)';\n"),
],
)
def test_elided_or_templated_values_are_not_secrets(self, path: str, source: str) -> None:
@@ -1475,6 +1480,7 @@ class TestSecretPrecision:
("source", "kind"),
[
('TOKEN = "xQzRmWpLkVnBtYcH"\n', "hardcoded_secret"),
+ ('TOKEN = "Zx9[a]+Qw8Rt5Yu3"\n', "hardcoded_secret"),
('TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9abc"\n', "hardcoded_secret"),
('const val API_TOKEN = "productionAccessKey"\n', "hardcoded_secret"),
('password="Tr0ub4dor 3xyz"\n', "hardcoded_password"),
@@ -1573,6 +1579,12 @@ class TestNoAlarmOnCodeThatDoesNotShip:
def test_declaration_file_holds_no_sink(self) -> None:
assert self._found("types/react.d.ts", "type T = { __html: SafeHtml };\n") == []
+ def test_a_library_release_in_a_static_resource_holds_no_sink(self) -> None:
+ source = 'eval("console.log("+args.join(",")+")")\n'
+ resources = "force-app/main/default/staticresources/Libs"
+ assert self._found(f"{resources}/arbor-v0.92/src/etc.js", source) == []
+ assert self._found(f"{resources}/app/main.js", source) == [("eval_call", "high")]
+
def test_inner_html_from_a_value_is_still_medium(self) -> None:
source = "<div dangerouslySetInnerHTML={{__html: foo}} />\n"
assert self._found("src/view.tsx", source) == [("unsafe_inner_html", "med")]
diff --git a/tests/unit/test_code_origin.py b/tests/unit/test_code_origin.py
index 06b745ecc..2a9e9fcfc 100644
--- a/tests/unit/test_code_origin.py
+++ b/tests/unit/test_code_origin.py
@@ -144,6 +144,14 @@ _OWN_HEADER = (
("dist/Main.Bundle.JS", "generated"),
("src/Forms/Main.designer.cs", "generated"),
("src/logging/diagnostic-support-bundle.ts", "production"),
+ # a library release unpacked in a Salesforce static resource, or its source mirror
+ ("force-app/main/default/staticresources/Libs/arbor-v0.92/src/etc.js", "vendored"),
+ ("StaticResourceSources/arbor-v0.92/lib/arbor.js", "vendored"),
+ ("src/staticresources/jquery-ui-1.12.1/ui.js", "vendored"),
+ ("force-app/main/default/staticresources/npsp-slds/modal.js", "production"),
+ ("force-app/main/default/staticresources/lmsvf/templates.js", "production"),
+ ("static/arbor-v0.92/src/etc.js", "production"), # static resources only
+ ("src/api/v1.2/handler.js", "production"),
],
)
def test_path_rules(path: str, expected: str) -> None:
@@ -291,10 +299,22 @@ def test_path_only_vendored_or_generated() -> None:
assert not is_vendored_or_generated_path("src/Monaco/monacoSRC/min/vs/loader.js")
assert not is_vendored_or_generated_path("src/pkg/external/client.py")
assert not is_vendored_or_generated_path("tests/test_app.py")
+ assert is_vendored_or_generated_path("src/staticresources/Libs/arbor-v0.92/src/etc.js")
+ assert not is_vendored_or_generated_path("src/staticresources/Libs/npsp-slds/modal.js")
@pytest.mark.parametrize("header", [_JQUERY, _TYPEAHEAD])
[email protected]("folder", ["docs/_static", "static/js", "assets", "public/lib"])
+ "folder",
+ [
+ "docs/_static",
+ "static/js",
+ "assets",
+ "public/lib",
+ "force-app/main/default/staticresources/Bootstrap/js",
+ "StaticResourceSources/jquery",
+ ],
+)
def test_third_party_header_under_asset_dirs_is_vendored(header: str, folder: str) -> None:
assert code_origin(f"{folder}/lib.js", header) == "vendored"