|  | 
|  | 1 | +""" | 
|  | 2 | +Loaded in Step 2/4/Optional 6 (/clear-site-data/clear-cache.https.html) | 
|  | 3 | +Sending Message for Step 3/5/Optional 7 (/clear-site-data/clear-cache.https.html) | 
|  | 4 | +""" | 
|  | 5 | +import uuid | 
|  | 6 | +import random | 
|  | 7 | + | 
|  | 8 | +def generate_png(width, height, color=(0, 0, 0)): | 
|  | 9 | +    import zlib | 
|  | 10 | +    import struct | 
|  | 11 | +    def chunk(chunk_type, data): | 
|  | 12 | +        return ( | 
|  | 13 | +            struct.pack(">I", len(data)) + | 
|  | 14 | +            chunk_type + | 
|  | 15 | +            data + | 
|  | 16 | +            struct.pack(">I", zlib.crc32(chunk_type + data) & 0xffffffff) | 
|  | 17 | +        ) | 
|  | 18 | + | 
|  | 19 | +    # PNG signature | 
|  | 20 | +    png = b"\x89PNG\r\n\x1a\n" | 
|  | 21 | + | 
|  | 22 | +    # IHDR chunk | 
|  | 23 | +    ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) | 
|  | 24 | +    png += chunk(b'IHDR', ihdr) | 
|  | 25 | + | 
|  | 26 | +    # IDAT chunk: RGB pixels | 
|  | 27 | +    row = b'\x00' + bytes(color * width) | 
|  | 28 | +    raw = row * height | 
|  | 29 | +    idat = zlib.compress(raw) | 
|  | 30 | +    png += chunk(b'IDAT', idat) | 
|  | 31 | + | 
|  | 32 | +    # IEND chunk | 
|  | 33 | +    png += chunk(b'IEND', b'') | 
|  | 34 | +    return png | 
|  | 35 | + | 
|  | 36 | +def main(request, response): | 
|  | 37 | +    # type of response: | 
|  | 38 | +    # - General purpose: returns random uuid or forwards uuid from iframe | 
|  | 39 | +    #   - "single_html": Main page html file with a different postMessage uuid on each response | 
|  | 40 | +    # - Pages for simple testing normal http cache: | 
|  | 41 | +    #   - "json": Json that always responds with a different uuid in a single-element array | 
|  | 42 | +    #   - "html_embed_json": Main page html that embeds a cachable version of the above json | 
|  | 43 | +    # - Pages for testing specialized caches | 
|  | 44 | +    #   - "image": Image that returns random dimensions up to 1024x1024 each time | 
|  | 45 | +    #   - "html_embed_image": Main page html that embeds a cachable version of the above image | 
|  | 46 | +    #   - "css": Style sheet with random uuid variable | 
|  | 47 | +    #   - "html_embed_css": Main page html that embeds a cachable version of the above css | 
|  | 48 | +    #   - "js": Script that returns a different uuid each time | 
|  | 49 | +    #   - "html_embed_js": Main page html that embeds a cachable version of the above js file | 
|  | 50 | +    response_type = request.GET.first(b"response") | 
|  | 51 | + | 
|  | 52 | +    cache_helper = request.GET.first(b"cache_helper") | 
|  | 53 | + | 
|  | 54 | +    # force enable caching when present or force disable if not | 
|  | 55 | +    cache = b"cache" in request.GET | 
|  | 56 | +    clear = None | 
|  | 57 | +    if b"clear" in request.GET: | 
|  | 58 | +        clear = request.GET.first(b"clear") | 
|  | 59 | +    if b"clear_first" in request.GET: | 
|  | 60 | +        if request.server.stash.take(cache_helper) is None: | 
|  | 61 | +            clear = request.GET.first(b"clear_first") | 
|  | 62 | +            request.server.stash.put(cache_helper, ()) | 
|  | 63 | + | 
|  | 64 | +    headers = [] | 
|  | 65 | +    if response_type == b"json": | 
|  | 66 | +        headers += [(b"Content-Type", b"application/json")] | 
|  | 67 | +    elif response_type == b"image": | 
|  | 68 | +        headers += [(b"Content-Type", b"image/png")] | 
|  | 69 | +    elif response_type == b"css": | 
|  | 70 | +        headers += [(b"Content-Type", b"text/css")] | 
|  | 71 | +    elif response_type == b"js": | 
|  | 72 | +        headers += [(b"Content-Type", b"text/javascript")] | 
|  | 73 | +    else: | 
|  | 74 | +        headers += [(b"Content-Type", b"text/html")] | 
|  | 75 | + | 
|  | 76 | +    headers += [(b"Access-Control-Allow-Origin", b"*")] | 
|  | 77 | +    if cache: | 
|  | 78 | +        headers += [(b"cache-control", b"public, max-age=31536000, immutable")] | 
|  | 79 | +    else: | 
|  | 80 | +        headers += [(b"cache-control", b"no-store")] | 
|  | 81 | + | 
|  | 82 | +    if clear is not None: | 
|  | 83 | +        if clear == b"all": | 
|  | 84 | +            headers += [(b"Clear-Site-Data", b'"*"')] | 
|  | 85 | +        else: | 
|  | 86 | +            headers += [(b"Clear-Site-Data", b'"' + clear + b'"')] | 
|  | 87 | + | 
|  | 88 | +    if response_type == b"single_html": | 
|  | 89 | +        iframe = "" | 
|  | 90 | +        if b"iframe" in request.GET: | 
|  | 91 | +            # forward message from iframe to opener | 
|  | 92 | +            iframe_url = request.GET.first(b"iframe").decode() | 
|  | 93 | +            content = f''' | 
|  | 94 | +                <script> | 
|  | 95 | +                    // forward iframe uuid to opener | 
|  | 96 | +                    window.addEventListener('message', function(event) {{ | 
|  | 97 | +                        if(window.opener) {{ | 
|  | 98 | +                            window.opener.postMessage(event.data, "*"); | 
|  | 99 | +                        }} else {{ | 
|  | 100 | +                            window.parent.postMessage(event.data, "*"); | 
|  | 101 | +                        }} | 
|  | 102 | +                        window.close(); | 
|  | 103 | +                    }}); | 
|  | 104 | +                </script> | 
|  | 105 | +                <br> | 
|  | 106 | +                {request.url}<br> | 
|  | 107 | +                {iframe_url}<br> | 
|  | 108 | +                <iframe src="{iframe_url}"></iframe> | 
|  | 109 | +                </body> | 
|  | 110 | +            ''' | 
|  | 111 | +        else: | 
|  | 112 | +            # send unique UUID. Cache got cleared when uuids don't match. | 
|  | 113 | +            u = uuid.uuid4() | 
|  | 114 | +            content = f''' | 
|  | 115 | +                <script> | 
|  | 116 | +                    if(window.opener) {{ | 
|  | 117 | +                        window.opener.postMessage("{u}", "*"); | 
|  | 118 | +                    }} else {{ | 
|  | 119 | +                        window.parent.postMessage("{u}", "*"); | 
|  | 120 | +                    }} | 
|  | 121 | +                    window.close(); | 
|  | 122 | +                </script> | 
|  | 123 | +                <body> | 
|  | 124 | +                    {request.url} | 
|  | 125 | +                </body>''' | 
|  | 126 | +    elif response_type == b"json": | 
|  | 127 | +        # send unique UUID. helper for below "html_embed_json" | 
|  | 128 | +        content = f'''["{uuid.uuid4()}"]''' | 
|  | 129 | +    elif response_type == b"html_embed_json": | 
|  | 130 | +        url = request.url_parts.path + "?response=json&cache&cache_helper=" + cache_helper.decode() | 
|  | 131 | +        content = f''' | 
|  | 132 | +            <script> | 
|  | 133 | +                fetch("{url}") | 
|  | 134 | +                    .then(response => response.json()) | 
|  | 135 | +                    .then(uuid => {{ | 
|  | 136 | +                        window.opener.postMessage(uuid[0], "*"); | 
|  | 137 | +                        window.close(); | 
|  | 138 | +                    }}); | 
|  | 139 | +            </script> | 
|  | 140 | +            <body> | 
|  | 141 | +                {request.url}<br> | 
|  | 142 | +                {url} | 
|  | 143 | +            </body>''' | 
|  | 144 | +    elif response_type == b"image": | 
|  | 145 | +        # send uniquely sized images, because that info can be retrived from html and definitly using the image cache | 
|  | 146 | +        # helper for below "html_embed_image" | 
|  | 147 | +        content = generate_png(random.randint(1, 1024), random.randint(1, 1024)) | 
|  | 148 | +    elif response_type == b"html_embed_image": | 
|  | 149 | +        urls = [request.url_parts.path + "?response=image&cache&cache_helper=" + cache_helper.decode() + "&img=" + str(i) for i in range(2)] | 
|  | 150 | +        content = f''' | 
|  | 151 | +            <!DOCTYPE html> | 
|  | 152 | +            <script> | 
|  | 153 | +                addEventListener("load", () => {{ | 
|  | 154 | +                    let img1 = document.getElementById("randomess1"); | 
|  | 155 | +                    let img2 = document.getElementById("randomess2"); | 
|  | 156 | +                    let id = img1.naturalWidth + "x" + img1.naturalHeight; | 
|  | 157 | +                    id += "-" + img2.naturalWidth + "x" + img2.naturalHeight | 
|  | 158 | +                    window.opener.postMessage(id, "*"); | 
|  | 159 | +                    window.close(); | 
|  | 160 | +                }}) | 
|  | 161 | +            </script> | 
|  | 162 | +            <body> | 
|  | 163 | +                {request.url}<br> | 
|  | 164 | +                <img id="randomess1" src="{urls[0]}"></img><br> | 
|  | 165 | +                <img id="randomess2" src="{urls[1]}"></img><br> | 
|  | 166 | +            </body>''' | 
|  | 167 | +    elif response_type == b"css": | 
|  | 168 | +        # send unique UUID. helper for below "html_embed_css" | 
|  | 169 | +        content = f''' | 
|  | 170 | +        :root {{ | 
|  | 171 | +            --uuid: "{uuid.uuid4()}" | 
|  | 172 | +        }}''' | 
|  | 173 | +    elif response_type == b"html_embed_css": | 
|  | 174 | +        url = request.url_parts.path + "?response=css&cache&cache_helper=" + cache_helper.decode() | 
|  | 175 | +        content = f''' | 
|  | 176 | +            <!DOCTYPE html> | 
|  | 177 | +            <link rel="stylesheet" href="{url}"> | 
|  | 178 | +            <script> | 
|  | 179 | +                let computed = getComputedStyle(document.documentElement); | 
|  | 180 | +                let uuid = computed.getPropertyValue("--uuid").trim().replaceAll('"', ''); | 
|  | 181 | +                window.opener.postMessage(uuid, "*"); | 
|  | 182 | +                window.close(); | 
|  | 183 | +            </script> | 
|  | 184 | +            <body> | 
|  | 185 | +                {request.url}<br> | 
|  | 186 | +                {url} | 
|  | 187 | +            </body>''' | 
|  | 188 | +    elif response_type == b"js": | 
|  | 189 | +        # send unique UUID. helper for below "html_embed_js" | 
|  | 190 | +        content = f''' | 
|  | 191 | +            window.opener.postMessage("{uuid.uuid4()}", "*"); | 
|  | 192 | +            window.close(); | 
|  | 193 | +        ''' | 
|  | 194 | +    elif response_type == b"html_embed_js": | 
|  | 195 | +        url = request.url_parts.path + "?response=js&cache&cache_helper=" + cache_helper.decode() | 
|  | 196 | +        content = f''' | 
|  | 197 | +            <script src="{url}"></script> | 
|  | 198 | +            <body> | 
|  | 199 | +                {request.url}<br> | 
|  | 200 | +                {url} | 
|  | 201 | +            </body>''' | 
|  | 202 | + | 
|  | 203 | +    return 200, headers, content | 
0 commit comments