Files
opengenesislinkserver/OpenSim/Framework/OglApprovedViewerPolicy.cs
T
OpenGenesis (MPL 2.0) 3f2629dd57 Allow official Singularity Viewer across Generation 1 login, HG and region admission (#451)
Add canonical Singularity/Singularity Viewer family recognition with scoped suffix checks; preserve existing anti-copybot guards. Cover current viewer release names and spoof-like negative cases, update default config comments and operator documentation. All 11 CI checks successful.
2026-11-16 19:36:01 +01:00

146 lines
5.6 KiB
C#

// SPDX-License-Identifier: MPL-2.0
using System;
namespace OpenSim.Framework
{
/// <summary>
/// Generation 1 admission policy for self-reported viewer names.
/// This is a compatibility allowlist, NOT verified client attestation:
/// a modified viewer can forge both the channel and version strings.
/// </summary>
public static class OglApprovedViewerPolicy
{
private static readonly string[] s_AllowedNames =
{
"Firestorm", "OpenGenesisLINK", "OGL Viewer", "Radegast-ng", "Radegast",
"WolfViewer-Release", "Frivolin Viewer", "TrixieStorm Viewer",
"Singularity", "Singularity Viewer"
};
private static readonly string[] s_KnownCopybotNames =
{
"Darkstorm", "Hydrastorm", "Copybot"
};
public static bool IsAllowed(string viewer, string channel)
{
if (ContainsKnownCopybotName(viewer) || ContainsKnownCopybotName(channel))
return false;
if (StartsWithApprovedName(viewer))
return string.IsNullOrWhiteSpace(channel) ||
StartsWithApprovedName(channel) ||
IsNumericVersion(channel);
return StartsWithApprovedName(channel) && IsNumericVersion(viewer);
}
private static bool ContainsKnownCopybotName(string value)
{
if (string.IsNullOrWhiteSpace(value))
return false;
foreach (string name in s_KnownCopybotNames)
{
if (value.Contains(name, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
private static bool StartsWithApprovedName(string value)
{
if (string.IsNullOrWhiteSpace(value))
return false;
string normalized = value.Trim();
// The official Singularity Viewer reports either "Singularity"
// or "Singularity Viewer" plus a version or release channel.
// Do not treat "Singularity ViewerFake" as a numeric version of
// the shorter "Singularity" alias.
if (normalized.StartsWith("Singularity", StringComparison.OrdinalIgnoreCase))
return IsOfficialSingularityName(normalized);
foreach (string name in s_AllowedNames)
{
if (!normalized.StartsWith(name, StringComparison.OrdinalIgnoreCase))
continue;
if (normalized.Length == name.Length)
return true;
char next = normalized[name.Length];
if (char.IsWhiteSpace(next) || next == '-' || next == '_')
return true;
}
return false;
}
private static bool IsOfficialSingularityName(string nameAndVersion)
{
// Longest variant first: an unauthorized "Singularity ViewerFake"
// must not be accepted as an extension of "Singularity".
foreach (string name in new[] { "Singularity Viewer", "Singularity" })
{
if (!nameAndVersion.StartsWith(name, StringComparison.OrdinalIgnoreCase))
continue;
string suffix = nameAndVersion.Substring(name.Length);
if (suffix.Length == 0)
return true;
// Official release/beta channels can be appended to the
// canonical family name. No arbitrary channel suffixes.
foreach (string channel in new[] { "-Release", "-Beta", "-Development" })
{
if (suffix.Equals(channel, StringComparison.OrdinalIgnoreCase))
return true;
if (suffix.StartsWith(channel + " ", StringComparison.OrdinalIgnoreCase) &&
IsNumericVersion(suffix.Substring(channel.Length).Trim()))
return true;
}
if (suffix.Length > 1 && char.IsWhiteSpace(suffix[0]))
{
string version = suffix.Trim();
if (IsNumericVersion(version))
return true;
// A few viewer login strings append a release indicator
// after an otherwise numeric build, e.g. "1.8.9 (8338) Release".
const string release = " Release";
if (version.EndsWith(release, StringComparison.OrdinalIgnoreCase) &&
IsNumericVersion(version.Substring(0, version.Length - release.Length)))
return true;
}
}
return false;
}
private static bool IsNumericVersion(string value)
{
if (string.IsNullOrWhiteSpace(value))
return true;
string normalized = value.Trim();
// Official OGL viewer builds may report v0.4.0.0 instead of
// 0.4.0.0. Allow a single leading v/V only when followed by
// digits; all other prefixes and viewer families stay blocked.
if (normalized.Length > 1 &&
(normalized[0] == 'v' || normalized[0] == 'V') &&
char.IsDigit(normalized[1]))
normalized = normalized.Substring(1);
if (!char.IsDigit(normalized[0]))
return false;
foreach (char c in normalized)
{
if (!char.IsDigit(c) && c != '.' && c != '-' &&
c != '_' && c != '(' && c != ')' && !char.IsWhiteSpace(c))
return false;
}
return true;
}
}
}