Skip to content

Commit a40bab5

Browse files
adietishsbouchetfbricon
committed
fix: validate uuid loaded from disk (#68)
Signed-off-by: Andre Dietisheim <[email protected]> Co-authored-by: Stephane Bouchet <[email protected]> Co-authored-by: Fred Bricon <[email protected]>
1 parent df761a0 commit a40bab5

File tree

2 files changed

+128
-11
lines changed
  • src
    • main/java/com/redhat/devtools/intellij/telemetry/core/service
    • test/java/com/redhat/devtools/intellij/telemetry/core/service

2 files changed

+128
-11
lines changed

src/main/java/com/redhat/devtools/intellij/telemetry/core/service/UserId.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;
1717

1818
import java.io.IOException;
19-
import java.io.Writer;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221
import java.util.UUID;
22+
import java.util.regex.Pattern;
2323
import java.util.stream.Stream;
2424

2525
public class UserId {
@@ -28,26 +28,37 @@ public class UserId {
2828

2929
public static final UserId INSTANCE = new UserId();
3030
private static final Path UUID_FILE = Directories.RED_HAT.resolve("anonymousId");
31-
31+
private static final Pattern UUID_REGEX =
32+
Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
3233
private final Lazy<String> uuid = new Lazy<>(() -> loadOrCreate(UUID_FILE));
3334

34-
private UserId() {}
35+
/** for testing purposes */
36+
protected UserId() {}
3537

3638
public String get() {
3739
return uuid.get();
3840
}
3941

4042
private String loadOrCreate(Path file) {
41-
if (Files.exists(file)) {
42-
return load(file);
43-
} else {
44-
String uuid = UUID.randomUUID().toString();
45-
write(uuid, file);
46-
return uuid;
43+
String uuid = null;
44+
if (exists(file)) {
45+
uuid = load(file);
46+
if (isValid(uuid)) {
47+
return uuid;
48+
}
4749
}
50+
uuid = create();
51+
write(uuid, file);
52+
return uuid;
53+
}
54+
55+
/** for testing purposes */
56+
protected boolean exists(Path file) {
57+
return Files.exists(file);
4858
}
4959

50-
private String load(Path uuidFile) {
60+
/** for testing purposes */
61+
protected String load(Path uuidFile) {
5162
String uuid = null;
5263
try(Stream<String> lines = Files.lines(uuidFile)) {
5364
uuid = lines
@@ -60,7 +71,19 @@ private String load(Path uuidFile) {
6071
return uuid;
6172
}
6273

63-
private void write(String uuid, Path uuidFile) {
74+
private boolean isValid(String uuid) {
75+
if (uuid == null) {
76+
return false;
77+
}
78+
return UUID_REGEX.matcher(uuid).matches();
79+
}
80+
81+
private String create() {
82+
return UUID.randomUUID().toString();
83+
}
84+
85+
/** for testing purposes */
86+
protected void write(String uuid, Path uuidFile) {
6487
try {
6588
FileUtils.createFileAndParent(uuidFile);
6689
FileUtils.write(uuid, uuidFile);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.intellij.telemetry.core.service;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.nio.file.Path;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
public class UserIdTest {
20+
21+
private static final String UUID = "123e4567-e89b-12d3-a456-426614174000";
22+
private static final String INVALID_UUID = "bogus";
23+
24+
@Test
25+
void get_should_load_and_not_write_if_file_exists_and_has_valid_UUID() {
26+
// given
27+
TestableUserId user = new TestableUserId(true, UUID);
28+
// when
29+
user.get();
30+
// then
31+
assertThat(user.loaded).isTrue();
32+
assertThat(user.written).isFalse();
33+
}
34+
35+
@Test
36+
void get_should_not_load_if_file_doesnt_exist() {
37+
// given
38+
TestableUserId user = new TestableUserId(false, UUID);
39+
// when
40+
user.get();
41+
// then
42+
assertThat(user.loaded).isFalse();
43+
}
44+
45+
@Test
46+
void get_should_write_if_file_doesnt_exist() {
47+
// given
48+
TestableUserId user = new TestableUserId(false, null);
49+
// when
50+
user.get();
51+
// then
52+
assertThat(user.written).isTrue();
53+
}
54+
55+
@Test
56+
void get_should_write_if_file_exists_but_is_invalid() {
57+
// given
58+
TestableUserId user = new TestableUserId(true, INVALID_UUID);
59+
// when
60+
user.get();
61+
// then
62+
assertThat(user.written).isTrue();
63+
}
64+
65+
private static class TestableUserId extends UserId {
66+
67+
private final boolean exists;
68+
private final String loadedUUID;
69+
boolean loaded = false;
70+
boolean written = false;
71+
72+
public TestableUserId(boolean exists, String loadedUUID) {
73+
this.exists = exists;
74+
this.loadedUUID = loadedUUID;
75+
}
76+
77+
@Override
78+
public boolean exists(Path file) {
79+
return exists;
80+
}
81+
82+
@Override
83+
public String load(Path file) {
84+
this.loaded = true;
85+
return loadedUUID;
86+
}
87+
88+
@Override
89+
protected void write(String uuid, Path uuidFile) {
90+
this.written = true;
91+
}
92+
}
93+
94+
}

0 commit comments

Comments
 (0)