Skip to content

Commit 00a1e54

Browse files
committed
fix: issue#62 strip auth key
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. Therefore, using regex expressions for stripping.
1 parent 87e78b1 commit 00a1e54

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

deepl-java/src/main/java/com/deepl/api/Translator.java

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public Translator(String authKey, TranslatorOptions options) throws IllegalArgum
4444
if (authKey == null || authKey.length() == 0) {
4545
throw new IllegalArgumentException("authKey must be a non-empty string");
4646
}
47+
authKey = StringUtil.strip(authKey);
48+
4749
String serverUrl =
4850
(options.getServerUrl() != null)
4951
? options.getServerUrl()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.deepl.api.utils;
2+
3+
public class StringUtil {
4+
/**
5+
* Java 11's String.strip() equivalent for returning a value with all leading and trailing {@link
6+
* Character#isWhitespace(int) white space} removed.
7+
*
8+
* <p>Java 8 does not support the String.strip() operation, and String.trim() only works for ASCII
9+
* whitespaces, failing to handle Unicode whitespaces, Therefore, this method sanitizes a string
10+
* value using regex expressions.
11+
*
12+
* <p>
13+
*
14+
* @param value - string value to strip
15+
* @return a string whose value is this string, with all leading and trailing white space removed
16+
*/
17+
public static String strip(String value) {
18+
if (value == null) {
19+
return null;
20+
}
21+
return value.replaceAll("^[\\p{javaWhitespace}\u00A0]+|[\\p{javaWhitespace}\u00A0]+$", "");
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.deepl.api.utils;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class StringUtilTest {
8+
9+
@Test
10+
void testStripNullString() {
11+
assertNull(StringUtil.strip(null));
12+
}
13+
14+
@Test
15+
void testStripEmptyString() {
16+
assertEquals("", StringUtil.strip(""));
17+
}
18+
19+
@Test
20+
void testStripAllWhitespace() {
21+
// ASCII whitespace
22+
assertEquals("", StringUtil.strip(" "));
23+
assertEquals("", StringUtil.strip("\t\n\r\f"));
24+
// Unicode whitespace
25+
assertEquals("", StringUtil.strip("\u00A0\u2002\u2003"));
26+
}
27+
28+
@Test
29+
void testStripLeadingTrailingWhitespace() {
30+
// ASCII
31+
assertEquals("Lorem", StringUtil.strip(" Lorem "));
32+
assertEquals("Lorem Ipsum", StringUtil.strip("\tLorem Ipsum\n"));
33+
// Unicode
34+
String input = "\u00A0\u2002Lorem Ipsum\u205F";
35+
assertEquals("Lorem Ipsum", StringUtil.strip(input));
36+
}
37+
38+
@Test
39+
void testStripNoChanges() {
40+
assertEquals("LoremIpsum", StringUtil.strip("LoremIpsum"));
41+
assertEquals("Lorem Ipsum", StringUtil.strip("Lorem Ipsum"));
42+
String input = "Lorem\u2003Ipsum"; // Unicode em space in the middle
43+
assertEquals(input, StringUtil.strip(input));
44+
}
45+
}

0 commit comments

Comments
 (0)