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

include_request_body not working for Flask #344

Open
javamo opened this issue Jul 31, 2020 · 3 comments
Open

include_request_body not working for Flask #344

javamo opened this issue Jul 31, 2020 · 3 comments

Comments

@javamo
Copy link

javamo commented Jul 31, 2020

When you pass include_request_body=False (which is the default), Rollbar will still send the body.

@moodh
Copy link

moodh commented Aug 18, 2020

We just ran into the same issue. Looking at the implementation:

def _build_werkzeug_request_data(request):
    request_data = {
        'url': request.url,
        'GET': dict(request.args),
        'POST': dict(request.form),
        'user_ip': _extract_user_ip(request),
        'headers': dict(request.headers),
        'method': request.method,
        'files_keys': list(request.files.keys()),
    }

    try:
        if request.json:
            request_data['body'] = request.json
    except Exception:
        pass

    return request_data

This doesn't contain the needed setting check. Simply adding it:

def _build_werkzeug_request_data(request):
    request_data = {
        'url': request.url,
        'GET': dict(request.args),
        'POST': dict(request.form),
        'user_ip': _extract_user_ip(request),
        'headers': dict(request.headers),
        'method': request.method,
        'files_keys': list(request.files.keys()),
    }

    if SETTINGS['include_request_body']:
        try:
            if request.json:
                request_data['body'] = request.json
        except Exception:
            pass

    return request_data

Fixes the issue. We'd appreciate this being updated!

@javamo
Copy link
Author

javamo commented Aug 18, 2020

@moodh This is my temporal fix in case it's helpful for you:

import os
import rollbar
import rollbar.contrib.flask
from flask import request, got_request_exception


@app.before_first_request
def init_rollbar():
    """Initialize Rollbar
    """
    rollbar_environment = os.environ.get('ROLLBAR_ENVIRONMENT')
    include_request_body = os.environ.get('ROLLBAR_INCLUDE_REQUEST_BODY')
    if rollbar_environment in ('production', 'testing', 'demo'):
        rollbar.init(
            os.environ.get('ROLLBAR_ACCESS_TOKEN'),
            rollbar_environment,
            root=os.path.dirname(os.path.realpath(__file__)),
            allow_logging_basic_config=False)

        # Send exceptions from `app` to rollbar, using flask's signal system
        if include_request_body:
            got_request_exception.connect(rollbar.contrib.flask.report_exception, app)
        else:
            got_request_exception.connect(report_exc_without_body, app)


def report_exc_without_body(sender, exception, **extra):
    # TODO: Temporal fix to avoid sending the body
    # https://github.com/rollbar/pyrollbar/issues/344
    rollbar.report_exc_info(request=request, payload_data={
        'request': {'body': None}
    })

@moodh
Copy link

moodh commented Aug 18, 2020

Thanks, here's how I removed the data from our end, but I haven't fully tested it yet :)

def initialize_rollbar(app):
    def _payload_handler(payload):
        try:
            payload['data']['request']['body'] = {}
        except Exception:
            pass

        return payload

    rollbar.init(...)

    rollbar.events.add_payload_handler(_payload_handler)

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

No branches or pull requests

2 participants