Repository navigation
JS-2774 Consume the shared identifier inventory. #8325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| import { strict as assert } from 'node:assert'; | ||
| import { describe, it } from 'node:test'; | ||
| import identifierUrls from '@sonarsource/analyzer-commons-configurations/identifier-urls.json' with { type: 'json' }; | ||
| import { isKnownIdentifierUrl } from './identifier-urls.js'; | ||
|
|
||
| describe('S5332 identifier URL matcher', () => { | ||
| it('uses an empty identifier inventory', () => { | ||
| assert.deepEqual(identifierUrls, {}); | ||
| }); | ||
|
|
||
| it('does not match URLs when the inventory is empty', () => { | ||
| assert.equal(isKnownIdentifierUrl('http://identifier.invalid/schema'), false); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * SonarQube JavaScript Plugin | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
|
|
||
| import identifierUrls from '@sonarsource/analyzer-commons-configurations/identifier-urls.json' with { type: 'json' }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 Cross-Repo: Commons npm package doesn't publish identifier-urls.jsonFix here and in Breaks in: This file imports Here: Once Commons publishes a release that contains identifier-urls.json, bump There: In sonar-analyzer-commons-configurations, generate identifier-urls.json (the domain-to-regex map) into the npm packaging dir, add it to In SonarSource/sonar-analyzer-commons - Was this helpful? React with 👍 / 👎 |
||
|
|
||
| // The inventory is maintained in Analyzer Commons; keep the matching semantics aligned. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Cross-PR: Padded URLs match in the Java filter but not in SonarJS · SonarSource/sonar-analyzer-commons #590Breaks when sonar-analyzer-commons#590 is released with a populated inventory and the S5332 rule checks a URL string that has leading or trailing whitespace. How
Fix
Compared Was this helpful? React with 👍 / 👎 |
||
| // A match identifies a possible identifier; the caller must also check its use in the AST. | ||
| const IDENTIFIER_URLS = new Map<string, RegExp>( | ||
| Object.entries<string>(identifierUrls).map(([domain, pattern]) => [domain, new RegExp(pattern)]), | ||
| ); | ||
|
|
||
| /** | ||
| * The Commons npm package shares the inventory as JSON. Its Java matcher, | ||
| * CleartextProtocolFilter.isKnownIdentifier(), is not exposed through that package. | ||
| * SonarJS therefore performs URL parsing and regex matching here in Node.js using the shared data. | ||
| */ | ||
| export function isKnownIdentifierUrl(value: string): boolean { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: isKnownIdentifierUrl is never called by the S5332 ruleThe PR title says S5332 now consumes the shared identifier inventory. But Was this helpful? React with 👍 / 👎 |
||
| // Do not use URL.hostname (drops ports), URL.host (normalizes :80), or a lowercased | ||
| // URL (changes identifier spelling). Only the authority is case-insensitive. | ||
| const match = /^http:\/\/([^\s/?#\\]+)([/?#][^\s\\]*)?$/i.exec(value); | ||
| if (!match) { | ||
| return false; | ||
| } | ||
| const remainder = match[2] ?? ''; | ||
| const suffix = remainder.startsWith('/') ? remainder : `/${remainder}`; | ||
| return IDENTIFIER_URLS.get(match[1].toLowerCase())?.test(suffix) === true; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Quality: Tests never exercise the URL parsing or matching logic
Both tests depend on the inventory being empty. One asserts
identifierUrlsdeep-equals{}, which will fail as soon as Commons ships any entry. The other only checks that an empty map returns false. So the authority/remainder regex, lowercasing only the authority, keeping the port, adding/before?/#suffixes, and running the per-domain regex all go untested. A bug in any of them would pass. DriveisKnownIdentifierUrlwith a fixture inventory (for example, factor the matcher to accept aMap<string, RegExp>) and test port, case, query, fragment and non-matching paths. Drop the assertion that the upstream data is empty.Was this helpful? React with 👍 / 👎