Created
March 13, 2018 17:42
-
-
Save richinseattle/8d1dcdb571baf45c0c4b41e94b5bd67d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # domato canvas generator.py replacement, put meta refresh tag at top of template.html | |
| # author: stelios | |
| from __future__ import print_function | |
| import os | |
| import re | |
| import random | |
| import sys | |
| import cherrypy | |
| import time | |
| parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) | |
| sys.path.append(parent_dir) | |
| from grammar import Grammar | |
| _N_MAIN_LINES = 1000 | |
| _N_EVENTHANDLER_LINES = 500 | |
| class DomFuzzer(object): | |
| def index(self): | |
| outfile = 'out/%d.html' % time.time() | |
| result = self.GenerateNewSample() | |
| if result is not None: | |
| print('Writing a sample to ' + outfile) | |
| try: | |
| f = open(outfile, 'w') | |
| f.write(result) | |
| f.close() | |
| except IOError: | |
| print('Error writing to output') | |
| return result | |
| index.exposed = True | |
| def __init__(self, grammar_dir): | |
| self.template = None | |
| self.jsgrammar = None | |
| f = open(os.path.join(grammar_dir, 'template.html')) | |
| self.template = f.read() | |
| f.close() | |
| self.jsgrammar = Grammar() | |
| err = self.jsgrammar.parse_from_file(os.path.join(grammar_dir, 'canvas.txt')) | |
| if err > 0: | |
| print('There were errors parsing grammar') | |
| return | |
| def generate_function_body(self, num_lines): | |
| js = '' | |
| js += self.jsgrammar._generate_code(num_lines) | |
| return js | |
| def GenerateNewSample(self): | |
| """Parses grammar rules from string. | |
| Args: | |
| template: A template string. | |
| htmlgrammar: Grammar for generating HTML code. | |
| cssgrammar: Grammar for generating CSS code. | |
| jsgrammar: Grammar for generating JS code. | |
| Returns: | |
| A string containing sample data. | |
| """ | |
| result = self.template | |
| handlers = False | |
| while '<canvasfuzz>' in result: | |
| numlines = _N_MAIN_LINES | |
| if handlers: | |
| numlines = _N_EVENTHANDLER_LINES | |
| else: | |
| handlers = True | |
| result = result.replace( | |
| '<canvasfuzz>', | |
| self.generate_function_body(numlines), | |
| 1 | |
| ) | |
| return result | |
| def main(): | |
| cherrypy.quickstart(DomFuzzer(os.path.dirname(__file__))) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment