-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6840: Implement S9363: Invalid time-zone IDs should not silently fall back to GMT #6009
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "org.sonarsource.sonarqube:sonar-server:src/test/java/org/sonar/server/issue/index/IssueIndexDebtTest.java": [ | ||
| 75 | ||
| ] | ||
| } |
150 changes: 150 additions & 0 deletions
150
java-checks-test-sources/default/src/main/java/checks/TimeZoneIdCheckSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package checks; | ||
|
|
||
| import java.util.TimeZone; | ||
|
|
||
| class TimeZoneIdCheckSample { | ||
|
|
||
| private static final String VALID_CONST = "America/Los_Angeles"; | ||
| private static final String INVALID_CONST = "America/Los_Angele"; | ||
| private static final String SPACED_CONST = "America/Los Angeles"; | ||
|
|
||
| void namedIds() { | ||
| TimeZone.getTimeZone("America/Los_Angele"); // Noncompliant {{Change this invalid time zone ID; an unrecognized identifier makes TimeZone.getTimeZone(String) return GMT with no error.}} [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("America/New_Yorkk"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("Europe/Pariss"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("Unknown/Invalid_Zone"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| void quickFixes() { | ||
| TimeZone.getTimeZone("America/Los Angeles"); // Noncompliant [[quickfixes=qf1]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf1 {{Replace spaces with underscores}} | ||
| // edit@qf1 [[sc=26;ec=47]] {{"America/Los_Angeles"}} | ||
|
|
||
| TimeZone.getTimeZone("America/New York"); // Noncompliant [[quickfixes=qf2]] | ||
| // ^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf2 {{Replace spaces with underscores}} | ||
| // edit@qf2 [[sc=26;ec=44]] {{"America/New_York"}} | ||
|
|
||
| TimeZone.getTimeZone("America/Port of Spain"); // Noncompliant [[quickfixes=qf3]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| // fix@qf3 {{Replace spaces with underscores}} | ||
| // edit@qf3 [[sc=26;ec=49]] {{"America/Port_of_Spain"}} | ||
|
|
||
| TimeZone.getTimeZone("Some Unknown City"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| void customOffsets() { | ||
| TimeZone.getTimeZone("GMT+24"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT-25"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+01:60"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT-8:99"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+1260"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+01:00:60"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+010030"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT-080030"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+0100:30"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+01:0030"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT1"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^ | ||
| TimeZone.getTimeZone("GMT12"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^ | ||
| TimeZone.getTimeZone("GMT+ "); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^ | ||
| TimeZone.getTimeZone("GMT- "); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^ | ||
| TimeZone.getTimeZone("UTC+0"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^ | ||
| TimeZone.getTimeZone("UTC+1"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^ | ||
| TimeZone.getTimeZone("UT+1"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^ | ||
| } | ||
|
|
||
| void constantsAndConcatenation() { | ||
| TimeZone.getTimeZone(VALID_CONST); // Compliant | ||
| TimeZone.getTimeZone(INVALID_CONST); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone(SPACED_CONST); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("America/" + "Los_Angeles"); // Compliant | ||
| TimeZone.getTimeZone("America/" + "Los_Angele"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("America/" + "Los Angeles"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| void dynamicAndEdgeCases(String dynamicZone) { | ||
| TimeZone.getTimeZone(dynamicZone); // Compliant | ||
| TimeZone.getTimeZone(getDynamicZone()); // Compliant | ||
| TimeZone.getTimeZone((String) null); // Compliant | ||
| TimeZone.getTimeZone(java.time.ZoneId.of("UTC")); // Compliant | ||
| java.time.ZoneId.of("America/Los_Angeles"); // Compliant | ||
| new OtherClass().getTimeZone("America/Los_Angele"); // Compliant | ||
| } | ||
|
|
||
| void compliantCases() { | ||
| TimeZone.getTimeZone("America/Los_Angeles"); | ||
| TimeZone.getTimeZone("America/New_York"); | ||
| TimeZone.getTimeZone("Europe/Paris"); | ||
| TimeZone.getTimeZone("UTC"); | ||
| TimeZone.getTimeZone("GMT"); | ||
| TimeZone.getTimeZone("PST"); | ||
| TimeZone.getTimeZone("EST"); | ||
| TimeZone.getTimeZone("HST"); | ||
| TimeZone.getTimeZone("CST"); | ||
| TimeZone.getTimeZone("JST"); | ||
| TimeZone.getTimeZone("US/Pacific"); | ||
| TimeZone.getTimeZone("Etc/GMT+8"); | ||
| TimeZone.getTimeZone("SystemV/PST8"); | ||
|
|
||
| TimeZone.getTimeZone("GMT+10"); | ||
| TimeZone.getTimeZone("GMT-8"); | ||
| TimeZone.getTimeZone("GMT+0"); | ||
| TimeZone.getTimeZone("GMT-0"); | ||
| TimeZone.getTimeZone("GMT+00:00"); | ||
| TimeZone.getTimeZone("GMT-8:00"); | ||
| TimeZone.getTimeZone("GMT+12:30"); | ||
| TimeZone.getTimeZone("GMT+0800"); | ||
| TimeZone.getTimeZone("GMT-0800"); | ||
| TimeZone.getTimeZone("GMT+123"); | ||
| TimeZone.getTimeZone("GMT-800"); | ||
| TimeZone.getTimeZone("GMT+23"); | ||
| TimeZone.getTimeZone("GMT-23"); | ||
| TimeZone.getTimeZone("GMT+23:59"); | ||
| TimeZone.getTimeZone("GMT-2359"); | ||
|
|
||
| TimeZone.getTimeZone("GMT+01:00:30"); | ||
| TimeZone.getTimeZone("GMT-8:00:00"); | ||
| TimeZone.getTimeZone("GMT+00:00:00"); | ||
| TimeZone.getTimeZone("GMT+23:59:59"); | ||
| TimeZone.getTimeZone("GMT-23:59:59"); | ||
| } | ||
|
|
||
| private String getDynamicZone() { | ||
| return "America/New_York"; | ||
| } | ||
|
|
||
| private static class OtherClass { | ||
| public TimeZone getTimeZone(String id) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
java-checks-test-sources/default/src/main/java/checks/TimeZoneIdCheck_java18.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package checks; | ||
|
|
||
| import java.util.TimeZone; | ||
|
|
||
| class TimeZoneIdCheck_java18 { | ||
|
|
||
| void customOffsetsWithSeconds() { | ||
| TimeZone.getTimeZone("GMT+01:00:30"); // Noncompliant {{Change this invalid time zone ID; an unrecognized identifier makes TimeZone.getTimeZone(String) return GMT with no error.}} [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT-8:00:00"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+00:00:00"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^ | ||
| TimeZone.getTimeZone("GMT+23:59:59"); // Noncompliant [[quickfixes=!]] | ||
| // ^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| void compliantCases() { | ||
| TimeZone.getTimeZone("America/Los_Angeles"); | ||
| TimeZone.getTimeZone("PST"); | ||
| TimeZone.getTimeZone("UTC"); | ||
| TimeZone.getTimeZone("GMT+10"); | ||
| TimeZone.getTimeZone("GMT-8:00"); | ||
| TimeZone.getTimeZone("GMT+00:00"); | ||
| TimeZone.getTimeZone("GMT-0800"); | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
java-checks/src/main/java/org/sonar/java/checks/TimeZoneIdCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * 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/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.TimeZone; | ||
| import java.util.regex.Pattern; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.java.checks.helpers.QuickFixHelper; | ||
| import org.sonar.java.checks.methods.AbstractMethodDetection; | ||
| import org.sonar.java.reporting.InternalJavaIssueBuilder; | ||
| import org.sonar.java.reporting.JavaQuickFix; | ||
| import org.sonar.java.reporting.JavaTextEdit; | ||
| import org.sonar.plugins.java.api.semantic.MethodMatchers; | ||
| import org.sonar.plugins.java.api.tree.ExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.MethodInvocationTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| @Rule(key = "S9363") | ||
| public class TimeZoneIdCheck extends AbstractMethodDetection { | ||
|
|
||
| private static final String MESSAGE = "Change this invalid time zone ID; an unrecognized identifier makes TimeZone.getTimeZone(String) return GMT with no error."; | ||
|
|
||
| private static final MethodMatchers TIME_ZONE_GET_TIME_ZONE = MethodMatchers.create() | ||
| .ofTypes("java.util.TimeZone") | ||
| .names("getTimeZone") | ||
| .addParametersMatcher("java.lang.String") | ||
| .build(); | ||
|
|
||
| private static final Set<String> AVAILABLE_ZONE_IDS = Set.copyOf(Arrays.asList(TimeZone.getAvailableIDs())); | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| private static final Pattern CUSTOM_GMT_PRE_JAVA19 = Pattern.compile( | ||
| "^GMT[+-](?:[01]?\\d|2[0-3])(?::[0-5]\\d|[0-5]\\d)?$" | ||
| ); | ||
|
|
||
| private static final Pattern CUSTOM_GMT_JAVA19_PLUS = Pattern.compile( | ||
| "^GMT[+-](?:[01]?\\d|2[0-3])(?::[0-5]\\d(?::[0-5]\\d)?|[0-5]\\d)?$" | ||
| ); | ||
|
|
||
| @Override | ||
| protected MethodMatchers getMethodInvocationMatchers() { | ||
| return TIME_ZONE_GET_TIME_ZONE; | ||
| } | ||
|
|
||
| @Override | ||
| protected void onMethodInvocationFound(MethodInvocationTree mit) { | ||
| if (!mit.methodSymbol().isStatic() || mit.arguments().isEmpty()) { | ||
| return; | ||
| } | ||
| ExpressionTree argument = mit.arguments().get(0); | ||
| Optional<String> constantValue = argument.asConstant(String.class); | ||
| if (constantValue.isEmpty()) { | ||
| return; | ||
| } | ||
| String zoneId = constantValue.get(); | ||
| if (isValidZoneId(zoneId)) { | ||
| return; | ||
| } | ||
|
|
||
| InternalJavaIssueBuilder issueBuilder = QuickFixHelper.newIssue(context) | ||
| .forRule(this) | ||
| .onTree(argument) | ||
| .withMessage(MESSAGE); | ||
|
|
||
| if (argument.is(Tree.Kind.STRING_LITERAL)) { | ||
| String proposed = zoneId.replace(' ', '_'); | ||
| if (!proposed.equals(zoneId) && AVAILABLE_ZONE_IDS.contains(proposed)) { | ||
| issueBuilder.withQuickFix(() -> JavaQuickFix.newQuickFix("Replace spaces with underscores") | ||
| .addTextEdit(JavaTextEdit.replaceTree(argument, "\"" + proposed + "\"")) | ||
| .build()); | ||
| } | ||
| } | ||
|
|
||
| issueBuilder.report(); | ||
| } | ||
|
|
||
| private boolean isValidZoneId(String zoneId) { | ||
| if (AVAILABLE_ZONE_IDS.contains(zoneId)) { | ||
| return true; | ||
| } | ||
| boolean allowsSeconds = context.getJavaVersion().isNotSet() || context.getJavaVersion().isJava19Compatible(); | ||
| Pattern customGmtPattern = allowsSeconds ? CUSTOM_GMT_JAVA19_PLUS : CUSTOM_GMT_PRE_JAVA19; | ||
| return customGmtPattern.matcher(zoneId).matches(); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
java-checks/src/test/java/org/sonar/java/checks/TimeZoneIdCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * 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/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; | ||
|
|
||
| class TimeZoneIdCheckTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/TimeZoneIdCheckSample.java")) | ||
| .withCheck(new TimeZoneIdCheck()) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_java_18() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/TimeZoneIdCheck_java18.java")) | ||
| .withCheck(new TimeZoneIdCheck()) | ||
| .withJavaVersion(18) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_java_19() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/TimeZoneIdCheckSample.java")) | ||
| .withCheck(new TimeZoneIdCheck()) | ||
| .withJavaVersion(19) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_without_semantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/TimeZoneIdCheckSample.java")) | ||
| .withCheck(new TimeZoneIdCheck()) | ||
| .withoutSemantic() | ||
| .verifyIssues(); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.