Skip to content

Improve security by adding request signing#34

Merged
Roach merged 24 commits intomasterfrom
roach/request-signing
Aug 15, 2018
Merged

Improve security by adding request signing#34
Roach merged 24 commits intomasterfrom
roach/request-signing

Conversation

@Roach
Copy link
Contributor

@Roach Roach commented Aug 8, 2018

Summary

Replaces request token verification with request signing.

See https://api.slack.com/docs/verifying-requests-from-slack for more info.

Fixes #33

Tests are still in progress.

Requirements (place an x in each [ ])

README.rst Outdated


slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, endpoint="/slack/events")
slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, endpoint="/slack_events")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...why the move from /slack/events to slack_events?

# Initialize the Slack event server
# If no endpoint is provided, default to listening on '/slack/events'
def __init__(self, verification_token, endpoint="/slack/events", server=None):
def __init__(self, signing_secret, endpoint="/slack/events", server=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this **kwargs used now that we aren't using any optional flags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Removed.

# Each request comes with request timestamp and request signature
# emit an error if the timestamp is out of range
req_timestamp = request.headers.get('X-Slack-Request-Timestamp')
if abs(time() - int(req_timestamp)) > 60 * 10:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be 60 * 5? In the docs, we say to make sure the request is no longer than 5 mins (https://api.slack.com/docs/verifying-requests-from-slack#how_to_make_a_request_signature_in_4_easy_steps__an_overview)

req_timestamp = request.headers.get('X-Slack-Request-Timestamp')
if abs(time() - int(req_timestamp)) > 60 * 10:
self.emitter.emit('error', Exception('Request timestamp invalid'))
return make_response("", 200)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

500 or 404 status code instead of 200?

req_signature = request.headers.get('X-Slack-Signature')
if not self.verify_signature(req_timestamp, req_signature):
self.emitter.emit('error', Exception('invalid request signature'))
return make_response("", 200)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


# Echo the URL verification challenge code
# Echo the URL verification challenge code back to Slack
if "challenge" in event_data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to move this to after the request signature verification, since we aren't verifying the token in the request body anymore.

@Roach Roach changed the title Added request signing Request signing Aug 8, 2018
.. image:: https://cloud.githubusercontent.com/assets/32463/24877833/950dd53c-1de5-11e7-984f-deb26e8b9482.png

Add your app's **Verification Token** to your python environmental variables
Add your app's **Singing Secret** to your python environmental variables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling. Singing -> Signing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                                 __ 
                       _ ,___,-'",-=-. 
           __,-- _ _,-'_)_  (""`'-._\ `. 
        _,'  __ |,' ,-' __)  ,-     /. | 
      ,'_,--'   |     -'  _)/         `\ 
    ,','      ,'       ,-'_,`           : 
    ,'     ,-'       ,(,-(              : 
         ,'       ,-' ,    _            ; 
        /        ,-._/`---'            / 
       /        (____)(----. )       ,' 
      /         (      `.__,     /\ /, 
     :           ;-.___         /__\/| 
     |         ,'      `--.      -,\ | 
     :        /            \    .__/ 
      \      (__            \    |_ 
       \       ,`-, *       /   _|,\ 
        \    ,'   `-.     ,'_,-'    \ 
       (_\,-'    ,'\")--,'-'       __\ 
        \       /  // ,'|      ,--'  `-. 
         `-.    `-/ \'  |   _,'         `. 
            `-._ /      `--'/             \ 
               ,'           |              \ 
              /             |               \ 
           ,-'              |               / 
          /                 |             -' 

req_signature = request.headers.get('X-Slack-Signature')
if not self.verify_signature(req_timestamp, req_signature):
self.emitter.emit('error', Exception('invalid request signature'))
return make_response("", 200)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}
)

assert res.status_code == 404
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

403?

# emit an error if the timestamp is out of range
req_timestamp = request.headers.get('X-Slack-Request-Timestamp')
if abs(time() - int(req_timestamp)) > 60 * 5:
slack_exception = Exception('Invalid request timestamp')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specific exception type

# emit an error if the signature can't be verified
req_signature = request.headers.get('X-Slack-Signature')
if not self.verify_signature(req_timestamp, req_signature):
slack_exception = Exception('Invalid request signature')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specific exception type

@Roach Roach changed the title Request signing Improve security by adding request signing Aug 14, 2018
@Roach Roach merged commit 6a269ed into master Aug 15, 2018
@seratch seratch deleted the roach/request-signing branch July 8, 2020 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants