From 88abf882aecb1e5c1a92ea4d9b868d02d37de687 Mon Sep 17 00:00:00 2001 From: Sree Siva Sandeep Palaparthi Date: Mon, 11 Nov 2019 17:44:09 -0500 Subject: [PATCH] feat: add tests for pastebin url analyzer (#109) * feat: add tests for pastebin url analyzer * fix: remove www from valid url * feat(url-analyzer): make pastbin www tests to positive * FIX: Analyzer should only catch certain links The pastebinurlanalyzer should only match on certain strings in the URL. I specified it a bit more precisely --- pastepwn/analyzers/pastebinurlanalyzer.py | 2 +- .../tests/pastebinurlanalyzer_test.py | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 pastepwn/analyzers/tests/pastebinurlanalyzer_test.py diff --git a/pastepwn/analyzers/pastebinurlanalyzer.py b/pastepwn/analyzers/pastebinurlanalyzer.py index 816e991..33919dc 100644 --- a/pastepwn/analyzers/pastebinurlanalyzer.py +++ b/pastepwn/analyzers/pastebinurlanalyzer.py @@ -7,5 +7,5 @@ class PastebinURLAnalyzer(URLAnalyzer): name = "PastebinURLAnalyzer" def __init__(self, actions, resolve=False): - regex = r"((?:https?:\/\/)?pastebin.com\/\S+)" + regex = r"((?:https?:\/\/)?pastebin.com\/[a-zA-Z0-9]{3,})" super().__init__(actions, regex, resolve) diff --git a/pastepwn/analyzers/tests/pastebinurlanalyzer_test.py b/pastepwn/analyzers/tests/pastebinurlanalyzer_test.py new file mode 100644 index 0000000..5d7717e --- /dev/null +++ b/pastepwn/analyzers/tests/pastebinurlanalyzer_test.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import unittest +from unittest import mock + +from pastepwn.analyzers.pastebinurlanalyzer import PastebinURLAnalyzer + + +class TestPastebinURLAnalyzer(unittest.TestCase): + def setUp(self): + self.analyzer = PastebinURLAnalyzer(None) + self.paste = mock.Mock() + + def test_match_positive(self): + self.paste.body = "https://pastebin.com/xyz" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "https://pastebin.com/xyz/" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "http://pastebin.com/xyz" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "http://pastebin.com/xyz/" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "https://pastebin.com/xyz " + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "www.pastebin.com/xyz" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "pastebin.com/xyx" + self.assertTrue(self.analyzer.match(self.paste)) + self.paste.body = "This is a pastebin URL: pastebin.com/ya249asd - and this is a test" + + def test_match_negative(self): + self.paste.body = "" + self.assertFalse(self.analyzer.match(self.paste)) + self.paste.body = None + self.assertFalse(self.analyzer.match(self.paste)) + self.paste.body = "https://google.com/xyz" + self.assertFalse(self.analyzer.match(self.paste)) + self.paste.body = "xyzpastebin.com/k" + self.assertFalse(self.analyzer.match(self.paste)) + self.paste.body = "https://pastebin.com/" + self.assertFalse(self.analyzer.match(self.paste)) + + if __name__ == '__main__': + unittest.main()