Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Aug 30, 2019
1 parent 4439e46 commit 8040534
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
127 changes: 127 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

/.idea/
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: python
install:
- pip install -U pip
- pip install pytest pyyaml rfc3987 jsonschema
script:
- pytest
108 changes: 108 additions & 0 deletions tests/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://chaitin.github.io/xray/assets/yaml-poc-schema.json",
"title": "POC Check",
"description": "A tool that checks XRay POC",
"type": "object",
"properties": {
"name": {
"description": "POC name",
"type": "string",
"pattern": "\\Apoc-yaml-(?!-)[a-z0-9\\-]+(?<!-)\\Z"
},
"rules": {
"description": "POC rules",
"type": "array",
"minItems": 1,
"items": {
"description": "A rule",
"type": "object",
"properties": {
"method": {
"description": "request method",
"type": "string",
"enum": [
"GET",
"POST",
"DELETE",
"PATCH",
"OPTIONS",
"HEAD",
"PUT",
"CONNECT"
]
},
"path": {
"description": "request path",
"type": "string",
"pattern": "^/.*"
},
"headers": {
"description": "request HTTP headers",
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"body": {
"description": "request body",
"type": "string"
},
"follow_redirects": {
"description": "This option determines whether the HTTP request will follow the jump",
"type": "boolean"
},
"expression": {
"description": "a CEL expression what determines whether the vulnerability exists",
"type": "string"
},
"search": {
"description": "a pattern to search the keywords in last response body",
"type": "string"
}
},
"required": [
"expression"
],
"additionalProperties": false
}
},
"detail": {
"description": "output details",
"type": "object",
"properties": {
"author": {
"description": "POC author",
"type": "string"
},
"test_env": {
"description": "a link that describe or reproduce the vulnerability",
"type": "string",
"format": "iri"
}
},
"additionalProperties": true
},
"test": {
"description": "for unittest",
"type": "object",
"properties": {
"target": {
"description": "test env target",
"type": "string",
"format": "iri"
},
"skip": {
"description": "This option determines whether the test is skip",
"type": "boolean"
}
},
"additionalProperties": false
}
},
"required": [
"name",
"rules"
],
"additionalProperties": false
}
24 changes: 24 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re
import pytest
import subprocess
import pathlib

ROOT = pathlib.Path(__file__).absolute().parent.parent
POCNAME_PATTERN = re.compile(r'\A(?!-)[a-z0-9\-]+(?<!-)\.yml\Z')


@pytest.fixture
def filenames():
diff = subprocess.check_output(['git', 'diff', '--name-only', 'master'], cwd=str(ROOT))
if diff:
return [filename.strip() for filename in diff.decode().split('\n')]
else:
return []


def test_filename(filenames):
for filename in filenames:
poc_file = pathlib.Path(filename)
assert poc_file.parent.absolute() == ROOT.absolute(), 'POC must be in pocs/ folder, without subfolder'
assert poc_file.suffix == '.yml', 'POC extension must be .yml'
assert POCNAME_PATTERN.match(poc_file.name), 'filename format is wrong'
33 changes: 33 additions & 0 deletions tests/test_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
import logging
import pytest
import pathlib
import yaml
import json
from jsonschema import validate, draft7_format_checker

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
ROOT = pathlib.Path(__file__).absolute().parent.parent
SCHEMA_FILE = ROOT / 'tests' / 'schema.json'
SCHEMA_DATA = json.loads(SCHEMA_FILE.read_bytes())


@pytest.fixture
def pocs():
return [file for file in ROOT.glob('pocs/*.yml')]


def check_field(f: pathlib.Path):
logging.info("check for %s", f.name)
data = yaml.safe_load(f.read_bytes())

validate(instance=data, schema=SCHEMA_DATA, format_checker=draft7_format_checker)


def check_poc_name(name):
pass


def test_yaml_parse(pocs):
for f in pocs:
check_field(f)

0 comments on commit 8040534

Please sign in to comment.