Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions its/ruling/src/test/resources/sonar-server/java-S9363.json
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
]
}
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=!]]
Comment thread
nathsou marked this conversation as resolved.
// ^^^^^^^^^^^^^^^^^^^
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;
}
}
}
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 java-checks/src/main/java/org/sonar/java/checks/TimeZoneIdCheck.java
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()));
Comment thread
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();
}
}
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();
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.
}
Loading
Loading