code
stringlengths
23
2.05k
label_name
stringlengths
6
7
label
int64
0
37
def parse_jwt_token(request: func.HttpRequest) -> Result[UserInfo]: """Obtains the Access Token from the Authorization Header""" token_str = get_auth_token(request) if token_str is None: return Error( code=ErrorCode.INVALID_REQUEST, errors=["unable to find authorization token"], ) # This token has already been verified by the azure authentication layer token = jwt.decode(token_str, options={"verify_signature": False}) application_id = UUID(token["appid"]) if "appid" in token else None object_id = UUID(token["oid"]) if "oid" in token else None upn = token.get("upn") return UserInfo(application_id=application_id, object_id=object_id, upn=upn)
CWE-285
23
def _handle_carbon_sent(self, msg): self.xmpp.event('carbon_sent', msg)
CWE-20
0
def _wsse_username_token(cnonce, iso_now, password): return base64.b64encode( _sha("%s%s%s" % (cnonce, iso_now, password)).digest() ).strip()
CWE-400
2
def start_requests(self): yield SplashRequest(self.url, endpoint='execute', args={'lua_source': DEFAULT_SCRIPT})
CWE-200
10
def test_modify_config(self) -> None: user1 = uuid4() user2 = uuid4() # no admins set self.assertTrue(can_modify_config_impl(InstanceConfig(), UserInfo())) # with oid, but no admin self.assertTrue( can_modify_config_impl(InstanceConfig(), UserInfo(object_id=user1)) ) # is admin self.assertTrue( can_modify_config_impl( InstanceConfig(admins=[user1]), UserInfo(object_id=user1) ) ) # no user oid set self.assertFalse( can_modify_config_impl(InstanceConfig(admins=[user1]), UserInfo()) ) # not an admin self.assertFalse( can_modify_config_impl( InstanceConfig(admins=[user1]), UserInfo(object_id=user2) ) )
CWE-346
16
async def on_PUT(self, origin, content, query, room_id, event_id): content = await self.handler.on_send_leave_request(origin, content, room_id) return 200, (200, content)
CWE-400
2
def __init__(self, private_key): self.signer = Signer(private_key) self.public_key = self.signer.public_key
CWE-863
11
def _ssl_wrap_socket( sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname, key_password
CWE-400
2
def get_release_file(root, request): session = DBSession() f = ReleaseFile.by_id(session, int(request.matchdict['file_id'])) rv = {'id': f.id, 'url': f.url, 'filename': f.filename, } f.downloads += 1 f.release.downloads += 1 f.release.package.downloads += 1 session.add(f.release.package) session.add(f.release) session.add(f) return rv
CWE-20
0
def try_ldap_login(login, password): """ Connect to a LDAP directory to verify user login/passwords""" result = "Wrong login/password" s = Server(config.LDAPURI, port=config.LDAPPORT, use_ssl=False, get_info=ALL) # 1. connection with service account to find the user uid uid = useruid(s, login) if uid: # 2. Try to bind the user to the LDAP c = Connection(s, user = uid , password = password, auto_bind = True) c.open() c.bind() result = c.result["description"] # "success" if bind is ok c.unbind() return result
CWE-74
1
def test_digest_object_stale(): credentials = ("joe", "password") host = None request_uri = "/digest/stale/" headers = {} response = httplib2.Response({}) response["www-authenticate"] = ( 'Digest realm="myrealm", nonce="bd669f", ' 'algorithm=MD5, qop="auth", stale=true' ) response.status = 401 content = b"" d = httplib2.DigestAuthentication( credentials, host, request_uri, headers, response, content, None ) # Returns true to force a retry assert d.response(response, content)
CWE-400
2
def is_valid_hostname(string: str) -> bool: """Validate that a given string is a valid hostname or domain name, with an optional port number. For domain names, this only validates that the form is right (for instance, it doesn't check that the TLD is valid). If a port is specified, it has to be a valid port number. :param string: The string to validate :type string: str :return: Whether the input is a valid hostname :rtype: bool """ host_parts = string.split(":", 1) if len(host_parts) == 1: return hostname_regex.match(string) is not None else: host, port = host_parts valid_hostname = hostname_regex.match(host) is not None try: port_num = int(port) valid_port = ( port == str(port_num) # exclude things like '08090' or ' 8090' and 1 <= port_num < 65536) except ValueError: valid_port = False return valid_hostname and valid_port
CWE-20
0
def __init__(self, path: Union[str, Path]): super().__init__(path=Path(path)) self['headers'] = {} self['cookies'] = {} self['auth'] = { 'type': None, 'username': None, 'password': None }
CWE-200
10
def test_can_read_token_from_headers(self): """Tests that Sydent correct extracts an auth token from request headers""" self.sydent.run() request, _ = make_request( self.sydent.reactor, "GET", "/_matrix/identity/v2/hash_details" ) request.requestHeaders.addRawHeader( b"Authorization", b"Bearer " + self.test_token.encode("ascii") ) token = tokenFromRequest(request) self.assertEqual(token, self.test_token)
CWE-20
0
def to_plist(self, data, options=None): """ Given some Python data, produces binary plist output. """ options = options or {} if biplist is None: raise ImproperlyConfigured("Usage of the plist aspects requires biplist.") return biplist.writePlistToString(self.to_simple(data, options))
CWE-20
0
def get(self, id, project=None): if not project: project = g.project return ( Person.query.filter(Person.id == id) .filter(Project.id == project.id) .one() )
CWE-863
11
def start_requests(self): yield SplashRequest(self.url)
CWE-200
10
def _checknetloc(netloc): if not netloc or netloc.isascii(): return # looking for characters like \u2100 that expand to 'a/c' # IDNA uses NFKC equivalence, so normalize for this check import unicodedata n = netloc.rpartition('@')[2] # ignore anything to the left of '@' n = n.replace(':', '') # ignore characters already included n = n.replace('#', '') # but not the surrounding text n = n.replace('?', '') netloc2 = unicodedata.normalize('NFKC', n) if n == netloc2: return for c in '/?#@:': if c in netloc2: raise ValueError("netloc '" + netloc + "' contains invalid " + "characters under NFKC normalization")
CWE-522
19
def refresh(self): """ Security endpoint for the refresh token, so we can obtain a new token without forcing the user to login again --- post: description: >- Use the refresh token to get a new JWT access token responses: 200: description: Refresh Successful content: application/json: schema: type: object properties: access_token: description: A new refreshed access token type: string 401: $ref: '#/components/responses/401' 500: $ref: '#/components/responses/500' security: - jwt_refresh: [] """ resp = { API_SECURITY_ACCESS_TOKEN_KEY: create_access_token( identity=get_jwt_identity(), fresh=False ) } return self.response(200, **resp)
CWE-287
4
def test_captcha_validate_value(self): captcha = FlaskSessionCaptcha(self.app) _default_routes(captcha, self.app) with self.app.test_request_context('/'): captcha.generate() answer = captcha.get_answer() assert not captcha.validate(value="wrong") captcha.generate() answer = captcha.get_answer() assert captcha.validate(value=answer)
CWE-754
31
def CreateID(self): """Create a packet ID. All RADIUS requests have a ID which is used to identify a request. This is used to detect retries and replay attacks. This function returns a suitable random number that can be used as ID. :return: ID number :rtype: integer """ return random.randrange(0, 256)
CWE-330
12
def test_tofile_sep(self): x = np.array([1.51, 2, 3.51, 4], dtype=float) f = open(self.filename, 'w') x.tofile(f, sep=',') f.close() f = open(self.filename, 'r') s = f.read() f.close() assert_equal(s, '1.51,2.0,3.51,4.0') os.unlink(self.filename)
CWE-20
0
def runTest(self): for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB, self.module.MODE_OPENPGP): encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) ciphertext = encryption_cipher.encrypt(self.plaintext) if mode != self.module.MODE_OPENPGP: decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) else: eiv = ciphertext[:self.module.block_size+2] ciphertext = ciphertext[self.module.block_size+2:] decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv) decrypted_plaintext = decryption_cipher.decrypt(ciphertext) self.assertEqual(self.plaintext, decrypted_plaintext)
CWE-119
26
def _slices_from_text(self, text): last_break = 0 for match in self._lang_vars.period_context_re().finditer(text): context = match.group() + match.group("after_tok") if self.text_contains_sentbreak(context): yield slice(last_break, match.end()) if match.group("next_tok"): # next sentence starts after whitespace last_break = match.start("next_tok") else: # next sentence starts at following punctuation last_break = match.end() # The last sentence should not contain trailing whitespace. yield slice(last_break, len(text.rstrip()))
CWE-400
2
def delete_scan(request, id): obj = get_object_or_404(ScanHistory, id=id) if request.method == "POST": delete_dir = obj.domain.name + '_' + \ str(datetime.datetime.strftime(obj.start_scan_date, '%Y_%m_%d_%H_%M_%S')) delete_path = settings.TOOL_LOCATION + 'scan_results/' + delete_dir os.system('rm -rf ' + delete_path) obj.delete() messageData = {'status': 'true'} messages.add_message( request, messages.INFO, 'Scan history successfully deleted!') else: messageData = {'status': 'false'} messages.add_message( request, messages.INFO, 'Oops! something went wrong!') return JsonResponse(messageData)
CWE-330
12
def __init__( self, credentials, host, request_uri, headers, response, content, http
CWE-400
2
def CreateID(self): """Create a packet ID. All RADIUS requests have a ID which is used to identify a request. This is used to detect retries and replay attacks. This function returns a suitable random number that can be used as ID. :return: ID number :rtype: integer """ return random.randrange(0, 256)
CWE-20
0
def debug_decisions(self, text): """ Classifies candidate periods as sentence breaks, yielding a dict for each that may be used to understand why the decision was made. See format_debug_decision() to help make this output readable. """ for match in self._lang_vars.period_context_re().finditer(text): decision_text = match.group() + match.group("after_tok") tokens = self._tokenize_words(decision_text) tokens = list(self._annotate_first_pass(tokens)) while tokens and not tokens[0].tok.endswith(self._lang_vars.sent_end_chars): tokens.pop(0) yield { "period_index": match.end() - 1, "text": decision_text, "type1": tokens[0].type, "type2": tokens[1].type, "type1_in_abbrs": bool(tokens[0].abbr), "type1_is_initial": bool(tokens[0].is_initial), "type2_is_sent_starter": tokens[1].type_no_sentperiod in self._params.sent_starters, "type2_ortho_heuristic": self._ortho_heuristic(tokens[1]), "type2_ortho_contexts": set( self._params._debug_ortho_context(tokens[1].type_no_sentperiod) ), "collocation": ( tokens[0].type_no_sentperiod, tokens[1].type_no_sentperiod, ) in self._params.collocations, "reason": self._second_pass_annotation(tokens[0], tokens[1]) or REASON_DEFAULT_DECISION, "break_decision": tokens[0].sentbreak, }
CWE-400
2
def _handle_carbon_received(self, msg): self.xmpp.event('carbon_received', msg)
CWE-20
0
def test_basic_for_domain(): # Test Basic Authentication http = httplib2.Http() password = tests.gen_password() handler = tests.http_reflect_with_auth( allow_scheme="basic", allow_credentials=(("joe", password),) ) with tests.server_request(handler, request_count=4) as uri: response, content = http.request(uri, "GET") assert response.status == 401 http.add_credentials("joe", password, "example.org") response, content = http.request(uri, "GET") assert response.status == 401 domain = urllib.parse.urlparse(uri)[1] http.add_credentials("joe", password, domain) response, content = http.request(uri, "GET") assert response.status == 200
CWE-400
2
def render_POST(self, request): """ Register with the Identity Server """ send_cors(request) args = get_args(request, ('matrix_server_name', 'access_token')) result = yield self.client.get_json( "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s" % ( args['matrix_server_name'], urllib.parse.quote(args['access_token']), ), 1024 * 5, ) if 'sub' not in result: raise Exception("Invalid response from homeserver") user_id = result['sub'] tok = yield issueToken(self.sydent, user_id) # XXX: `token` is correct for the spec, but we released with `access_token` # for a substantial amount of time. Serve both to make spec-compliant clients # happy. defer.returnValue({ "access_token": tok, "token": tok, })
CWE-20
0
def prepare_key(self, key): key = force_bytes(key) invalid_strings = [ b"-----BEGIN PUBLIC KEY-----", b"-----BEGIN CERTIFICATE-----", b"-----BEGIN RSA PUBLIC KEY-----", b"ssh-rsa", ] if any(string_value in key for string_value in invalid_strings): raise InvalidKeyError( "The specified key is an asymmetric key or x509 certificate and" " should not be used as an HMAC secret." ) return key
CWE-327
3
def dataReceived(self, data: bytes) -> None: self.stream.write(data) self.length += len(data) if self.max_size is not None and self.length >= self.max_size: self.deferred.errback( SynapseError( 502, "Requested file is too large > %r bytes" % (self.max_size,), Codes.TOO_LARGE, ) ) self.deferred = defer.Deferred() self.transport.loseConnection()
CWE-400
2
def connectionMade(self): method = getattr(self.factory, 'method', b'GET') self.sendCommand(method, self.factory.path) if self.factory.scheme == b'http' and self.factory.port != 80: host = self.factory.host + b':' + intToBytes(self.factory.port) elif self.factory.scheme == b'https' and self.factory.port != 443: host = self.factory.host + b':' + intToBytes(self.factory.port) else: host = self.factory.host self.sendHeader(b'Host', self.factory.headers.get(b"host", host)) self.sendHeader(b'User-Agent', self.factory.agent) data = getattr(self.factory, 'postdata', None) if data is not None: self.sendHeader(b"Content-Length", intToBytes(len(data))) cookieData = [] for (key, value) in self.factory.headers.items(): if key.lower() not in self._specialHeaders: # we calculated it on our own self.sendHeader(key, value) if key.lower() == b'cookie': cookieData.append(value) for cookie, cookval in self.factory.cookies.items(): cookieData.append(cookie + b'=' + cookval) if cookieData: self.sendHeader(b'Cookie', b'; '.join(cookieData)) self.endHeaders() self.headers = {} if data is not None: self.transport.write(data)
CWE-74
1
def load_module(name): if os.path.exists(name) and os.path.splitext(name)[1] == '.py': sys.path.insert(0, os.path.dirname(os.path.abspath(name))) try: m = os.path.splitext(os.path.basename(name))[0] module = importlib.import_module(m) finally: sys.path.pop(0) return module return importlib.import_module('dbusmock.templates.' + name)
CWE-20
0
def generate_subparsers(root, parent_parser, definitions): action_dest = '_'.join(parent_parser.prog.split()[1:] + ['action']) actions = parent_parser.add_subparsers( dest=action_dest ) for command, properties in definitions.items(): is_subparser = isinstance(properties, dict) descr = properties.pop('help', None) if is_subparser else properties.pop(0) command_parser = actions.add_parser(command, description=descr) command_parser.root = root if is_subparser: generate_subparsers(root, command_parser, properties) continue for argument in properties: command_parser.add_argument(**argument)
CWE-200
10
def event_from_pdu_json(pdu_json, outlier=False): """Construct a FrozenEvent from an event json received over federation Args: pdu_json (object): pdu as received over federation outlier (bool): True to mark this event as an outlier Returns: FrozenEvent Raises: SynapseError: if the pdu is missing required fields """ # we could probably enforce a bunch of other fields here (room_id, sender, # origin, etc etc) assert_params_in_request(pdu_json, ('event_id', 'type')) event = FrozenEvent( pdu_json ) event.internal_metadata.outlier = outlier return event
CWE-20
0
def parse_html_description(tree: "etree.Element") -> Optional[str]: """ Calculate a text description based on an HTML document. Grabs any text nodes which are inside the <body/> tag, unless they are within an HTML5 semantic markup tag (<header/>, <nav/>, <aside/>, <footer/>), or if they are within a <script/>, <svg/> or <style/> tag, or if they are within a tag whose content is usually only shown to old browsers (<iframe/>, <video/>, <canvas/>, <picture/>). This is a very very very coarse approximation to a plain text render of the page. Args: tree: The parsed HTML document. Returns: The plain text description, or None if one cannot be generated. """ # We don't just use XPATH here as that is slow on some machines. from lxml import etree TAGS_TO_REMOVE = ( "header", "nav", "aside", "footer", "script", "noscript", "style", "svg", "iframe", "video", "canvas", "img", "picture", etree.Comment, ) # Split all the text nodes into paragraphs (by splitting on new # lines) text_nodes = ( re.sub(r"\s+", "\n", el).strip() for el in _iterate_over_text(tree.find("body"), *TAGS_TO_REMOVE) ) return summarize_paragraphs(text_nodes)
CWE-674
28
def _handle_carbon_sent(self, msg): self.xmpp.event('carbon_sent', msg)
CWE-346
16
def verify_cert_against_key(self, filename, key_filename): """ check that a certificate validates against its private key. """ cert = self.data + filename key = self.data + key_filename cmd = "openssl x509 -noout -modulus -in %s | openssl md5" % cert cert_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read() cmd = "openssl rsa -noout -modulus -in %s | openssl md5" % key key_md5 = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT).stdout.read() if cert_md5 == key_md5: return True return False
CWE-20
0
def intermediate_dir(): """ Location in temp dir for storing .cpp and .o files during builds. """ python_name = "python%d%d_intermediate" % tuple(sys.version_info[:2]) path = os.path.join(tempfile.gettempdir(),"%s"%whoami(),python_name) if not os.path.exists(path): os.makedirs(path, mode=0o700) return path
CWE-269
6
async def account_register_post( request: Request, U: str = Form(default=str()), # Username E: str = Form(default=str()), # Email H: str = Form(default=False), # Hide Email BE: str = Form(default=None), # Backup Email R: str = Form(default=""), # Real Name HP: str = Form(default=None), # Homepage I: str = Form(default=None), # IRC Nick # noqa: E741 K: str = Form(default=None), # PGP Key L: str = Form(default=aurweb.config.get("options", "default_lang")), TZ: str = Form(default=aurweb.config.get("options", "default_timezone")), PK: str = Form(default=None), # SSH PubKey CN: bool = Form(default=False), UN: bool = Form(default=False), ON: bool = Form(default=False), captcha: str = Form(default=None), captcha_salt: str = Form(...),
CWE-200
10
def __init__( self, cache, safe=safename
CWE-400
2
def remove_dir(self,d): import distutils.dir_util distutils.dir_util.remove_tree(d)
CWE-269
6
def auth_user_registration_role(self): return self.appbuilder.get_app.config["AUTH_USER_REGISTRATION_ROLE"]
CWE-287
4
def auth_user_registration(self): return self.appbuilder.get_app.config["AUTH_USER_REGISTRATION"]
CWE-287
4
def save(self): self['__meta__'] = { 'httpie': __version__ } if self.helpurl: self['__meta__']['help'] = self.helpurl if self.about: self['__meta__']['about'] = self.about self.ensure_directory() json_string = json.dumps( obj=self, indent=4, sort_keys=True, ensure_ascii=True, ) self.path.write_text(json_string + '\n', encoding=UTF8)
CWE-200
10
def _writeHeaders(self, transport, TEorCL): hosts = self.headers.getRawHeaders(b'host', ()) if len(hosts) != 1: raise BadHeaders(u"Exactly one Host header required") # In the future, having the protocol version be a parameter to this # method would probably be good. It would be nice if this method # weren't limited to issuing HTTP/1.1 requests. requestLines = [] requestLines.append(b' '.join([self.method, self.uri, b'HTTP/1.1\r\n'])) if not self.persistent: requestLines.append(b'Connection: close\r\n') if TEorCL is not None: requestLines.append(TEorCL) for name, values in self.headers.getAllRawHeaders(): requestLines.extend([name + b': ' + v + b'\r\n' for v in values]) requestLines.append(b'\r\n') transport.writeSequence(requestLines)
CWE-74
1
def load(self): config_type = type(self).__name__.lower() try: with self.path.open(encoding=UTF8) as f: try: data = json.load(f) except ValueError as e: raise ConfigFileError( f'invalid {config_type} file: {e} [{self.path}]' ) self.update(data) except FileNotFoundError: pass except OSError as e: raise ConfigFileError(f'cannot read {config_type} file: {e}')
CWE-200
10
def parse(self, response): yield {'response': response}
CWE-200
10
def is_2fa_enabled(self): return self.totp_status == TOTPStatus.ENABLED
CWE-287
4
async def _has_watch_regex_match(self, text: str) -> Tuple[Union[bool, re.Match], Optional[str]]: """ Return True if `text` matches any regex from `word_watchlist` or `token_watchlist` configs. `word_watchlist`'s patterns are placed between word boundaries while `token_watchlist` is matched as-is. Spoilers are expanded, if any, and URLs are ignored. Second return value is a reason written to database about blacklist entry (can be None). """ if SPOILER_RE.search(text): text = self._expand_spoilers(text) text = self.clean_input(text) # Make sure it's not a URL if URL_RE.search(text): return False, None watchlist_patterns = self._get_filterlist_items('filter_token', allowed=False) for pattern in watchlist_patterns: match = re.search(pattern, text, flags=re.IGNORECASE) if match: return match, self._get_filterlist_value('filter_token', pattern, allowed=False)['comment'] return False, None
CWE-20
0
def CreateAuthenticator(): """Create a packet autenticator. All RADIUS packets contain a sixteen byte authenticator which is used to authenticate replies from the RADIUS server and in the password hiding algorithm. This function returns a suitable random string that can be used as an authenticator. :return: valid packet authenticator :rtype: binary string """ data = [] for i in range(16): data.append(random.randrange(0, 256)) if six.PY3: return bytes(data) else: return ''.join(chr(b) for b in data)
CWE-20
0
def test_list(self): self.user.session_set.create(session_key='ABC123', ip='127.0.0.1', expire_date=datetime.now() + timedelta(days=1), user_agent='Firefox') response = self.client.get(reverse('user_sessions:session_list')) self.assertContains(response, 'Active Sessions') self.assertContains(response, 'End Session', 3) self.assertContains(response, 'Firefox')
CWE-326
9
def _process(tlist): def get_next_comment(): # TODO(andi) Comment types should be unified, see related issue38 return tlist.token_next_by(i=sql.Comment, t=T.Comment) def _get_insert_token(token): """Returns either a whitespace or the line breaks from token.""" # See issue484 why line breaks should be preserved. m = re.search(r'((\r\n|\r|\n)+) *$', token.value) if m is not None: return sql.Token(T.Whitespace.Newline, m.groups()[0]) else: return sql.Token(T.Whitespace, ' ') tidx, token = get_next_comment() while token: pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) nidx, next_ = tlist.token_next(tidx, skip_ws=False) # Replace by whitespace if prev and next exist and if they're not # whitespaces. This doesn't apply if prev or next is a parenthesis. if (prev_ is None or next_ is None or prev_.is_whitespace or prev_.match(T.Punctuation, '(') or next_.is_whitespace or next_.match(T.Punctuation, ')')): # Insert a whitespace to ensure the following SQL produces # a valid SQL (see #425). if prev_ is not None and not prev_.match(T.Punctuation, '('): tlist.tokens.insert(tidx, _get_insert_token(token)) tlist.tokens.remove(token) else: tlist.tokens[tidx] = _get_insert_token(token) tidx, token = get_next_comment()
CWE-400
2
def set_admins(self) -> None: name = self.results["deploy"]["func-name"]["value"] key = self.results["deploy"]["func-key"]["value"] table_service = TableService(account_name=name, account_key=key) if self.admins: update_admins(table_service, self.application_name, self.admins)
CWE-285
23
def fetch(cls) -> "InstanceConfig": entry = cls.get(get_instance_name()) if entry is None: entry = cls() entry.save() return entry
CWE-285
23
def auth_ldap_server(self): return self.appbuilder.get_app.config["AUTH_LDAP_SERVER"]
CWE-287
4
async def on_exchange_third_party_invite_request( self, room_id: str, event_dict: Dict
CWE-400
2
def __init__(self, conn=None, host=None, result=None, comm_ok=True, diff=dict()): # which host is this ReturnData about? if conn is not None: self.host = conn.host delegate = getattr(conn, 'delegate', None) if delegate is not None: self.host = delegate else: self.host = host self.result = result self.comm_ok = comm_ok # if these values are set and used with --diff we can show # changes made to particular files self.diff = diff if type(self.result) in [ str, unicode ]: self.result = utils.parse_json(self.result) if self.host is None: raise Exception("host not set") if type(self.result) != dict: raise Exception("dictionary result expected")
CWE-20
0
def auth_type(self): return self.appbuilder.get_app.config["AUTH_TYPE"]
CWE-287
4
def _get_element_ptr_tuplelike(parent, key): typ = parent.typ assert isinstance(typ, TupleLike) if isinstance(typ, StructType): assert isinstance(key, str) subtype = typ.members[key] attrs = list(typ.tuple_keys()) index = attrs.index(key) annotation = key else: assert isinstance(key, int) subtype = typ.members[key] attrs = list(range(len(typ.members))) index = key annotation = None # generated by empty() + make_setter if parent.value == "~empty": return IRnode.from_list("~empty", typ=subtype) if parent.value == "multi": assert parent.encoding != Encoding.ABI, "no abi-encoded literals" return parent.args[index] ofst = 0 # offset from parent start if parent.encoding in (Encoding.ABI, Encoding.JSON_ABI): if parent.location == STORAGE: raise CompilerPanic("storage variables should not be abi encoded") # pragma: notest member_t = typ.members[attrs[index]] for i in range(index): member_abi_t = typ.members[attrs[i]].abi_type ofst += member_abi_t.embedded_static_size() return _getelemptr_abi_helper(parent, member_t, ofst) if parent.location.word_addressable: for i in range(index): ofst += typ.members[attrs[i]].storage_size_in_words elif parent.location.byte_addressable: for i in range(index): ofst += typ.members[attrs[i]].memory_bytes_required else: raise CompilerPanic(f"bad location {parent.location}") # pragma: notest return IRnode.from_list( add_ofst(parent, ofst), typ=subtype, location=parent.location, encoding=parent.encoding, annotation=annotation, )
CWE-119
26
def http_parse_auth(s): """https://tools.ietf.org/html/rfc7235#section-2.1 """ scheme, rest = s.split(" ", 1) result = {} while True: m = httplib2.WWW_AUTH_RELAXED.search(rest) if not m: break if len(m.groups()) == 3: key, value, rest = m.groups() result[key.lower()] = httplib2.UNQUOTE_PAIRS.sub(r"\1", value) return result
CWE-400
2
def _write_headers(self): # Self refers to the Generator object. for h, v in msg.items(): print("%s:" % h, end=" ", file=self._fp) if isinstance(v, header.Header): print(v.encode(maxlinelen=self._maxheaderlen), file=self._fp) else: # email.Header got lots of smarts, so use it. headers = header.Header( v, maxlinelen=self._maxheaderlen, charset="utf-8", header_name=h ) print(headers.encode(), file=self._fp) # A blank line always separates headers from body. print(file=self._fp)
CWE-400
2
def request( self, uri, method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None,
CWE-400
2
def __init__(self): super(ManyCookies, self).__init__() self.putChild(b'', HelloWorld()) self.putChild(b'login', self.SetMyCookie())
CWE-200
10
def _parse_www_authenticate(headers, headername="www-authenticate"): """Returns a dictionary of dictionaries, one dict per auth_scheme.""" retval = {} if headername in headers: try: authenticate = headers[headername].strip() www_auth = ( USE_WWW_AUTH_STRICT_PARSING and WWW_AUTH_STRICT or WWW_AUTH_RELAXED ) while authenticate: # Break off the scheme at the beginning of the line if headername == "authentication-info": (auth_scheme, the_rest) = ("digest", authenticate) else: (auth_scheme, the_rest) = authenticate.split(" ", 1) # Now loop over all the key value pairs that come after the scheme, # being careful not to roll into the next scheme match = www_auth.search(the_rest) auth_params = {} while match: if match and len(match.groups()) == 3: (key, value, the_rest) = match.groups() auth_params[key.lower()] = UNQUOTE_PAIRS.sub( r"\1", value ) # '\\'.join([x.replace('\\', '') for x in value.split('\\\\')]) match = www_auth.search(the_rest) retval[auth_scheme.lower()] = auth_params authenticate = the_rest.strip() except ValueError: raise MalformedHeader("WWW-Authenticate") return retval
CWE-400
2
def run_tests(self): # pytest may be not installed yet import pytest args = ['--forked', '--fulltrace', '--no-cov', 'tests/'] if self.test_suite: args += ['-k', self.test_suite] sys.stderr.write('setup.py:test run pytest {}\n'.format(' '.join(args))) errno = pytest.main(args) sys.exit(errno)
CWE-400
2
def from_crawler(cls, crawler): splash_base_url = crawler.settings.get('SPLASH_URL', cls.default_splash_url) log_400 = crawler.settings.getbool('SPLASH_LOG_400', True) slot_policy = crawler.settings.get('SPLASH_SLOT_POLICY', cls.default_policy) if slot_policy not in SlotPolicy._known: raise NotConfigured("Incorrect slot policy: %r" % slot_policy) return cls(crawler, splash_base_url, slot_policy, log_400)
CWE-200
10
async def on_send_join_request( self, origin: str, content: JsonDict, room_id: str
CWE-400
2
def test_change_response_class_to_text(): mw = _get_mw() req = SplashRequest('http://example.com/', magic_response=True) req = mw.process_request(req, None) # Such response can come when downloading a file, # or returning splash:html(): the headers say it's binary, # but it can be decoded so it becomes a TextResponse. resp = TextResponse('http://mysplash.example.com/execute', headers={b'Content-Type': b'application/pdf'}, body=b'ascii binary data', encoding='utf-8') resp2 = mw.process_response(req, resp, None) assert isinstance(resp2, TextResponse) assert resp2.url == 'http://example.com/' assert resp2.headers == {b'Content-Type': [b'application/pdf']} assert resp2.body == b'ascii binary data'
CWE-200
10
def _wsse_username_token(cnonce, iso_now, password): return base64.b64encode( _sha(("%s%s%s" % (cnonce, iso_now, password)).encode("utf-8")).digest() ).strip().decode("utf-8")
CWE-400
2
def test_auth_type_stored_in_session_file(self, httpbin): self.config_dir = mk_config_dir() self.session_path = self.config_dir / 'test-session.json' class Plugin(AuthPlugin): auth_type = 'test-saved' auth_require = True def get_auth(self, username=None, password=None): return basic_auth() plugin_manager.register(Plugin) http('--session', str(self.session_path), httpbin + '/basic-auth/user/password', '--auth-type', Plugin.auth_type, '--auth', 'user:password', ) updated_session = json.loads(self.session_path.read_text(encoding=UTF8)) assert updated_session['auth']['type'] == 'test-saved' assert updated_session['auth']['raw_auth'] == "user:password" plugin_manager.unregister(Plugin)
CWE-200
10
def fetch(cls) -> "InstanceConfig": entry = cls.get(get_instance_name()) if entry is None: entry = cls() entry.save() return entry
CWE-346
16
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] if user is None: return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': 'login_failed'}, m='login_failed') if not user.has_2fa(): return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': '2fa_not_enabled'}, m='2fa_not_enabled') token = serializer.data['tfa'] if len(token) == 6: if user.totp_device is not None and user.totp_device.validate_token(token): return self.issue_token(user) elif len(token) == 8: for code in user.backup_codes: if token == code.code: code.delete() return self.issue_token(user) return self.issue_token(user)
CWE-287
4
def __init__(self, method, uri, headers, bodyProducer, persistent=False): """ @param method: The HTTP method for this request, ex: b'GET', b'HEAD', b'POST', etc. @type method: L{bytes} @param uri: The relative URI of the resource to request. For example, C{b'/foo/bar?baz=quux'}. @type uri: L{bytes} @param headers: Headers to be sent to the server. It is important to note that this object does not create any implicit headers. So it is up to the HTTP Client to add required headers such as 'Host'. @type headers: L{twisted.web.http_headers.Headers} @param bodyProducer: L{None} or an L{IBodyProducer} provider which produces the content body to send to the remote HTTP server. @param persistent: Set to C{True} when you use HTTP persistent connection, defaults to C{False}. @type persistent: L{bool} """ self.method = method self.uri = uri self.headers = headers self.bodyProducer = bodyProducer self.persistent = persistent self._parsedURI = None
CWE-74
1
def test_digest_next_nonce_nc(): # Test that if the server sets nextnonce that we reset # the nonce count back to 1 http = httplib2.Http() password = tests.gen_password() grenew_nonce = [None] handler = tests.http_reflect_with_auth( allow_scheme="digest", allow_credentials=(("joe", password),), out_renew_nonce=grenew_nonce, ) with tests.server_request(handler, request_count=5) as uri: http.add_credentials("joe", password) response1, _ = http.request(uri, "GET") info = httplib2._parse_www_authenticate(response1, "authentication-info") assert response1.status == 200 assert info.get("digest", {}).get("nc") == "00000001", info assert not info.get("digest", {}).get("nextnonce"), info response2, _ = http.request(uri, "GET") info2 = httplib2._parse_www_authenticate(response2, "authentication-info") assert info2.get("digest", {}).get("nc") == "00000002", info2 grenew_nonce[0]() response3, content = http.request(uri, "GET") info3 = httplib2._parse_www_authenticate(response3, "authentication-info") assert response3.status == 200 assert info3.get("digest", {}).get("nc") == "00000001", info3
CWE-400
2
def test_can_read_token_from_query_parameters(self): """Tests that Sydent correct extracts an auth token from query parameters""" self.sydent.run() request, _ = make_request( self.sydent.reactor, "GET", "/_matrix/identity/v2/hash_details?access_token=" + self.test_token ) token = tokenFromRequest(request) self.assertEqual(token, self.test_token)
CWE-20
0
def _normalize_headers(headers): return dict( [ ( _convert_byte_str(key).lower(), NORMALIZE_SPACE.sub(_convert_byte_str(value), " ").strip(), ) for (key, value) in headers.items() ] )
CWE-400
2
def request( self, uri, method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None,
CWE-400
2
def build_cert(self, key_filename, entry, metadata): """ creates a new certificate according to the specification """ req_config = self.build_req_config(entry, metadata) req = self.build_request(key_filename, req_config, entry) ca = self.cert_specs[entry.get('name')]['ca'] ca_config = self.CAs[ca]['config'] days = self.cert_specs[entry.get('name')]['days'] passphrase = self.CAs[ca].get('passphrase') if passphrase: cmd = "openssl ca -config %s -in %s -days %s -batch -passin pass:%s" % (ca_config, req, days, passphrase) else: cmd = "openssl ca -config %s -in %s -days %s -batch" % (ca_config, req, days) cert = Popen(cmd, shell=True, stdout=PIPE).stdout.read() try: os.unlink(req_config) os.unlink(req) except OSError: self.logger.error("Failed to unlink temporary files") return cert
CWE-20
0
def test_with_admins(self) -> None: no_admins = InstanceConfig(admins=None) with_admins = InstanceConfig(admins=[UUID(int=0)]) with_admins_2 = InstanceConfig(admins=[UUID(int=1)]) no_admins.update(with_admins) self.assertEqual(no_admins.admins, None) with_admins.update(with_admins_2) self.assertEqual(with_admins.admins, with_admins_2.admins)
CWE-285
23
def test_get_mpi_implementation(self): def test(output, expected, exit_code=0): ret = (output, exit_code) if output is not None else None env = {'VAR': 'val'} with mock.patch("horovod.runner.mpi_run.tiny_shell_exec.execute", return_value=ret) as m: implementation = _get_mpi_implementation(env) self.assertEqual(expected, implementation) m.assert_called_once_with('mpirun --version', env) test(("mpirun (Open MPI) 2.1.1\n" "Report bugs to http://www.open-mpi.org/community/help/\n"), _OMPI_IMPL) test("OpenRTE", _OMPI_IMPL) test("IBM Spectrum MPI", _SMPI_IMPL) test(("HYDRA build details:\n" " Version: 3.3a2\n" " Configure options: 'MPICHLIB_CFLAGS=-g -O2'\n"), _MPICH_IMPL) test("Intel(R) MPI", _IMPI_IMPL) test("Unknown MPI v1.00", _UNKNOWN_IMPL) test("output", exit_code=1, expected=_MISSING_IMPL) test(None, _MISSING_IMPL)
CWE-668
7
async def on_GET(self, origin, content, query, context, event_id): return await self.handler.on_event_auth(origin, context, event_id)
CWE-400
2
def generic_visit(self, node): if type(node) not in SAFE_NODES: #raise Exception("invalid expression (%s) type=%s" % (expr, type(node))) raise Exception("invalid expression (%s)" % expr) super(CleansingNodeVisitor, self).generic_visit(node)
CWE-74
1
def __init__( self, proxy_type, proxy_host, proxy_port, proxy_rdns=True, proxy_user=None, proxy_pass=None, proxy_headers=None,
CWE-400
2
def cookies(self) -> RequestsCookieJar: jar = RequestsCookieJar() for name, cookie_dict in self['cookies'].items(): jar.set_cookie(create_cookie( name, cookie_dict.pop('value'), **cookie_dict)) jar.clear_expired_cookies() return jar
CWE-200
10
def fixed_fetch( url, payload=None, method="GET", headers={}, allow_truncated=False, follow_redirects=True, deadline=None,
CWE-400
2
def _get_mw(): crawler = _get_crawler({}) return SplashMiddleware.from_crawler(crawler)
CWE-200
10
def testFlushFunction(self): logdir = self.get_temp_dir() with context.eager_mode(): writer = summary_ops.create_file_writer_v2( logdir, max_queue=999999, flush_millis=999999) with writer.as_default(): get_total = lambda: len(events_from_logdir(logdir)) # Note: First tf.compat.v1.Event is always file_version. self.assertEqual(1, get_total()) summary_ops.write('tag', 1, step=0) summary_ops.write('tag', 1, step=0) self.assertEqual(1, get_total()) summary_ops.flush() self.assertEqual(3, get_total()) # Test "writer" parameter summary_ops.write('tag', 1, step=0) self.assertEqual(3, get_total()) summary_ops.flush(writer=writer) self.assertEqual(4, get_total()) summary_ops.write('tag', 1, step=0) self.assertEqual(4, get_total()) summary_ops.flush(writer=writer._resource) # pylint:disable=protected-access self.assertEqual(5, get_total())
CWE-20
0
def test_roundtrip_file(self): f = open(self.filename, 'wb') self.x.tofile(f) f.close() # NB. doesn't work with flush+seek, due to use of C stdio f = open(self.filename, 'rb') y = np.fromfile(f, dtype=self.dtype) f.close() assert_array_equal(y, self.x.flat) os.unlink(self.filename)
CWE-20
0
def request(self, method, request_uri, headers, content): """Modify the request headers""" keys = _get_end2end_headers(headers) keylist = "".join(["%s " % k for k in keys]) headers_val = "".join([headers[k] for k in keys]) created = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) cnonce = _cnonce() request_digest = "%s:%s:%s:%s:%s" % ( method, request_uri, cnonce, self.challenge["snonce"], headers_val, ) request_digest = ( hmac.new(self.key, request_digest, self.hashmod).hexdigest().lower() ) headers["authorization"] = ( 'HMACDigest username="%s", realm="%s", snonce="%s",' ' cnonce="%s", uri="%s", created="%s", ' 'response="%s", headers="%s"' ) % ( self.credentials[0], self.challenge["realm"], self.challenge["snonce"], cnonce, request_uri, created, request_digest, keylist, )
CWE-400
2
def settings(request): """ Default scrapy-splash settings """ s = dict( # collect scraped items to .collected_items attribute ITEM_PIPELINES={ 'tests.utils.CollectorPipeline': 100, }, # scrapy-splash settings SPLASH_URL=os.environ.get('SPLASH_URL'), DOWNLOADER_MIDDLEWARES={ # Engine side 'scrapy_splash.SplashCookiesMiddleware': 723, 'scrapy_splash.SplashMiddleware': 725, 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810, # Downloader side }, SPIDER_MIDDLEWARES={ 'scrapy_splash.SplashDeduplicateArgsMiddleware': 100, }, DUPEFILTER_CLASS='scrapy_splash.SplashAwareDupeFilter', HTTPCACHE_STORAGE='scrapy_splash.SplashAwareFSCacheStorage', ) return Settings(s)
CWE-200
10
def safe_text(raw_text: str) -> jinja2.Markup: """ Process text: treat it as HTML but escape any tags (ie. just escape the HTML) then linkify it. """ return jinja2.Markup( bleach.linkify(bleach.clean(raw_text, tags=[], attributes={}, strip=False)) )
CWE-74
1
def test_digest_object_with_opaque(): credentials = ("joe", "password") host = None request_uri = "/digest/opaque/" headers = {} response = { "www-authenticate": 'Digest realm="myrealm", nonce="30352fd", algorithm=MD5, ' 'qop="auth", opaque="atestopaque"' } content = "" d = httplib2.DigestAuthentication( credentials, host, request_uri, headers, response, content, None ) d.request("GET", request_uri, headers, content, cnonce="5ec2") our_request = "authorization: " + headers["authorization"] working_request = ( 'authorization: Digest username="joe", realm="myrealm", ' 'nonce="30352fd", uri="/digest/opaque/", algorithm=MD5' + ', response="a1fab43041f8f3789a447f48018bee48", qop=auth, nc=00000001, ' 'cnonce="5ec2", opaque="atestopaque"' ) assert our_request == working_request
CWE-400
2
def get_revision(self): """Read svn revision information for the Bcfg2 repository.""" try: data = Popen(("env LC_ALL=C svn info %s" % (self.datastore)), shell=True, stdout=PIPE).communicate()[0].split('\n') return [line.split(': ')[1] for line in data \ if line[:9] == 'Revision:'][-1] except IndexError: logger.error("Failed to read svn info; disabling svn support") logger.error('''Ran command "svn info %s"''' % (self.datastore)) logger.error("Got output: %s" % data) raise Bcfg2.Server.Plugin.PluginInitError
CWE-20
0
def _unpack_returndata(buf, contract_sig, skip_contract_check, context): return_t = contract_sig.return_type if return_t is None: return ["pass"], 0, 0 return_t = calculate_type_for_external_return(return_t) # if the abi signature has a different type than # the vyper type, we need to wrap and unwrap the type # so that the ABI decoding works correctly should_unwrap_abi_tuple = return_t != contract_sig.return_type abi_return_t = return_t.abi_type min_return_size = abi_return_t.min_size() max_return_size = abi_return_t.size_bound() assert 0 < min_return_size <= max_return_size ret_ofst = buf ret_len = max_return_size # revert when returndatasize is not in bounds ret = [] # runtime: min_return_size <= returndatasize # TODO move the -1 optimization to IR optimizer if not skip_contract_check: ret += [["assert", ["gt", "returndatasize", min_return_size - 1]]] # add as the last IRnode a pointer to the return data structure # the return type has been wrapped by the calling contract; # unwrap it so downstream code isn't confused. # basically this expands to buf+32 if the return type has been wrapped # in a tuple AND its ABI type is dynamic. # in most cases, this simply will evaluate to ret. # in the special case where the return type has been wrapped # in a tuple AND its ABI type is dynamic, it expands to buf+32. buf = IRnode(buf, typ=return_t, encoding=_returndata_encoding(contract_sig), location=MEMORY) if should_unwrap_abi_tuple: buf = get_element_ptr(buf, 0, array_bounds_check=False) ret += [buf] return ret, ret_ofst, ret_len
CWE-119
26
def format_time(self, data): """ A hook to control how times are formatted. Can be overridden at the ``Serializer`` level (``datetime_formatting``) or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``). Default is ``iso-8601``, which looks like "03:02:14". """ if self.datetime_formatting == 'rfc-2822': return format_time(data) return data.isoformat()
CWE-20
0
def __init__( self, credentials, host, request_uri, headers, response, content, http
CWE-400
2
def response(self, response, content): if "authentication-info" not in response: challenge = _parse_www_authenticate(response, "www-authenticate").get( "digest", {} ) if "true" == challenge.get("stale"): self.challenge["nonce"] = challenge["nonce"] self.challenge["nc"] = 1 return True else: updated_challenge = _parse_www_authenticate( response, "authentication-info" ).get("digest", {}) if "nextnonce" in updated_challenge: self.challenge["nonce"] = updated_challenge["nextnonce"] self.challenge["nc"] = 1 return False
CWE-400
2