Skip to content

Commit b00769f

Browse files
authored
Merge pull request #67 from rubberduck-vba/webhook
Fix CORS issues, probably
2 parents ac99633 + 511c044 commit b00769f

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

rubberduckvba.Server/Api/Admin/AdminController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Cors;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.Extensions.Options;
45

@@ -13,6 +14,7 @@ public class AdminController(ConfigurationOptions options, HangfireLauncherServi
1314
/// </summary>
1415
/// <returns>The unique identifier of the enqueued job.</returns>
1516
[Authorize("github")]
17+
[EnableCors("CorsPolicy")]
1618
[HttpPost("admin/update/xmldoc")]
1719
public IActionResult UpdateXmldocContent()
1820
{
@@ -25,6 +27,7 @@ public IActionResult UpdateXmldocContent()
2527
/// </summary>
2628
/// <returns>The unique identifier of the enqueued job.</returns>
2729
[Authorize("github")]
30+
[EnableCors("CorsPolicy")]
2831
[HttpPost("admin/update/tags")]
2932
public IActionResult UpdateTagMetadata()
3033
{

rubberduckvba.Server/Api/Admin/WebhookController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Cors;
23
using Microsoft.AspNetCore.Mvc;
34
using System.Text.Json;
45

@@ -21,6 +22,7 @@ public WebhookController(
2122
}
2223

2324
[Authorize("webhook")]
25+
[EnableCors("webhookPolicy")]
2426
[HttpPost("webhook/github")]
2527
public async Task<IActionResult> GitHub([FromBody] dynamic body) =>
2628
GuardInternalAction(() =>

rubberduckvba.Server/Api/Auth/AuthController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Cors;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.Extensions.Options;
45
using Octokit;
@@ -70,6 +71,7 @@ public IActionResult Index()
7071
}
7172

7273
[HttpPost("auth/signin")]
74+
[EnableCors("CorsPolicy")]
7375
[AllowAnonymous]
7476
public IActionResult SessionSignIn(SignInViewModel vm)
7577
{
@@ -106,6 +108,7 @@ public IActionResult SessionSignIn(SignInViewModel vm)
106108
}
107109

108110
[HttpPost("auth/github")]
111+
[EnableCors("CorsPolicy")]
109112
[AllowAnonymous]
110113
public IActionResult OnGitHubCallback(SignInViewModel vm)
111114
{

rubberduckvba.Server/ContentSynchronization/Pipeline/Sections/SyncTags/LoadGitHubTagsBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public LoadGitHubTagsBlock(PipelineSection<SyncContext> parent, CancellationToke
1616

1717
public override async Task<SyncContext> TransformAsync(SyncRequestParameters input)
1818
{
19-
var githubTags = await _github.GetAllTagsAsync(); // does not get the assets
19+
var githubTags = await _github.GetAllTagsAsync(Context.RubberduckDbMain.Name);
2020
var (gitHubMain, gitHubNext, gitHubOthers) = githubTags.GetLatestTags();
2121

2222
Context.LoadGitHubTags(gitHubMain, gitHubNext, gitHubOthers);

rubberduckvba.Server/ContentSynchronization/Pipeline/Sections/SyncXmldoc/SyncXmldocSection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ protected override async Task ActionAsync(SyncRequestParameters input)
9090
{
9191
Context.LoadParameters(input);
9292

93-
var githubTags = await _github.GetAllTagsAsync();
93+
var dbMain = await _content.GetLatestTagAsync(RepositoryId.Rubberduck, includePreRelease: false);
94+
var githubTags = await _github.GetAllTagsAsync(dbMain.Name);
9495

9596
// LoadInspectionDefaultConfig
9697
var config = await _github.GetCodeAnalysisDefaultsConfigAsync();
@@ -115,7 +116,6 @@ await Task.WhenAll([
115116

116117
await Task.Delay(TimeSpan.FromSeconds(2)); // just in case the tags job was scheduled at/around the same time
117118

118-
var dbMain = await _content.GetLatestTagAsync(RepositoryId.Rubberduck, includePreRelease: false);
119119
var dbNext = await _content.GetLatestTagAsync(RepositoryId.Rubberduck, includePreRelease: true);
120120

121121
var dbTags = _tagServices.GetAllTags().ToDictionary(e => e.Name);

rubberduckvba.Server/Program.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void Main(string[] args)
4545

4646
builder.Services.AddCors(builder =>
4747
{
48-
builder.AddDefaultPolicy(policy =>
48+
builder.AddPolicy("CorsPolicy", policy =>
4949
{
5050
policy
5151
.SetIsOriginAllowed(origin => true)
@@ -54,22 +54,6 @@ public static void Main(string[] args)
5454
.AllowCredentials()
5555
.Build();
5656
});
57-
58-
builder.AddPolicy("webhookPolicy", policy =>
59-
{
60-
policy
61-
#if DEBUG
62-
.SetIsOriginAllowed(origin => true)
63-
#else
64-
.SetIsOriginAllowedToAllowWildcardSubdomains()
65-
.WithOrigins("*.github.com")
66-
#endif
67-
.WithHeaders("Content-Type", "X-GitHub-Event", "X-GitHub-Delivery", "X-GitHub-Hook-ID", "X-Hub-Signature", "X-Hub-Signature256")
68-
.WithMethods("POST")
69-
.DisallowCredentials()
70-
.SetPreflightMaxAge(TimeSpan.FromHours(48))
71-
.Build();
72-
});
7357
});
7458

7559
builder.Services.AddAuthentication(options =>

rubberduckvba.Server/Services/GitHubClientService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using rubberduckvba.Server.ContentSynchronization.XmlDoc.Schema;
77
using rubberduckvba.Server.Model;
88
using System.Collections.Immutable;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Security.Claims;
1011
using System.Text;
1112
using System.Web;
@@ -16,13 +17,20 @@ namespace rubberduckvba.Server.Services;
1617
public interface IGitHubClientService
1718
{
1819
Task<ClaimsPrincipal?> ValidateTokenAsync(string token);
19-
Task<IEnumerable<TagGraph>> GetAllTagsAsync();
20+
Task<IEnumerable<TagGraph>> GetAllTagsAsync(string? dbMainTagName);
2021
Task<TagGraph> GetTagAsync(string? token, string name);
2122
Task<IEnumerable<InspectionDefaultConfig>> GetCodeAnalysisDefaultsConfigAsync();
2223
}
2324

2425
public class GitHubClientService(IOptions<GitHubSettings> configuration, ILogger<ServiceLogger> logger) : IGitHubClientService
2526
{
27+
private class ReleaseComparer : IEqualityComparer<Release>
28+
{
29+
public bool Equals(Release? x, Release? y) => x?.Name == y?.Name;
30+
31+
public int GetHashCode([DisallowNull] Release obj) => HashCode.Combine(obj.Name);
32+
}
33+
2634
public async Task<ClaimsPrincipal?> ValidateTokenAsync(string? token)
2735
{
2836
if (token is null)
@@ -52,13 +60,18 @@ public class GitHubClientService(IOptions<GitHubSettings> configuration, ILogger
5260
return new ClaimsPrincipal(identity);
5361
}
5462

55-
public async Task<IEnumerable<TagGraph>> GetAllTagsAsync()
63+
public async Task<IEnumerable<TagGraph>> GetAllTagsAsync(string? dbMainTagName)
5664
{
5765
var config = configuration.Value;
5866
var credentials = new Credentials(config.OrgToken);
5967
var client = new GitHubClient(new ProductHeaderValue(config.UserAgent), new InMemoryCredentialStore(credentials));
6068

61-
var releases = await client.Repository.Release.GetAll(config.OwnerOrg, config.Rubberduck, new ApiOptions { PageCount = 1, PageSize = 10 });
69+
70+
var getReleases = client.Repository.Release.GetAll(config.OwnerOrg, config.Rubberduck, new ApiOptions { PageCount = 1, PageSize = 10 });
71+
var getKnownMain = client.Repository.Release.Get(config.OwnerOrg, config.Rubberduck, dbMainTagName);
72+
await Task.WhenAll(getReleases, getKnownMain);
73+
74+
var releases = (await getReleases).Append(await getKnownMain).ToHashSet(new ReleaseComparer());
6275

6376
return (from release in releases
6477
let installer = release.Assets.SingleOrDefault(asset => asset.Name.EndsWith(".exe") && asset.Name.StartsWith("Rubberduck.Setup"))

0 commit comments

Comments
 (0)