Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions test/modules/filters/test_010_deflate_alter_etag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import random
import string

import pytest

from pyhttpd.conf import HttpdConf

# DeflateAlterETag controls how mod_deflate modifies the ETag header when
# compression is applied. Three modes exist:
# AddSuffix -- append a compression tag, so compressed and plain ETags differ
# NoChange -- leave the ETag alone, so both representations share one
# Remove -- strip the ETag from compressed responses entirely


class TestDeflateAlterEtag:

@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
# The ScriptSock path warning is harmless; it fires when the
# working-tree path is long (e.g. in CI worktrees).
env.httpd_error_log.add_ignored_lognos(["AH01254"])
# A document large enough that mod_deflate actually compresses it.
doc_dir = os.path.join(env.server_dir, "htdocs", "test1")
body = ''.join(random.choices(string.ascii_lowercase, k=8000))
for name in ("etag1.html", "etag2.html", "etag3.html"):
with open(os.path.join(doc_dir, name), "w") as f:
f.write(f"<html><body>{body}</body></html>")

def configure(self, env, alter_etag):
doc_root = os.path.join(env.server_dir,
"htdocs", "test1").replace('\\', '/')
conf = HttpdConf(env, extras={
f"test1.{env.http_tld}": f"""
<Directory "{doc_root}">
AddOutputFilterByType DEFLATE text/html
</Directory>
DeflateAlterETag {alter_etag}
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0

def get_etag(self, env, path, accept_gzip=False):
url = env.mkurl("http", "test1", path)
options = ["-H", "Cache-Control: no-cache"]
if accept_gzip:
options.extend(["-H", "Accept-Encoding: gzip"])
r = env.curl_get(url, options=options)
assert r.response, f"no response for {path}"
assert r.response["status"] == 200, \
f"status {r.response['status']} for {path}"
return r.response["header"].get("etag")

# AddSuffix: the compressed ETag gets a suffix, so it differs from the
# plain one.
def test_filters_010_01_addsuffix_etags_differ(self, env):
self.configure(env, "AddSuffix")
etag_plain = self.get_etag(env, "/etag1.html", accept_gzip=False)
etag_gzip = self.get_etag(env, "/etag1.html", accept_gzip=True)
assert etag_plain is not None, "plain response should have an ETag"
assert etag_gzip is not None, "gzip response should have an ETag"
assert etag_plain != etag_gzip, \
(f"AddSuffix: ETags should differ between plain and compressed, "
f"got {etag_plain} and {etag_gzip}")

# NoChange: the ETag is left as-is, so both representations match.
def test_filters_010_02_nochange_etags_match(self, env):
self.configure(env, "NoChange")
etag_plain = self.get_etag(env, "/etag2.html", accept_gzip=False)
etag_gzip = self.get_etag(env, "/etag2.html", accept_gzip=True)
assert etag_plain is not None, "plain response should have an ETag"
assert etag_gzip is not None, "gzip response should have an ETag"
assert etag_plain == etag_gzip, \
(f"NoChange: ETags should be identical, "
f"got {etag_plain} and {etag_gzip}")

# Remove: the compressed response has no ETag at all.
def test_filters_010_03_remove_etag_stripped(self, env):
self.configure(env, "Remove")
etag_plain = self.get_etag(env, "/etag3.html", accept_gzip=False)
etag_gzip = self.get_etag(env, "/etag3.html", accept_gzip=True)
assert etag_plain is not None, "plain response should have an ETag"
assert etag_gzip is None, \
f"Remove: gzip response should have no ETag, got {etag_gzip}"
70 changes: 70 additions & 0 deletions test/modules/filters/test_011_sed_large_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os

import pytest

from pyhttpd.conf import HttpdConf

# mod_sed must handle large request bodies without crashing. A historical
# issue caused a segmentation fault when a large body was processed through
# an InputSed filter due to integer overflow in buffer management. This
# test sends a multi-megabyte POST body through InputSed and verifies that
# the server responds and stays alive.

# 8 MB is enough to exercise the large-input paths without making the test
# impractically slow.
LARGE_SIZE = 8 * 1024 * 1024

# Bound the request so a regression that stops answering fails the test
# rather than hanging the suite.
BOUNDED = ["--max-time", "60"]


class TestSedLargeInput:

@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
# The ScriptSock path warning is harmless; it fires when the
# working-tree path is long (e.g. in CI worktrees).
env.httpd_error_log.add_ignored_lognos(["AH01254"])
doc_dir = os.path.join(env.server_dir, "htdocs", "test1")
with open(os.path.join(doc_dir, "target.html"), "w") as f:
f.write("This is the target page")
# Pre-create a large file to POST through the InputSed filter.
large_file = os.path.join(env.server_dir, "large_input.bin")
with open(large_file, "wb") as f:
chunk = b"A" * (1024 * 1024)
for _ in range(LARGE_SIZE // (1024 * 1024)):
f.write(chunk)

@staticmethod
def large_file_path(env):
return os.path.join(env.server_dir, "large_input.bin")

def test_filters_011_01_large_body_no_crash(self, env):
doc_root = os.path.join(env.server_dir,
"htdocs", "test1").replace('\\', '/')
conf = HttpdConf(env, extras={
f"test1.{env.http_tld}": f"""
<Directory "{doc_root}">
AddInputFilter SED html
InputSed "s/A/Z/g"
</Directory>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0
# POST the large file through the InputSed filter.
url = env.mkurl("http", "test1", "/target.html")
large_file = self.large_file_path(env)
r = env.curl_raw(url, timeout=60, options=list(BOUNDED) + [
"-X", "POST",
"--data-binary", f"@{large_file}",
])
assert r.response, "no response: server may have crashed"
assert r.response["status"] in (200, 400), \
f"unexpected status {r.response['status']}"
# The server must still be running after processing the large input.
assert env.is_live(), "httpd is not running after large input"
env.httpd_error_log.ignore_recent(
matches=[r".*exit signal Segmentation fault.*"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't be needed (right?) and we generally do want to tests to fail in that case

Loading