Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cd9e72b

Browse files
authored
Add X-Robots-Tag header to stop crawlers from indexing media (#8887)
Fixes / related to: #6533 This should do essentially the same thing as a robots.txt file telling robots to not index the media repo. https://developers.google.com/search/reference/robots_meta_tag Signed-off-by: Aaron Raimist <[email protected]>
1 parent ab7a24c commit cd9e72b

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

changelog.d/8887.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `X-Robots-Tag` header to stop web crawlers from indexing media.

synapse/rest/media/v1/_base.py

+5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ def _quote(x):
155155
request.setHeader(b"Cache-Control", b"public,max-age=86400,s-maxage=86400")
156156
request.setHeader(b"Content-Length", b"%d" % (file_size,))
157157

158+
# Tell web crawlers to not index, archive, or follow links in media. This
159+
# should help to prevent things in the media repo from showing up in web
160+
# search results.
161+
request.setHeader(b"X-Robots-Tag", "noindex, nofollow, noarchive, noimageindex")
162+
158163

159164
# separators as defined in RFC2616. SP and HT are handled separately.
160165
# see _can_encode_filename_as_token.

tests/rest/media/v1/test_media_storage.py

+13
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,16 @@ def _test_thumbnail(self, method, expected_body, expected_found):
362362
"error": "Not found [b'example.com', b'12345']",
363363
},
364364
)
365+
366+
def test_x_robots_tag_header(self):
367+
"""
368+
Tests that the `X-Robots-Tag` header is present, which informs web crawlers
369+
to not index, archive, or follow links in media.
370+
"""
371+
channel = self._req(b"inline; filename=out" + self.test_image.extension)
372+
373+
headers = channel.headers
374+
self.assertEqual(
375+
headers.getRawHeaders(b"X-Robots-Tag"),
376+
[b"noindex, nofollow, noarchive, noimageindex"],
377+
)

0 commit comments

Comments
 (0)