Validate scopes on /api/v1/apps
This commit is contained in:
parent
a734583b15
commit
8b7c227619
2 changed files with 28 additions and 3 deletions
|
@ -45,6 +45,18 @@ public class MastodonAuthController(DatabaseContext db) : Controller {
|
||||||
if (request.RedirectUris.Any(p => !MastodonOauthHelpers.ValidateRedirectUri(p)))
|
if (request.RedirectUris.Any(p => !MastodonOauthHelpers.ValidateRedirectUri(p)))
|
||||||
throw GracefulException.BadRequest("redirect_uris parameter contains invalid protocols");
|
throw GracefulException.BadRequest("redirect_uris parameter contains invalid protocols");
|
||||||
|
|
||||||
|
if (!MastodonOauthHelpers.ValidateScopes(request.Scopes))
|
||||||
|
throw GracefulException.BadRequest("Invalid scopes parameter");
|
||||||
|
|
||||||
|
if (request.Website != null)
|
||||||
|
try {
|
||||||
|
var uri = new Uri(request.Website);
|
||||||
|
if (!uri.IsAbsoluteUri || uri.Scheme is "http" or "https") throw new Exception();
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
throw GracefulException.BadRequest("Invalid website URL");
|
||||||
|
}
|
||||||
|
|
||||||
var app = new OauthApp {
|
var app = new OauthApp {
|
||||||
Id = IdHelpers.GenerateSlowflakeId(),
|
Id = IdHelpers.GenerateSlowflakeId(),
|
||||||
ClientId = CryptographyHelpers.GenerateRandomString(32),
|
ClientId = CryptographyHelpers.GenerateRandomString(32),
|
||||||
|
|
|
@ -40,6 +40,15 @@ public static class MastodonOauthHelpers {
|
||||||
"write:mutes"
|
"write:mutes"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
private static readonly List<string> ScopeGroups = [
|
||||||
|
"read",
|
||||||
|
"write",
|
||||||
|
"follow",
|
||||||
|
"push"
|
||||||
|
];
|
||||||
|
|
||||||
|
private static readonly List<string> ForbiddenSchemes = ["javascript", "file", "data", "mailto", "tel"];
|
||||||
|
|
||||||
public static IEnumerable<string> ExpandScopes(IEnumerable<string> scopes) {
|
public static IEnumerable<string> ExpandScopes(IEnumerable<string> scopes) {
|
||||||
var res = new List<string>();
|
var res = new List<string>();
|
||||||
foreach (var scope in scopes) {
|
foreach (var scope in scopes) {
|
||||||
|
@ -49,15 +58,19 @@ public static class MastodonOauthHelpers {
|
||||||
res.AddRange(WriteScopes);
|
res.AddRange(WriteScopes);
|
||||||
if (scope == "follow")
|
if (scope == "follow")
|
||||||
res.AddRange(FollowScopes);
|
res.AddRange(FollowScopes);
|
||||||
else {
|
else
|
||||||
res.Add(scope);
|
res.Add(scope);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.Distinct();
|
return res.Distinct();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly List<string> ForbiddenSchemes = ["javascript", "file", "data", "mailto", "tel"];
|
public static bool ValidateScopes(List<string> scopes) {
|
||||||
|
if (scopes.Distinct().Count() < scopes.Count) return false;
|
||||||
|
|
||||||
|
var validScopes = ScopeGroups.Concat(ReadScopes).Concat(WriteScopes).Concat(FollowScopes);
|
||||||
|
return !scopes.Except(validScopes).Any();
|
||||||
|
}
|
||||||
|
|
||||||
public static bool ValidateRedirectUri(string uri) {
|
public static bool ValidateRedirectUri(string uri) {
|
||||||
if (uri == "urn:ietf:wg:oauth:2.0:oob") return true;
|
if (uri == "urn:ietf:wg:oauth:2.0:oob") return true;
|
||||||
|
|
Loading…
Add table
Reference in a new issue