Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI encoding of redirects #866

Merged
merged 4 commits into from
Jan 10, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ ParameterizedMessage suggestSearchMsg(const std::string& searchURL, const std::s
std::unique_ptr<Response>
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
{
const auto path = kiwix::urlEncode(item.getPath());
const auto path = kiwix::urlEncode(item.getPath(), true);
const auto redirectUrl = m_root + "/content/" + bookName + "/" + path;
return Response::build_redirect(*this, redirectUrl);
}
Expand Down
Binary file modified test/data/corner_cases.zim
Binary file not shown.
1 change: 1 addition & 0 deletions test/data/corner_cases/wtf.html
1 change: 1 addition & 0 deletions test/data/corner_cases/wtf?
11 changes: 11 additions & 0 deletions test/data/corner_cases/wtf?.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
mgautierfr marked this conversation as resolved.
Show resolved Hide resolved
<html>
<head>
<meta charset="utf-8" />
<title>WTF?</title>
</head>
<body>
<p>WTF? is an acronym coined by cryptography and security researcher Walter
Thomas Freiwald. It stands for "Will They Factorize?"</p>
</body>
</html>
15 changes: 8 additions & 7 deletions test/data/create_corner_cases_zim_file
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

cd "$(dirname "$0")"
rm -f corner_cases.zim
zimwriterfs -w empty.html \
-f empty.png \
-l=en \
-t="ZIM corner cases" \
-d="" \
-c="" \
-p="" \
zimwriterfs --withoutFTIndex --dont-check-arguments \
-w empty.html \
-I empty.png \
-l en \
-t "ZIM corner cases" \
-d "" \
-c "" \
-p "" \
corner_cases \
corner_cases.zim \
&& echo 'corner_cases.zim was successfully created' \
Expand Down
25 changes: 18 additions & 7 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const ResourceCollection resources200Compressible{
{ DYNAMIC_CONTENT, "/ROOT/catalog/search" },

{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/root.xml" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/languages" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/entries" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/partial_entries" },

Expand Down Expand Up @@ -150,16 +149,17 @@ const ResourceCollection resources200Uncompressible{
{ DYNAMIC_CONTENT, "/ROOT/catalog/searchdescription.xml" },

{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/categories" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/languages" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/searchdescription.xml" },
{ DYNAMIC_CONTENT, "/ROOT/catalog/v2/illustration/6f1d19d0-633f-087b-fb55-7ac324ff9baf?size=48" },

{ DYNAMIC_CONTENT, "/ROOT/catch/external?source=www.example.com" },

{ ZIM_CONTENT, "/ROOT/content/zimfile/I/m/Ray_Charles_classic_piano_pose.jpg" },

{ ZIM_CONTENT, "/ROOT/content/corner_cases/A/empty.html" },
{ ZIM_CONTENT, "/ROOT/content/corner_cases/-/empty.css" },
{ ZIM_CONTENT, "/ROOT/content/corner_cases/-/empty.js" },
{ ZIM_CONTENT, "/ROOT/content/corner_cases/empty.html" },
{ ZIM_CONTENT, "/ROOT/content/corner_cases/empty.css" },
{ ZIM_CONTENT, "/ROOT/content/corner_cases/empty.js" },


// The following url's responses are too small to be compressed
Expand Down Expand Up @@ -1145,7 +1145,7 @@ TEST_F(ServerTest, RandomPageRedirectsToAnExistingArticle)
auto g = zfs1_->GET("/ROOT/random?content=zimfile");
ASSERT_EQ(302, g->status);
ASSERT_TRUE(g->has_header("Location"));
ASSERT_TRUE(kiwix::startsWith(g->get_header_value("Location"), "/ROOT/content/zimfile/A/"));
ASSERT_TRUE(kiwix::startsWith(g->get_header_value("Location"), "/ROOT/content/zimfile/A%2F"));
ASSERT_EQ(getCacheControlHeader(*g), "max-age=0, must-revalidate");
ASSERT_FALSE(g->has_header("ETag"));
}
Expand Down Expand Up @@ -1196,13 +1196,24 @@ TEST_F(ServerTest, NonEndpointUrlsAreRedirectedToContentUrls)
}
}

TEST_F(ServerTest, RedirectionsToURLsWithSpecialSymbols)
{
auto g = zfs1_->GET("/ROOT/content/corner_cases/wtf.html");
ASSERT_EQ(302, g->status);
ASSERT_TRUE(g->has_header("Location"));
ASSERT_EQ(g->get_header_value("Location"), "/ROOT/content/corner_cases/wtf%3F.html");
ASSERT_EQ(getCacheControlHeader(*g), "max-age=0, must-revalidate");
ASSERT_FALSE(g->has_header("ETag"));
}


TEST_F(ServerTest, BookMainPageIsRedirectedToArticleIndex)
{
{
auto g = zfs1_->GET("/ROOT/content/zimfile");
ASSERT_EQ(302, g->status);
ASSERT_TRUE(g->has_header("Location"));
ASSERT_EQ("/ROOT/content/zimfile/A/index", g->get_header_value("Location"));
ASSERT_EQ("/ROOT/content/zimfile/A%2Findex", g->get_header_value("Location"));
}
}

Expand Down Expand Up @@ -1507,7 +1518,7 @@ TEST_F(ServerTest, InvalidAndMultiRangeByteRangeRequestsResultIn416Responses)

TEST_F(ServerTest, ValidByteRangeRequestsOfZeroSizedEntriesResultIn416Responses)
{
const char url[] = "/ROOT/content/corner_cases/-/empty.js";
const char url[] = "/ROOT/content/corner_cases/empty.js";

const char* ranges[] = {
"bytes=0-",
Expand Down