Skip to main content

An assertive security library.

Project description

Nosorog

Introdaction

An assertive security library.

Requirements

3.5 >= Python <= 3.10

Installing

pip install nosorog

Testing

cd /path/to/lib/
python3 -m unittest discover

How to use

Exceptions

Exception Default message
NosorogMangledNameError "Use method`s dunder name instead."
NosorogWrongPlaceCallError (1) "Protected method can be called from specified methods only."
NosorogWrongPlaceCallError (2) "Protected method can not be called from other object, use self instead."
NosorogWentWrongError "Something broken."
NosorogTypeError child of TypeError. No especial message provided.

It is possible to use a concatenation of predefined and custom messages:

raise NosorogMangledNameError("Method __get accessible with _MangledName__get() call.")
# NosorogMangledNameError: "Use method`s dunder name instead. Method __get accessible with _MangledName__get() call."

But it is one exclusion: NosorogWrongPlaceCallError uses the message "Protected method can be called from specified methods only." by default and or other instead:

from nosorog.exceptions.mixins.nosorog_exception_messages import NosorogExceptionMessages

raise NosorogWrongPlaceCallError(NosorogExceptionMessages.use_self)
# NosorogExceptionMessages: "Protected method can not be called from other object, use self instead."

It is not concatenated.

Full list of predefined messages

Attribute Message
protected_from_not_private_call "This method protected from not private call."
method_protected "This method protected."
wrong_place "Protected method can be called from specified places only."
use_self "Protected method can not be called from other object, use self instead."
mangled_call_blocked "Use method`s dunder name instead."

Class based decorators

To import class based decorators use:

from nosorog.decorators import protect_private, copy_dicts, silent
Decorator Description
@silent intercepts all the exceptions of Nosorog and returns None instead.
@silent.include(exceptions) same as above and list of provided exceptions to.
--- ---
@protect_private.block_mangled_call protect of name mangling usage.
@protect_private.one_obj decorated method accessible with self usage only.
@protect_private.one_method("method_name") decorated method accessible from one method only.
@protect_private.call_from(methods) decorated method accessible from the methods provided in list only.
--- ---
@copy_dicts makes shallow copy of all the dicts in args and kwargs
@copy_dicts.deep_args makes deep copy of all the dicts in args
@copy_dicts.deep_kwargs makes deep copy of all the dicts in kwargs
@copy_dicts.deep_all makes deep copy of all the dicts in args and kwargs
@copy_dicts.shallow_args makes shallow copy of all the dicts in args
@copy_dicts.shallow_kwargs makes shallow copy of all the dicts in kwargs
@copy_dicts.shallow_all makes shallow copy of all the dicts in args and kwargs

Function based decorators

To import function based decorators use:

from nosorog.decorators.function_based_decorators import protect_private, copy_dicts, protect_ids, protected_call
Decorator Description
@protect_private(allowed_list=list) make a call with _Class__private_method() impossible. allowed_list it is str names of method which you can call the private method from. also support 'self' (str) for calls from same object only.
@protected_call(from_method=str, from_file=str) make the attack by the file injection impossible.
@copy_dicts(deep_copy=bool) make a copy of dicts in args and kwargs.
@protect_ids(id_names=[str]) trying to convert id to int or throw Exception.

Examples

This explanation written for the function based decorators. Class based decorators works the same way with some differences in the syntax. Read the full documentation on https://nosorog.readthedocs.io.

Private methods

Usage of dunder methods ( __method() ) protects the code avoiding direct access to the method.

class Example:
    def __get_data(self):
        return 1

>>> Example().__get_data()  # AttributeError: 'Example' object has no attribute '__get_data'

But it is possible to use the name mangling.

>>> Example()._Example__get_data()  # 1

Nosorog provides simple and pushy way to protect the dunder method.

class Example:
    @protect_private(allowed_list=['trusted_func'])
    def __get_data(self):
        return 1

class Trusted:
    @staticmethod
    def trusted_func():
        return Example()._Example__get_data()

>>> Example().__get_data()  # AttributeError: 'Example' object has no attribute '__get_data'
>>> Example()._Example__get_data()  # Exception: This method protected from not private call.
>>> Trusted()._Example__get_data()  # 1

Also, str 'self' can be used as a list item to make impossible to call without self.

class Example:
    @protect_private(allowed_list=['trusted_func', 'self'])
    def __get_data(self):
        return 1

    def trusted_func(self):
        return self.__get_data()


class Trusted:
    @staticmethod
    def trusted_func():
        return Example()._Example__get_data()

>>> Example().trusted_func()  # 1
>>> Trusted().trusted_func()  # Exception: This method can not be called from other object, use self instead.

Localization of method call

Python does not provide an easy way to limit where the method can be called from. This makes it possible to conduct an attack by File Injection. With the help of the Nosorog library it is possible to specify the places from which the method can be called.

class Example:
    @protected_call(from_method='safe_method', from_file=os.path.abspath(__file__))
    def __get_data(self):
        return 1

class Trusted:
    # Place it to the same file as described in the decorator usage.
    def safe_method():
        return Example()._Example__get_data()  # 1

This is just a variation of the previous decorator.

Protection of the dicts

In the projects where the undefined number of dicts can be passed in args and kwargs, it is possible to make a deep copy of each if needed.

class Example:
    @copy_dicts(deep_copy=False)
    def some_method(self, *args, **kwargs):
        # now dicts are shallow copies
        pass

Use @copy_dicts(deep_copy=True) to make deep copies.

Protection of ids

This method has been added just for fun. It is converts all the ids in the list if possible or throws the TypeError.

class Example:
    @protect_ids(id_names=['user_id', 'pk'])
    def some_method(user_id=None, pk=None)
        pass

Possible Exceptions

@protect_ids(id_names=['user_id', dict()])
>>> Example().some_method(user_id='1')  # TypeError: Wrong format of id_names in decorator. Must be list of str.

@protect_ids(id_names=['user_id', 'pk'])
>>> Example().some_method(user_id=1.234, pk='text_id')  # TypeError: Received the ids of wrong type.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nosorog-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nosorog-0.2.0-py3.9.egg (4.6 kB view details)

Uploaded Egg

nosorog-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file nosorog-0.2.0.tar.gz.

File metadata

  • Download URL: nosorog-0.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nosorog-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9f6efc437f4adb2d1fe86b32079fd4ec31e0051a7dd44dab200bce4ba1f08cff
MD5 d876bf89c77548aecd7c1ba113413262
BLAKE2b-256 b42189fb462649d741f84735863678f722c555cd62e8ab850f7f0f87faf9bc0b

See more details on using hashes here.

File details

Details for the file nosorog-0.2.0-py3.9.egg.

File metadata

  • Download URL: nosorog-0.2.0-py3.9.egg
  • Upload date:
  • Size: 4.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nosorog-0.2.0-py3.9.egg
Algorithm Hash digest
SHA256 6966242f712f0ce25a559917ae869ade55b5216ad9938977116b589949b474d8
MD5 75fd1d564ca7c38ba9b35e27354ae066
BLAKE2b-256 fc912396790af66417f91d7d39a7f694457541222ff9b6de16121efc6711463d

See more details on using hashes here.

File details

Details for the file nosorog-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nosorog-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nosorog-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b3564657e1b8fa5443ab358d1835f22509f3977fd602433bb363ad782e6a10f
MD5 1c78480a64f4f01f3905b6fe8f32ec70
BLAKE2b-256 4a0cb60125a4da958ea77e78a74b98ff2d13874808688095b0992d1509d5326a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page