Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor X-Hub-Signature validation #24

Closed
nwf opened this issue Jan 15, 2022 · 5 comments · May be fixed by #262
Closed

Refactor X-Hub-Signature validation #24

nwf opened this issue Jan 15, 2022 · 5 comments · May be fixed by #262
Labels
Status: Needs info Full requirements are not yet known, so implementation should not be started Status: Stale Used by stalebot to clean house Type: Feature New feature or request

Comments

@nwf
Copy link

nwf commented Jan 15, 2022

Describe the feature

private static async Task<bool> VerifySignatureAsync(HttpContext context, string secret, string body)
requires a HttpContext object only for its header and for generating a response in the case of failure. Unfortunately. HttpContexts are sometimes hard to come by; for example, Azure Functions, at least as of runner version 4, for example, provide only HttpRequests and not the full HttpContext.

Therefore, it would be quite nice and DRY if this method could be split into a worker and wrapper, with the worker taking the already-extracted signature header and returning a HttpStatusCode and String for the body, and the wrapper adapting for the ASP.Net HttpContext object.

@JamieMagee
Copy link
Contributor

JamieMagee commented Jan 16, 2022

Okay, so to clarify you'd like an interface that looks something like this:

public static class GitHubWebhookExtensionsWorker
{
    public static VerifyResult VerifyContentType(HttpRequest request, string expectedContentType);

    private static async Task<string> GetBodyAsync(HttpRequest request);

    private static async Task<VerifyResult> VerifySignatureAsync(HttpRequest request, string secret, string body)
}

where VerifyResult is something like:

public sealed record VerifyResult
{
    public bool IsSuccess { get; init; } = null!;

    public int? ResponseCode { get; init; }

    public string? ResponseMessage { get; init; }
}

and MapGitHubWebhooks will call GitHubWebhookExtensionsWorker methods checking IsSuccess?

@nwf
Copy link
Author

nwf commented Jan 16, 2022

That seems plausible, though maybe GitHubWebhookUtilities rather than ...ExtensionsWorker?

Is it possible to make VerifyResult parametric on its success case? Then VerifySignatureAsync can return VerifyResult<bool> and MapGitHubWebhooks could also be split into the preflight checks (fairly reusable) and flight (ASP.Net specific) bits? That is, pull

// Verify content type
if (!VerifyContentType(context, MediaTypeNames.Application.Json))
{
return;
}
// Get body
var body = await GetBodyAsync(context).ConfigureAwait(false);
// Verify signature
if (!await VerifySignatureAsync(context, secret, body).ConfigureAwait(false))
{
return;
}
out to their own method (adjusted to take a HttpRequest and to return a Task<VerifyResult<string>> capturing the body of a verified message) in the worker class?

@nwf
Copy link
Author

nwf commented Jan 17, 2022

Ah, one more thought re: MapGitHubWebhooks, while in the neighborhood: should there be a limit on the content length done before pulling the entire body into memory?

nwf pushed a commit to nwf/octokit-webhooks.net that referenced this issue Jan 17, 2022
@nickfloyd nickfloyd added Priority: Low Type: Feature New feature or request Status: Needs info Full requirements are not yet known, so implementation should not be started and removed priority-4-low labels Oct 26, 2022
@github-actions
Copy link

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label Jul 24, 2023
@github-actions github-actions bot removed the Status: Stale Used by stalebot to clean house label Jul 25, 2023
Copy link

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label Apr 21, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs info Full requirements are not yet known, so implementation should not be started Status: Stale Used by stalebot to clean house Type: Feature New feature or request
Projects
None yet
3 participants