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

ws: Prevent search engine indexing with robots.txt #21430

Merged
merged 1 commit into from
Dec 13, 2024
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
11 changes: 11 additions & 0 deletions src/ws/cockpithandlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,17 @@ cockpit_handler_default (CockpitWebServer *server,
path = cockpit_web_response_get_path (response);
g_return_val_if_fail (path != NULL, FALSE);

/* robots.txt is unauthorized and works on any directory */
if (g_str_has_suffix (path, "/robots.txt"))
{
g_autoptr(GHashTable) out_headers = cockpit_web_server_new_table ();
g_hash_table_insert (out_headers, g_strdup ("Content-Type"), g_strdup ("text/plain"));
const char *body ="User-agent: *\nDisallow: /\n";
g_autoptr(GBytes) content = g_bytes_new_static (body, strlen (body));
cockpit_web_response_content (response, out_headers, content, NULL);
return TRUE;
}

resource = g_str_has_prefix (path, "/cockpit/") ||
g_str_has_prefix (path, "/cockpit+") ||
g_str_equal (path, "/cockpit");
Expand Down
6 changes: 6 additions & 0 deletions test/verify/check-connection
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,12 @@ class TestConnection(testlib.MachineCase):
self.assertIn("HTTP/1.1 200", out)
self.assertNotIn("<html>", out)

# robots.txt from any directory and with/without TLS, and without auth
expected = "User-agent: *\nDisallow: /\n"
self.assertEqual(m.execute("curl http://localhost:9000/robots.txt"), expected)
self.assertEqual(m.execute("curl http://localhost:9000/somedir/robots.txt"), expected)
self.assertEqual(m.execute("curl -k https://localhost:9000/robots.txt"), expected)

@testlib.nondestructive
def testHeadRequest(self):
m = self.machine
Expand Down
Loading