-
Notifications
You must be signed in to change notification settings - Fork 135
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
feature: add LogsafeArgName errorprone rule #1459
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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 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
89 changes: 89 additions & 0 deletions
89
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LogsafeArgName.java
This file contains 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,89 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.BugPattern.SeverityLevel; | ||
import com.google.errorprone.ErrorProneFlags; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.CompileTimeConstantExpressionMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.Matchers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "LogsafeArgName", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
providesFix = BugPattern.ProvidesFix.REQUIRES_HUMAN_ATTENTION, | ||
severity = SeverityLevel.ERROR, | ||
summary = "Prevent certain argument names from being logged as safe.") | ||
public final class LogsafeArgName extends BugChecker implements MethodInvocationTreeMatcher { | ||
static final String UNSAFE_ARG_NAMES_FLAG = "LogsafeArgName:UnsafeArgNames"; | ||
|
||
private static final Matcher<ExpressionTree> SAFE_ARG_OF = | ||
Matchers.staticMethod().onClass("com.palantir.logsafe.SafeArg").named("of"); | ||
private final Matcher<ExpressionTree> compileTimeConstExpressionMatcher = | ||
new CompileTimeConstantExpressionMatcher(); | ||
|
||
private final Set<String> unsafeParamNames; | ||
|
||
// Must have default constructor for service loading to work correctly | ||
public LogsafeArgName() { | ||
this.unsafeParamNames = ImmutableSet.of(); | ||
} | ||
|
||
public LogsafeArgName(ErrorProneFlags flags) { | ||
this.unsafeParamNames = | ||
flags.getList(UNSAFE_ARG_NAMES_FLAG).map(ImmutableSet::copyOf).orElseGet(ImmutableSet::of); | ||
} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (unsafeParamNames.isEmpty() || !SAFE_ARG_OF.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
List<? extends ExpressionTree> args = tree.getArguments(); | ||
ExpressionTree argNameExpression = args.get(0); | ||
if (compileTimeConstExpressionMatcher.matches(argNameExpression, state)) { | ||
String argName = (String) ((JCTree.JCLiteral) argNameExpression).getValue(); | ||
if (unsafeParamNames.stream().anyMatch(unsafeArgName -> unsafeArgName.equalsIgnoreCase(argName))) { | ||
return buildDescription(tree) | ||
.setMessage("Arguments with name '" + argName + "' must be marked as unsafe.") | ||
.addFix(SuggestedFix.builder() | ||
.replace(tree.getMethodSelect(), "UnsafeArg.of") | ||
.addImport("com.palantir.logsafe.UnsafeArg") | ||
.build()) | ||
.build(); | ||
} | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LogsafeArgNameTest.java
This file contains 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,111 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.ErrorProneFlags; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public final class LogsafeArgNameTest { | ||
private CompilationTestHelper compilationHelper; | ||
private RefactoringValidator refactoringHelper; | ||
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. Slight preference to use factory methods for these, that way we can fluently build the test from the factory. |
||
|
||
@BeforeEach | ||
public void before() { | ||
compilationHelper = CompilationTestHelper.newInstance(LogsafeArgName.class, getClass()) | ||
.setArgs(ImmutableList.of("-XepOpt:" + LogsafeArgName.UNSAFE_ARG_NAMES_FLAG + "=foo,bar")); | ||
refactoringHelper = RefactoringValidator.of( | ||
new LogsafeArgName(ErrorProneFlags.builder() | ||
.putFlag(LogsafeArgName.UNSAFE_ARG_NAMES_FLAG, "foo,bar") | ||
.build()), | ||
getClass()); | ||
} | ||
|
||
@Test | ||
public void catches_unsafe_arg_name() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.palantir.logsafe.SafeArg;", | ||
"class Test {", | ||
" void f() {", | ||
" // BUG: Diagnostic contains: must be marked as unsafe", | ||
" SafeArg.of(\"foo\", 1);", | ||
" }", | ||
"", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void catches_case_insensitive_unsafe_arg_name() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.palantir.logsafe.SafeArg;", | ||
"class Test {", | ||
" void f() {", | ||
" // BUG: Diagnostic contains: must be marked as unsafe", | ||
" SafeArg.of(\"Foo\", 1);", | ||
" }", | ||
"", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void fixes_unsafe_arg_name() { | ||
refactoringHelper | ||
.addInputLines( | ||
"Test.java", | ||
"import com.palantir.logsafe.SafeArg;", | ||
"class Test {", | ||
" void f() {", | ||
" SafeArg.of(\"Foo\", 1);", | ||
" }", | ||
"", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import com.palantir.logsafe.SafeArg;", | ||
"import com.palantir.logsafe.UnsafeArg;", | ||
"class Test {", | ||
" void f() {", | ||
" UnsafeArg.of(\"Foo\", 1);", | ||
" }", | ||
"", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void ignores_safe_arg_names() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.palantir.logsafe.SafeArg;", | ||
"class Test {", | ||
" void f() {", | ||
" SafeArg.of(\"baz\", 1);", | ||
" }", | ||
"", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains 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,6 @@ | ||
type: feature | ||
feature: | ||
description: add LogsafeArgName errorprone rule which allows users to specify a | ||
list of argument names that must always be tagged as unsafe. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1459 |
This file contains 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
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.
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.
SuggestedFixes.qualifyType on "com.palantir.logsafe.UnsafeArg" will add the import automagically, or fully qualify the type if there happens to be a
foo.bar.UnsafeArg
already imported. We can append".of"
to the result.