Skip to content

Commit

Permalink
fix: issue#62 strip auth key
Browse files Browse the repository at this point in the history
Fixing the issue raised in #62

Problem: auth key with leading / trailing whitespaces is throwing exceptions. For example, assigning the secret via `echo` leaves a trailing `\n`.

Solution: strip input auth key as an input sanitization procedure

Notice: Java 8 does not support `String.strip()`, while `String.trim()` does not support Unicode whitespaces. We anticipate ASCII only input and utilizing `trim()` for simplicity.
  • Loading branch information
timazhum committed Feb 17, 2025
1 parent 87e78b1 commit b1f7887
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions deepl-java/src/main/java/com/deepl/api/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ public class Translator {
*/
@Deprecated
public Translator(String authKey, TranslatorOptions options) throws IllegalArgumentException {
if (authKey == null || authKey.length() == 0) {
throw new IllegalArgumentException("authKey must be a non-empty string");
if (authKey == null) {
throw new IllegalArgumentException("authKey cannot be null");
}

authKey = authKey.trim();

if (authKey.isEmpty()) {
throw new IllegalArgumentException("authKey cannot be empty");
}

String serverUrl =
(options.getServerUrl() != null)
? options.getServerUrl()
Expand Down
10 changes: 10 additions & 0 deletions deepl-java/src/test/java/com/deepl/api/GeneralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ void testEmptyAuthKey() {
});
}

@Test
void testNullAuthKey() {
IllegalArgumentException thrown =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
Translator translator = new Translator(null);
});
}

@Test
void testInvalidAuthKey() {
String authKey = "invalid";
Expand Down

0 comments on commit b1f7887

Please sign in to comment.