【问题标题】:GitHub GitHub Webhook Secret ValidationGitHub GitHub Webhook 秘密验证
【发布时间】:2021-10-06 22:33:49
【问题描述】:

我正在尝试使用 API Gateway 验证 GitHub Webhook 密码。

这是我的 lambda:

import json
import hmac
import hashlib
import re


GITHUB_SECRET = 'HELLO WORLD' # from Github UI


def lambda_handler(event, context):
    print("Lambda execution starting up...")

    incoming_signature = re.sub(r'^sha1=', '', event['headers']['X-Hub-Signature'])
    enhanced_body_msg = json.dumps(event['body'], default=str)
    calculated_signature = calculate_signature(GITHUB_SECRET, enhanced_body_msg.encode('utf-8'))

    print("Incoming sig:", incoming_signature)
    print("calculated_signature:", calculated_signature)
    if incoming_signature != calculated_signature:
        print('Unauthorized attempt')
        return {
            'statusCode': 403,
            'body': json.dumps('Forbidden')
        }

    print('Request successfully authorized')

    # do stuff in Lambda

    return {
        'statusCode': 200,
        'body': json.dumps(f'Work in progress')
    }


def calculate_signature(github_signature, githhub_payload):
    signature_bytes = bytes(github_signature, 'utf-8')
    digest = hmac.new(key=signature_bytes, msg=githhub_payload, digestmod=hashlib.sha1)
    signature = digest.hexdigest()
    return signature

将此用作参考(Github Webhooks secret with AWS API Gateway),但仍然无法匹配。请如果有人可以指出错误。也试过X-Hub-Signature-256同样的问题。

【问题讨论】:

  • 您使用的是 Lambda 集成还是代理 lambda 集成?
  • 集成类型:Lambda函数
  • 所以我总是忘记哪个做了哪个,但是 Integration: Lambda with or without the proxy lambad box check changes the body of the event being pass in. 我会用 print(event[ body]) 在您的代码中查看它的外观。很可能您*认为* github body 还没有,实际上还有几个层次。或者,如果您已经验证了这一点,那么再添加一些打印语句!找出每个值的结果,但我怀疑下一个问题是错误的 haslib 但是......那超出了我的知识库。
  • @lynkfox 检查代理 lambda 框是否有效,添加答案以防其他人卡住。感谢您的建议。

标签: python github aws-lambda aws-api-gateway


【解决方案1】:

使用 sha256 工作的 Lambda 函数:

import hmac
import hashlib
import re


GITHUB_SECRET = 'hello' # from Github UI

def calculate_signature(github_signature, payload):
    """
    Signature calculator
    """
    signature_bytes = bytes(github_signature, 'utf-8')
    digest = hmac.new(key=signature_bytes, msg=payload, digestmod=hashlib.sha256)
    signature = digest.hexdigest()
    print(f"Calculated signature: {signature}")
    return signature
    
def lambda_handler(event, context):
    print("Lambda execution starting...")
    incoming_signature = re.sub(r'^sha256=', '', event['headers']['X-Hub-Signature-256'])
    print(f"Incoming Signature: {incoming_signature}")
    calculated_signature = calculate_signature(GITHUB_SECRET, event['body'].encode('utf-8'))
    if incoming_signature != calculated_signature:
        print("Unauthorized attempt")
    else:
        print("Authorized access")
    # Lambda logic 


In API Gateway configuration, ensure `Lambda Proxy Integration` box should is checked, else the body from github is not what is needed.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2023-02-09
    • 1970-01-01
    • 2019-04-14
    • 2022-01-19
    • 2021-05-12
    相关资源
    最近更新 更多