mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-20 07:58:03 +01:00
Restrict user-agent version matching to a more confined space.
This commit is contained in:
committed by
Jon Chambers
parent
f3b644ceb8
commit
40684a93a2
@@ -5,6 +5,7 @@ import org.whispersystems.textsecuregcm.util.Pair;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -15,66 +16,38 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class UserAgentTagUtil {
|
||||
|
||||
static final int MAX_VERSIONS = 10_000;
|
||||
public static final String PLATFORM_TAG = "platform";
|
||||
public static final String VERSION_TAG = "clientVersion";
|
||||
static final List<Tag> OVERFLOW_TAGS = List.of(Tag.of(PLATFORM_TAG, "overflow"), Tag.of(VERSION_TAG, "overflow"));
|
||||
static final List<Tag> UNRECOGNIZED_TAGS = List.of(Tag.of(PLATFORM_TAG, "unrecognized"), Tag.of(VERSION_TAG, "unrecognized"));
|
||||
|
||||
public static final String PLATFORM_TAG = "platform";
|
||||
public static final String VERSION_TAG = "clientVersion";
|
||||
private static final Map<String, Pattern> PATTERNS_BY_PLATFORM = Map.of(
|
||||
"android", Pattern.compile("^Signal-Android (4[^ ]+).*$", Pattern.CASE_INSENSITIVE),
|
||||
"desktop", Pattern.compile("^Signal Desktop (1[^ ]+).*$", Pattern.CASE_INSENSITIVE),
|
||||
"ios", Pattern.compile("^Signal/(3[^ ]+) \\(.*ios.*\\)$", Pattern.CASE_INSENSITIVE));
|
||||
|
||||
static final List<Tag> OVERFLOW_TAGS = List.of(Tag.of(PLATFORM_TAG, "overflow"), Tag.of(VERSION_TAG, "overflow"));
|
||||
static final List<Tag> UNRECOGNIZED_TAGS = List.of(Tag.of(PLATFORM_TAG, "unrecognized"), Tag.of(VERSION_TAG, "unrecognized"));
|
||||
|
||||
private static final Pattern USER_AGENT_PATTERN = Pattern.compile("^Signal[ \\-]([^ ]+) ([^ ]+).*$", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern IOS_USER_AGENT_PATTERN = Pattern.compile("^Signal/([^ ]+) \\(.*ios.*\\)$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private static final Set<Pair<String, String>> SEEN_VERSIONS = new HashSet<>();
|
||||
static final int MAX_VERSIONS = 10_000;
|
||||
private static final Set<Pair<String, String>> SEEN_VERSIONS = new HashSet<>();
|
||||
|
||||
private UserAgentTagUtil() {
|
||||
}
|
||||
|
||||
public static List<Tag> getUserAgentTags(final String userAgent) {
|
||||
final List<Tag> tags;
|
||||
if (userAgent != null) {
|
||||
for (final Map.Entry<String, Pattern> entry : PATTERNS_BY_PLATFORM.entrySet()) {
|
||||
final String platform = entry.getKey();
|
||||
final Pattern pattern = entry.getValue();
|
||||
final Matcher matcher = pattern.matcher(userAgent);
|
||||
|
||||
if (userAgent == null) {
|
||||
tags = UNRECOGNIZED_TAGS;
|
||||
} else {
|
||||
tags = getAndroidOrDesktopUserAgentTags(userAgent)
|
||||
.orElseGet(() -> getIOSUserAgentTags(userAgent)
|
||||
.orElse(UNRECOGNIZED_TAGS));
|
||||
if (matcher.matches()) {
|
||||
final String version = matcher.group(1);
|
||||
|
||||
return allowVersion(platform, version) ? List.of(Tag.of(PLATFORM_TAG, platform), Tag.of(VERSION_TAG, version)) : OVERFLOW_TAGS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static Optional<List<Tag>> getAndroidOrDesktopUserAgentTags(final String userAgent) {
|
||||
final Matcher matcher = USER_AGENT_PATTERN.matcher(userAgent);
|
||||
final Optional<List<Tag>> maybeTags;
|
||||
|
||||
if (matcher.matches()) {
|
||||
final String platform = matcher.group(1).toLowerCase();
|
||||
final String version = matcher.group(2);
|
||||
|
||||
maybeTags = Optional.of(allowVersion(platform, version) ? List.of(Tag.of(PLATFORM_TAG, platform), Tag.of(VERSION_TAG, version)) : OVERFLOW_TAGS);
|
||||
} else {
|
||||
maybeTags = Optional.empty();
|
||||
}
|
||||
|
||||
return maybeTags;
|
||||
}
|
||||
|
||||
private static Optional<List<Tag>> getIOSUserAgentTags(final String userAgent) {
|
||||
final Matcher matcher = IOS_USER_AGENT_PATTERN.matcher(userAgent);
|
||||
final Optional<List<Tag>> maybeTags;
|
||||
|
||||
if (matcher.matches()) {
|
||||
final String platform = "ios";
|
||||
final String version = matcher.group(1);
|
||||
|
||||
maybeTags = Optional.of(allowVersion(platform, version) ? List.of(Tag.of(PLATFORM_TAG, platform), Tag.of(VERSION_TAG, version)) : OVERFLOW_TAGS);
|
||||
} else {
|
||||
maybeTags = Optional.empty();
|
||||
}
|
||||
|
||||
return maybeTags;
|
||||
return UNRECOGNIZED_TAGS;
|
||||
}
|
||||
|
||||
private static boolean allowVersion(final String platform, final String version) {
|
||||
|
||||
Reference in New Issue
Block a user