Commit Graph

9948 Commits

Author SHA1 Message Date
evazion
c17678d509 routes: add a new 404 page.
* Fix a bug where non-GET 404 requests weren't handled.
* Fix a bug where non-HTML 404 requests weren't handled.
* Show a random image from a specified pool on the 404 page.
2020-12-24 00:17:35 -06:00
evazion
039ccfa3af routes: optimize route order.
Put the most used routes at the top of the file to optimize route
performance.
2020-12-24 00:17:35 -06:00
evazion
a1cd9d2b5c routes: remove unused Danbooru 1 redirects.
Remove various redirects for old Danbooru 1 links. Most of these
received little to no traffic and were only used in a small number of
places in old comments or forum posts.
2020-12-24 00:17:35 -06:00
evazion
ca742db07a routes: remove legacy /user/index and /artist/index API endpoints.
These endpoints get zero traffic.
2020-12-24 00:17:26 -06:00
evazion
dbb66ace90 routes: replace hardcoded routes in models with route helpers.
Add a Routes module that gives models access to route helpers outside of
views, and use it to replace various hardcoded routes.
2020-12-24 00:17:19 -06:00
evazion
a947a10c53 config: add debug_mode option.
Add a debug mode option. This is useful when debugging failed tests.

Debug mode disables parallel testing so you can set breakpoints in tests
with binding.pry (normally parallel testing makes it hard to set
breakpoints).

Debug mode also disables global exception handling for controllers. This
lets exceptions bubble up to the console during controller tests
(normally exceptions are swallowed by the controller, which prevents you
from seeing backtraces in failed controller tests).
2020-12-24 00:17:19 -06:00
evazion
a084da2dbe artists: hide other names of banned artists on index page.
Don't show other names of banned artists on the /artists page to
anonymous users. Hides potentially sensitive information from Google and
logged out users.
2020-12-22 02:15:21 -06:00
evazion
fbb4cfb807 users: let mods see users' last login time. 2020-12-21 22:42:50 -06:00
evazion
3c4781f6d8 users: update last_logged_in_at hourly.
Update last_logged_in_at on an hourly basis instead of a weekly basis.
2020-12-21 22:42:50 -06:00
evazion
025631ee64 users: show IPs to mods on /users page. 2020-12-21 22:42:50 -06:00
evazion
8221c8dcba users: inline search form on /users index page.
* Add the user search form to the /users page.
* Remove the /users/search page.
2020-12-21 22:42:50 -06:00
evazion
7a2f72ce98 ip bans: fix /ip_bans listing not showing subnet. 2020-12-21 22:42:50 -06:00
evazion
db488c247d ip bans: fix deleted field in /ip_bans search form.
Fix the value not being remembered in the search form because we
accidentally used `input_html` instead of `selected`.
2020-12-21 22:42:50 -06:00
evazion
0be9c8dc8b emails: optimize /emails listing.
Fix a suboptimal query that made the /emails page really slow.
2020-12-21 22:42:50 -06:00
evazion
f3880569e1 rails: update settings to 6.1 defaults.
Most of the new settings aren't relevant to us. We do have to fix some
tests to work around a Rails bug. `assert_enqueued_email_with` uses the
wrong queue, so we have to specify it explicitly. This is fixed in Rails
HEAD but not yet released.
2020-12-21 22:42:50 -06:00
evazion
906430b983 config: add option for customizing session cookie name.
Fixes getting logged out when you visited Testbooru because of
Testbooru's session cookies clobbering Danbooru's session cookies.
2020-12-21 22:42:50 -06:00
evazion
6c99bbbf47 posts: limit sources to 1200 chars long.
The longest sources on Danbooru are DeviantArt wixmp.com sources, which
max out at ~900 chars.
2020-12-21 22:42:39 -06:00
evazion
6ac9882711 newrelic: log country of each request in newrelic.
Log the country of each HTTP request in NewRelic. Uses the CF-IPCountry
header set by Cloudflare.
2020-12-21 20:47:58 -06:00
evazion
efb836ac02 wikis: normalize Unicode characters in wiki bodies.
* Introduce an abstraction for normalizing attributes. Very loosely
  modeled after https://github.com/fnando/normalize_attributes.
* Normalize wiki bodies to Unicode NFC form.
* Normalize Unicode space characters in wiki bodies (strip zero width
  spaces, normalize line endings to CRLF, normalize Unicode spaces to
  ASCII spaces).
* Trim spaces from the start and end of wiki page bodies. This may cause
  wiki page diffs to show spaces being removed even when the user didn't
  explicitly remove the spaces themselves.
2020-12-21 20:47:50 -06:00
evazion
48ff7c42cd autocomplete: bump opensearch description version.
Fix browsers still using the old autocomplete endpoint for opensearch
searches (searches performed in the browser toolbar).
2020-12-20 01:33:10 -06:00
evazion
3ad4beac02 autocomplete: fix exception when completing unsupported metatags. 2020-12-20 01:27:48 -06:00
evazion
28926c2332 autocomplete: remove old autocomplete endpoints.
Remove /tag/autocomplete.json and /saved_searches/labels.json.
2020-12-20 00:51:29 -06:00
evazion
9de7a07af7 /status: fix blank distro version field.
The `source` command is a bash-ism and doesn't work in a strictly POSIX
shell like dash, which is the /bin/sh on Debian/Ubuntu. Use `.` instead.

https://en.wikipedia.org/wiki/Dot_(command)
2020-12-20 00:51:29 -06:00
evazion
a129eb4251 wikis: force wiki names to follow same rules as tag names.
Don't allow wiki pages to have invalid names.

This incidentally means that you can't create wiki pages for pools. For
example, you can't create a wiki titled "pool:almost_heart-warming".
This is not a valid tag name, so it's not a valid wiki name either. This
was done in a handful of cases to translate Pixiv tags to Danbooru pools
(see: <https://danbooru.donmai.us/wiki_page_versions?search[title_like]=pool:*>)

Also fix it so that titles are normalized before validation, not before save.
2020-12-20 00:51:29 -06:00
evazion
7708e2e08f wikis: don't allow adding other names to artist wikis.
Prevent users from adding other names to artist wikis. These should be
added to the artist entry instead.
2020-12-20 00:51:29 -06:00
evazion
4cb39422b2 post replacements: rename <attr>_was to old_<attr>
Rename the following post replacement attributes:

* file_size_was -> old_file_size
* file_ext_was -> old_file_ext
* image_width_was -> old_image_width
* image_height_was -> old_image_height
* md5_was -> old_md5

In Rails 6.1, having attributes named `file_size` and `file_size_was` on
the same model breaks things because it conflicts with Rails' dirty
attribute tracking.
2020-12-19 14:26:07 -06:00
evazion
09e3146819 artist finder: add blog.livedoor.jp to blacklist. 2020-12-19 00:51:34 -06:00
evazion
c97186abd7 /status: add missing template files.
Add missing templates that were forgotten in 7a87225ac.
2020-12-19 00:27:24 -06:00
evazion
6849a3d68b Update app files to Rails 6.1 defaults. 2020-12-19 00:26:27 -06:00
evazion
5fc99b9946 Upgrade to Rails 6.1.
* Swap out activerecord-hierarchical_query gem for some guy's patched
  version because the mainline version is incompatible with 6.1.
* Disable meta_request gem because it hangs puma on startup on 6.1.
2020-12-18 02:04:43 -06:00
evazion
53653372ec notes: include search form on search results page.
Also eliminate /notes/search endpoint.
2020-12-18 01:59:39 -06:00
evazion
2c92794eba wiki: include search form on search results page.
Include the search form on the search results page so you can more
easily refine your search.
2020-12-17 23:57:13 -06:00
evazion
25069865b7 ip bans: add search form.
* Add IP ban search form to /ip_bans page.
* Make some attributes searchable that weren't previously searchable.
2020-12-17 23:57:13 -06:00
evazion
3d1ff9dff9 autocomplete: fix not detecting correct tag in edit box. 2020-12-17 23:57:13 -06:00
evazion
2c1da660fd tags: allow tag abbreviations in searches and during tagging.
Expand the tag abbreviation system introduced in b0be8ae45 so that it
works in searches and when tagging posts, not just in autocomplete.

For example, you can tag a post with /evth and it will add the tag
eyebrows_visible_through_hair. You can search for /evth and it will
search for the tag eyebrows_visible_through_hair.

Some more examples:

* /ops is short for one-piece_swimsuit
* /hooe is short for hair_over_one_eye
* /saol is short for standing_on_one_leg
* /tlozbotw is short for the_legend_of_zelda:_breath_of_the_wild

If two tags have the same abbreviation, then the larger tag takes
precedence. For example, /be is short for blue_eyes, not brown_eyes,
because blue_eyes is the bigger tag.

If there is an existing shortcut alias that conflicts with the
abbreviation, then the alias take precedence. For example, /sh is short
for suzumiya_haruhi, not short_hair, because there's an old alias for
/sh -> suzumiya_haruhi.
2020-12-17 23:57:13 -06:00
evazion
991896c4eb tags: don't allow tags more than 170 chars long.
Limit tag length to 170 chars. 170 chars was chosen because it's
longer than the longest active tag on Danbooru.

Tag length is limited because in some contexts we can't deal with
excessively long tags. Tag autocorrect for example uses the levenshtein
function in Postgres, which can't handle strings more than 255 chars long.
2020-12-17 21:38:24 -06:00
evazion
1809f67b2b tags: don't allow tags to begin with a '/'.
Disallow tags from starting with a '/' character. This is so that tag
abbreviations in autocomplete, which start with a '/', don't conflict
with regular tags.

Also disallow some other punctuation characters: `%{})]. Currently no
tags start with these characters. This is to reserve other special
characters in case we need them for other future syntax extensions.
2020-12-17 21:38:18 -06:00
evazion
7a87225ac8 Add basic server status page at /status.
Lists versions of various dependencies plus some Postgres and Redis
metrics.
2020-12-17 03:13:54 -06:00
evazion
ee4516f5fe searchable: refactor searchable_includes.
Pass searchable associations directly to search_attributes instead of
defining them separately in searchable_includes.
2020-12-16 23:57:07 -06:00
evazion
e771c0fca8 searchable: don't automatically include id, created_at, updated_at.
Don't make search methods on models call super in order to search
certain default attributes (id, created_at, updated_at). Simplifies some
magic.
2020-12-16 23:57:07 -06:00
evazion
b0659eb76c searchable: add tests for Searchable concern. 2020-12-16 23:57:04 -06:00
evazion
2297bf5da5 Fix #4638: Add exclusions to the numeric attributes.
Add the following search operators:

* /tags?search[post_count_eq]=42
* /tags?search[post_count_not_eq]=42
* /tags?search[post_count_gt]=42
* /tags?search[post_count_gteq]=42
* /tags?search[post_count_lt]=42
* /tags?search[post_count_lteq]=42

Works for all numeric attributes on all index actions.
2020-12-16 20:03:09 -06:00
evazion
0d83106a21 autocomplete: fix cache issue related to content negotiation.
This is the scenario:

* You type something in autocomplete, let's say 'touhou'.
* Autocomplete calls /autocomplete?search[query]=touhou&search[type]=tag_query
* The endpoint returns JSON, because the autocomplete call sets an
  `Accept: application/json` header requesting JSON.
* Visit /autocomplete?search[query]=touhou&search[type]=tag_query in your browser.
* Notice that the cached JSON response is incorrectly returned, not an
  HTML response like the browser requested.

The problem is that the response type is chosen based on the Accept
header, but the response didn't set the `Vary: Accept` header, so the
browser doesn't know the response type can vary and so it incorrectly
returns the cached response.

This issue is partially fixed by Rails 6.1 ([1]), which properly sets the
`Vary: Accept` header when the response depends on the Accept header.

However, the next issue is that Cloudflare doesn't respect the Vary
header at all ([2], [3]). Therefore we can't use the Accept header to
pick the format, instead we have explicitly specify the format with
/autocomplete.json.

This is clearer and better for caching anyway. Using the `Vary: Accept`
header reduces the cache hit rate, because the exact format of the
Accept header varies across browsers, which fragments the cache.

Whew.

[1] https://github.com/rails/rails/pull.36213
[2] https://community.cloudflare.com/t/cloudflare-cdn-cache-to-support-http-vary-header/160802
[3] https://support.cloudflare.com/hc/en-us/articles/115003206852
[4] https://www.smashingmagazine.com/2017/11/understanding-vary-header/
2020-12-16 03:13:55 -06:00
evazion
c836c93b81 autocomplete: don't send cookies in publicly cached responses.
Fix session cookies being sent in publicly cached /autocomplete.json
responses. We can't set any cookies in a response that is being publicly
cached, otherwise they'll be visible to other users. If a user's session
cookies were to be cached, then it would allow their account to be stolen.

In reality, well-behaved caches like Cloudflare will simply refuse to
cache responses that contain cookies to avoid this scenario.

https://support.cloudflare.com/hc/en-us/articles/200172516-Understanding-Cloudflare-s-CDN:

    BYPASS is returned when enabling Origin Cache-Control. Cloudflare also
    sets BYPASS when your origin web server sends cookies in the response
    header.
2020-12-15 03:48:59 -06:00
evazion
26246b0ac9 autocomplete: fix exception when typing "/" in autocomplete.
Fix an exception that could occur when typing "/" by itself in
autocomplete and a regular tag starting with "/" was returned. This
caused an exception in `r[:antecedent].length` because the tag's
antecedent was nil.
2020-12-14 21:57:28 -06:00
evazion
4cdaf7bcdf autocomplete: update html data attributes.
* Remove the `source` and `weight` html data attributes (no longer used).
* Make the `type` html data attribute properly indicate the completion
  type. Valid types: `tag`, `tag-alias`, `tag-abbreviation`,
  `tag-autocorrect`, `tag-other-name`.
2020-12-14 18:58:11 -06:00
evazion
c02c31b966 autocomplete: recognize Japanese tags in autocomplete.
Allowing typing Japanese tags in autocomplete. For example, typing 東方
in autocomplete will be completed to the touhou tag. Typing ぶくぶ will
complete to the bkub tag.

This works using wiki page and artist other names. Effectively, any name
listed as an other name in a wiki or artist page will be treated like an
alias for autocomplete purposes. This is limited to non-ASCII other names,
to prevent English other names from interfering with regular tag searches.
2020-12-14 18:58:11 -06:00
evazion
23f6b8a46d js: refactor Cookie.put.
* Set Max-Age= flag instead of Expires= flag.
* Set Secure flag when using HTTPS.
* Extend default cookie lifetime from 1 year to 20 years.
* Remove "session" expiration option (unused).
* Remove max cookie size check.

The cookie size check was previously added in #2518 to deal with running
out of space due to tag scripts and blacklists. This should no longer
happen since we no longer use cookies for these things. Remove the
warning because it should never happen, we can't fix it if it does, and
the user probably won't know how to fix it either.
2020-12-14 15:39:17 -06:00
evazion
df1404b673 js: set SameSite=Lax on cookies set by Javascript.
This is the new default for most browsers nowadays. Fixes a warning in
Firefox about using SameSite=None without the Secure flag.
2020-12-14 15:37:58 -06:00
evazion
0150911343 css: remove missing --dtext-expand-border-color var. 2020-12-14 14:29:31 -06:00