Skip to content

Commit

Permalink
SCANNPM-44 Trim property values to avoid misconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed Dec 12, 2024
1 parent f6f823f commit 29eec76
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function getBootstrapperProperties(startTimestampMs: number): ScannerProperties
* Get endpoint properties from scanner properties.
*/
export function getHostProperties(properties: ScannerProperties): ScannerProperties {
const sonarHostUrl = properties[ScannerProperty.SonarHostUrl]?.replace(/\/$/, '');
const sonarHostUrl = properties[ScannerProperty.SonarHostUrl]?.replace(/\/$/, '')?.trim();
const sonarApiBaseUrl = properties[ScannerProperty.SonarScannerApiBaseUrl];
const sonarCloudSpecified =
properties[ScannerProperty.SonarScannerSonarCloudUrl] === sonarHostUrl ||
Expand Down Expand Up @@ -427,6 +427,8 @@ function normalizeProperties(properties: ScannerProperties) {
properties[key] = '';
} else if (typeof value === 'undefined') {
delete properties[key];
} else {
properties[key] = value.toString().trim();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# The following test parsing of properties file according to the specification
# @see https://en.wikipedia.org/wiki/.properties

# The following property contains a trailing space
sonar.host.url = http://localhost:1234

# Multi-line value
sonar.exclusions=\
**/node_modules/**,\
Expand Down
13 changes: 4 additions & 9 deletions test/unit/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,22 +686,17 @@ describe('getProperties', () => {
projectHandler.reset('fake_project_with_sonar_properties');
projectHandler.setEnvironmentVariables({});

const properties = getProperties(
{
serverUrl: 'http://localhost/sonarqube',
},
projectHandler.getStartTime(),
);
const properties = getProperties({}, projectHandler.getStartTime());

expect(properties).toEqual({
...projectHandler.getExpectedProperties(),
'sonar.host.url': 'http://localhost/sonarqube',
'sonar.scanner.apiBaseUrl': 'http://localhost/sonarqube/api/v2',
'sonar.host.url': 'http://localhost:1234',
'sonar.scanner.apiBaseUrl': 'http://localhost:1234/api/v2',
'sonar.scanner.internal.isSonarCloud': 'false',
'sonar.exclusions': '**/node_modules/**,**/docs-dist/**',
'sonar.scanner.dummy.path': 'C:path\toproject',
'sonar.scanner.dummy.space.around.eq': 'value',
'sonar.scanner.dummy.whitespace.at.beginning': ' value',
'sonar.scanner.dummy.whitespace.at.beginning': 'value', // SCANNPM-44 Leading whitespaces are NOT preserved
'sonar.scanner.empty.property': '',
});
});
Expand Down

0 comments on commit 29eec76

Please sign in to comment.