<commit_before>
from twisted.internet.defer import inlineCallbacks

# Override the GLSetting with test values
from globaleaks.settings import GLSetting
from globaleaks.tests import helpers

from globaleaks.rest import requests
from globaleaks.rest.errors import GLException, InvalidInputFormat
from globaleaks.handlers import base, admin, submission
from globaleaks.utils import log
from globaleaks.tests.test_tip import TTip

class MockHandler(base.BaseHandler):

    def __init__(self):
        pass

class SubmissionTest(helpers.TestGL):
    """
    This unitTest want to explore various logic attacks scenario:
    like: https://github.com/globaleaks/GlobaLeaks/issues/31
    """

    context_used = context_unused = None
    receiver_used = receiver_unused = None
    submission_desc = None

    def setUp(self):
        # helpers.TestGL.setUp(self) is done only in the first test
        pass

    def tearDown(self):
        # helpers.TestGL.tearDown(self) is done only in the last test
        pass

    aContext1 = TTip.tipContext

    aContext2 = {
        'name': u'UNUSED', 'description': u'UNUSED',
        'escalation_threshold': u'0', 'tip_max_access': u'2',
        'tip_timetolive': 200, 'file_max_download': 2, 'selectable_receiver': True,
        'receivers': [], 'fields': [], 'submission_timetolive': 100,
        'receipt_regexp': GLSetting.defaults.receipt_regexp,
        'receipt_description': u"blah", 'submission_introduction': u"bleh", 'submission_disclaimer': u"bloh",
        'file_required': False, 'tags' : [ u'one', u'two', u'y' ],
    }

    aReceiver1 = TTip.tipReceiver1
    aReceiver2 = TTip.tipReceiver2

    aSubmission = {
        # here too, are checked the default fields
        'wb_fields': { u'Short title': None, u'Full description': None },
        'context_gus': '', 'receivers': [], 'files': [], 'finalize': False
    }


class TestTipInstance(SubmissionTest):

    @inlineCallbacks
    def test_1_setup_submission_environment(self):
        helpers.TestGL.setUp(self)
        
        basehandler = MockHandler()

        basehandler.validate_jmessage( SubmissionTest.aContext1, requests.adminContextDesc)
        SubmissionTest.context_used = yield admin.create_context(SubmissionTest.aContext1)

        basehandler.validate_jmessage( SubmissionTest.aContext2, requests.adminContextDesc)
        SubmissionTest.context_unused = yield admin.create_context(SubmissionTest.aContext2)

        SubmissionTest.aReceiver1['contexts'] = [ SubmissionTest.context_used['context_gus'] ]
        basehandler.validate_jmessage( SubmissionTest.aReceiver1, requests.adminReceiverDesc )
        SubmissionTest.receiver_used = yield admin.create_receiver(SubmissionTest.aReceiver1)

        SubmissionTest.aReceiver2['contexts'] = [ SubmissionTest.context_unused['context_gus'] ]
        basehandler.validate_jmessage( SubmissionTest.aReceiver2, requests.adminReceiverDesc )
        SubmissionTest.receiver_unused = yield admin.create_receiver(SubmissionTest.aReceiver2)

        self.assertTrue(SubmissionTest.receiver_used['name'] == SubmissionTest.aReceiver1['name'])
        self.assertTrue(SubmissionTest.receiver_unused['name'] == SubmissionTest.aReceiver2['name'])

        self.assertTrue(len(SubmissionTest.receiver_used['contexts']), 1)
        self.assertTrue(len(SubmissionTest.receiver_unused['contexts']), 1)


    @inlineCallbacks
    def test_2_create_submission_missing_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = []
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Success in creation: %s" % str(r))
            self.assertTrue(False)
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertEqual(e.error_code, 22) # SubmissionFailFields
        except Exception, e:
            log.debug("Exception %s" % str(e) )
            self.assertTrue(False, msg=str(e))


    @inlineCallbacks
    def test_3_create_submission_flip_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = [ SubmissionTest.receiver_unused['receiver_gus'] ]
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Success in creation: %s" % str(r))
            self.assertTrue(False)
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertTrue(True)
        except Exception, e:
            log.debug("Exception %s" % str(e) )
            self.assertTrue(False, msg=str(e))


    @inlineCallbacks
    def test_4_create_submission_both_valid_and_invalid_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = [ SubmissionTest.receiver_unused['receiver_gus'],
                                            SubmissionTest.receiver_used['receiver_gus']  ]
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Success in creation: %s" % str(r))
            self.assertTrue(False, msg="Created!")
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertTrue(True)
        except Exception, e:
            log.debug("Exception %s" % str(e) )<commit_msg>fixed submission_abuses<commit_after>
from twisted.internet.defer import inlineCallbacks

# Override the GLSetting with test values
from globaleaks.settings import GLSetting
from globaleaks.tests import helpers

from globaleaks.rest import requests
from globaleaks.rest.errors import GLException, InvalidInputFormat
from globaleaks.handlers import base, admin, submission
from globaleaks.utils import log
from globaleaks.tests.test_tip import TTip

class MockHandler(base.BaseHandler):

    def __init__(self):
        pass

class SubmissionTest(helpers.TestGL):
    """
    This unitTest want to explore various logic attacks scenario:
    like: https://github.com/globaleaks/GlobaLeaks/issues/31
    """

    context_used = context_unused = None
    receiver_used = receiver_unused = None
    submission_desc = None

    def setUp(self):
        # helpers.TestGL.setUp(self) is done only in the first test
        pass

    def tearDown(self):
        # helpers.TestGL.tearDown(self) is done only in the last test
        pass

    aContext1 = TTip.tipContext

    aContext2 = {
        'name': u'UNUSED', 'description': u'UNUSED',
        'escalation_threshold': u'0', 'tip_max_access': u'2',
        'tip_timetolive': 200, 'file_max_download': 2, 'selectable_receiver': True,
        'receivers': [], 'fields': [], 'submission_timetolive': 100,
        'receipt_regexp': GLSetting.defaults.receipt_regexp,
        'receipt_description': u"blah", 'submission_introduction': u"bleh", 'submission_disclaimer': u"bloh",
        'file_required': False, 'tags' : [ u'one', u'two', u'y' ],
    }

    aReceiver1 = TTip.tipReceiver1
    aReceiver2 = TTip.tipReceiver2

    aSubmission = {
        # here too, are checked the default fields
        'wb_fields': { u'Short title': None, u'Full description': None },
        'context_gus': '', 'receivers': [], 'files': [], 'finalize': False
    }


class TestTipInstance(SubmissionTest):

    @inlineCallbacks
    def test_1_setup_submission_environment(self):
        helpers.TestGL.setUp(self)
        
        basehandler = MockHandler()

        basehandler.validate_jmessage( SubmissionTest.aContext1, requests.adminContextDesc)
        SubmissionTest.context_used = yield admin.create_context(SubmissionTest.aContext1)
        # Correctly, TTip.tipContext has not selectable receiver, and we want test it in the 2nd test
        SubmissionTest.context_used['selectable_receiver'] = True
        SubmissionTest.context_used = yield admin.update_context(SubmissionTest.context_used['context_gus'],
            SubmissionTest.context_used)

        basehandler.validate_jmessage( SubmissionTest.aContext2, requests.adminContextDesc)
        SubmissionTest.context_unused = yield admin.create_context(SubmissionTest.aContext2)

        SubmissionTest.aReceiver1['contexts'] = [ SubmissionTest.context_used['context_gus'] ]
        basehandler.validate_jmessage( SubmissionTest.aReceiver1, requests.adminReceiverDesc )
        SubmissionTest.receiver_used = yield admin.create_receiver(SubmissionTest.aReceiver1)

        SubmissionTest.aReceiver2['contexts'] = [ SubmissionTest.context_unused['context_gus'] ]
        basehandler.validate_jmessage( SubmissionTest.aReceiver2, requests.adminReceiverDesc )
        SubmissionTest.receiver_unused = yield admin.create_receiver(SubmissionTest.aReceiver2)

        self.assertTrue(SubmissionTest.receiver_used['name'] == SubmissionTest.aReceiver1['name'])
        self.assertTrue(SubmissionTest.receiver_unused['name'] == SubmissionTest.aReceiver2['name'])

        self.assertTrue(len(SubmissionTest.receiver_used['contexts']), 1)
        self.assertTrue(len(SubmissionTest.receiver_unused['contexts']), 1)


    @inlineCallbacks
    def test_2_create_submission_missing_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = []
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            print submission_request
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Unexpected Success in creation: %s" % str(r))
            print r
            self.assertTrue(False)
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertEqual(e.error_code, 22) # SubmissionFailFields
        except Exception, e:
            log.debug("Unexpected Exception %s" % str(e) )
            self.assertTrue(False, msg=str(e))


    @inlineCallbacks
    def test_3_create_submission_flip_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = [ SubmissionTest.receiver_unused['receiver_gus'] ]
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Unexpected Success in creation: %s" % str(r))
            self.assertTrue(False)
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertTrue(True)
        except Exception, e:
            log.debug("Unexpected Exception %s" % str(e) )
            self.assertTrue(False, msg=str(e))


    @inlineCallbacks
    def test_4_create_submission_both_valid_and_invalid_receiver(self):
        submission_request = dict(SubmissionTest.aSubmission)

        submission_request['receivers'] = [ SubmissionTest.receiver_unused['receiver_gus'],
                                            SubmissionTest.receiver_used['receiver_gus']  ]
        submission_request['context_gus'] = SubmissionTest.context_used['context_gus']
        submission_request['finalize'] = True

        try:
            r = yield submission.create_submission(submission_request, finalize=True)
            log.debug("Unexpected Success in creation: %s" % str(r))
            self.assertTrue(False, msg="Created!")
        except GLException, e:
            log.debug("GLException %s %s" % (str(e), e.reason) )
            self.assertTrue(True)
        except Exception, e:
            log.debug("Unexpected Exception %s" % str(e) )
{'fraction_non_alphanumeric': 0.10436634717784878, 'fraction_numerical': 0.0061440157286802655, 'mean_word_length': 3.400865176640231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6427750', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3544, 'n_words': 654}