-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SafeStringFormatter. Address fuzzing issue around CelValidationEx…
…ception. PiperOrigin-RevId: 601609078
- Loading branch information
1 parent
e69a4dd
commit 948fd36
Showing
10 changed files
with
134 additions
and
17 deletions.
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
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
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
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
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
48 changes: 48 additions & 0 deletions
48
common/src/main/java/dev/cel/common/internal/SafeStringFormatter.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,48 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 dev.cel.common.internal; | ||
|
||
import com.google.re2j.Pattern; | ||
import dev.cel.common.annotations.Internal; | ||
|
||
/** | ||
* {@link SafeStringFormatter} is a wrapper around JDK's {@link String#format}. It prevents any | ||
* unsafe string.format calls by only allowing known formatting specifiers to be provided. | ||
* | ||
* <p>CEL Library Internals. Do Not Use. | ||
*/ | ||
@Internal | ||
public final class SafeStringFormatter { | ||
// Allow format specifiers of %d, %f, %s and %n only. | ||
private static final Pattern FORBIDDEN_FORMAT_SPECIFIERS = Pattern.compile("%[^dfsn]"); | ||
|
||
/** | ||
* Performs a safe {@link String#format}. | ||
* | ||
* @param format A format string. Only %d, %f, %s and %n are allowed as formatting specifiers. All | ||
* other formatting specifiers will be stripped out. | ||
* @return A formatted string | ||
*/ | ||
public static String format(String format, Object... args) { | ||
if (args.length == 0) { | ||
return format; | ||
} | ||
|
||
String sanitizedMessage = FORBIDDEN_FORMAT_SPECIFIERS.matcher(format).replaceAll(""); | ||
return String.format(sanitizedMessage, args); | ||
} | ||
|
||
private SafeStringFormatter() {} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
common/src/test/java/dev/cel/common/CelValidationExceptionTest.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,42 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 dev.cel.common; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class CelValidationExceptionTest { | ||
|
||
@Test | ||
public void construct_withLargeErrorCount() { | ||
ImmutableList.Builder<CelIssue> issueBuilder = ImmutableList.builder(); | ||
for (int i = 0; i < 1500; i++) { | ||
issueBuilder.add(CelIssue.formatError(i + 1, i + 1, "generic error")); | ||
} | ||
|
||
CelValidationException celValidationException = | ||
new CelValidationException(CelSource.newBuilder().build(), issueBuilder.build()); | ||
|
||
assertThat(celValidationException.getErrors()).hasSize(1500); | ||
assertThat(celValidationException) | ||
.hasMessageThat() | ||
.endsWith("...and 500 more errors (truncated)"); | ||
} | ||
} |
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
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