Commit Graph

118 Commits

Author SHA1 Message Date
evazion
b43a913ad7 users: delete more data when user deactivates their account.
* Don't delete the user's favorites unless private favorites are enabled. The general rule is that
  public account activity is kept and private account activity is deleted.
* Delete the user's API keys, forum topics visits, private favgroups, downvotes, and upvotes (if
  privacy is enabled).
* Reset all of the user's account settings to default. This means custom CSS is deleted, where it
  wasn't before.
* Delete everything but the user's name and password asynchronously.
* Don't log the current user out if it's the owner deleting another user's account.
* Fix #5067 (Mod actions sometimes not created for user deletions) by wrapping the deletion process
  in a transaction.
2022-11-06 00:05:18 -05:00
evazion
3ffde5b23d users: move account deletion endpoint to /users/:id/deactivate.
Move the account deletion endpoint from /maintenance/users/deletion to either:

* https://danbooru.donmai.us/users/deactivate
* https://danbooru.donmai.us/users/:id/deactivate

This incidentally allows the Owner-level user to deactivate accounts belonging to other users. This
is meant for things like deactivating inactive accounts with invalid or abusive names. This is
limited to accounts below Gold level for security.
2022-11-05 19:09:56 -05:00
evazion
21a3763f0f users: set archives DB timeout on profile pages.
Fixes profile pages potentially hanging for 60+ seconds if the archives
database is slow to respond.
2022-11-05 19:09:56 -05:00
evazion
b94cb7d824 emails: include logging information in email headers.
Log the following information in email headers:

* X-Danbooru-User: the user's name and ID.
* X-Danbooru-IP: the user's IP.
* X-Danbooru-Session: the users' session ID.
* X-Danbooru-URL: the page that triggered the email.
* X-Danbooru-Job-Id: the ID of the background job that sent the email.
* X-Danbooru-Enqueued-At: when the email was queued as a background job.
* X-Danbooru-Dmail: for Dmail notifications, the link to the Dmail.
* X-Request-Id: the request ID of the HTTP request that triggered the email.

Also make it so we log an event in the APM when we send an email.
2022-09-29 04:36:11 -05:00
evazion
1d2bac7b95 Remove CurrentUser.ip_addr.
Remove the `CurrentUser.ip_addr` global variable and replace it with
`request.remote_ip`. Before we had to track the current user's IP in a
global variable so that when we edited a post for example, we could pass
down the user's IP to the model and save it in the post_versions table.
Now that we now longer save IPs in version tables, we don't need a global
variable to get access to the current user's IP outside of controllers.
2022-09-18 05:02:10 -05:00
evazion
d9cf7f7443 users: allow anonymous users to use /profile.json.
Allow logged out users to call https://danbooru.donmai.us/profile.json.
This allows getting information on default settings and limits for
anonymous users.

May be a breaking API change if users were using the HTTP response code
from /profile.json to check if they were successfully logged in.
2022-08-24 02:24:05 -05:00
evazion
acf565be7b Fix #4678: Validate custom CSS.
* Make it an error to add invalid custom CSS to your account.
* Add a fix script to remove custom CSS from all accounts with invalid CSS.
2022-01-15 23:20:49 -06:00
evazion
32613f9bb1 emails: fix sending emails to invalid addresses.
Fix mailers to not attempt deliveries to invalid or nonexistent email
addresses. This usually happened when someone changed their email, and
we tried to send a confirmation email to a nonexistent address.
2022-01-02 16:07:57 -06:00
evazion
a7dc05ce63 Enable frozen string literals.
Make all string literals immutable by default.
2021-12-14 21:33:27 -06:00
evazion
2e9f4dc2f4 controllers: refactor rate limits.
Refactor controllers so that endpoint rate limits are declared locally,
with the endpoint, instead of globally, in a single method in ApplicationController.

This way an endpoint's rate limit is declared in the same file as the
endpoint itself.

This is so we can add fine-grained rate limits for certain GET requests.
Before rate limits were only for non-GET requests.
2021-12-10 01:46:01 -06:00
evazion
f93b1fe478 custom css: don't add !important to every line.
Fixes #4398.
2021-03-16 20:04:09 -05:00
evazion
413cd34c45 rate limits: adjust limits for various actions.
* Tie rate limits to both the user's ID and their IP address.

* Make each endpoint have separate rate limits. This means that, for
  example, your post edit rate limit is separate from your post vote
  rate limit. Before all write actions had a shared rate limit.

* Make all write endpoints have rate limits. Before some endpoints, such
  as voting, favoriting, commenting, or forum posting, weren't subject
  to rate limits.

* Add stricter rate limits for some endpoints:

** 1 per 5 minutes for creating new accounts.
** 1 per minute for login attempts, changing your email address, or
   for creating mod reports.
** 1 per minute for sending dmails, creating comments, creating forum
   posts, or creating forum topics.
** 1 per second for voting, favoriting, or disapproving posts.
** These rate limits all have burst factors high enough that they
   shouldn't affect normal, non-automated users.

* Raise the default write rate limit for Gold users from 2 per second to
  4 per second, for all other actions not listed above.

* Raise the default burst factor to 200 for all other actions not listed
  above. Before it was 10 for Members, 30 for Gold, and 60 for Platinum.
2021-03-05 16:02:57 -06:00
evazion
4719a5ed1c users: set default settings in ruby instead of in database.
Specify the default settings for new users inside the User model instead
of inside the database. This makes it easier to change defaults, and it
makes the code clearer.
2021-01-15 02:03:54 -06:00
evazion
c05868e7f1 users: log restricted signups to NewRelic.
When a new user creates an account and their account is automatically
restricted, log the reason why to NewRelic.
2021-01-12 04:25:11 -06:00
evazion
65adcd09c2 users: track logins, signups, and other user events.
Add tracking of certain important user actions. These events include:

* Logins
* Logouts
* Failed login attempts
* Account creations
* Account deletions
* Password reset requests
* Password changes
* Email address changes

This is similar to the mod actions log, except for account activity
related to a single user.

The information tracked includes the user, the event type (login,
logout, etc), the timestamp, the user's IP address, IP geolocation
information, the user's browser user agent, and the user's session ID
from their session cookie. This information is visible to mods only.

This is done with three models. The UserEvent model tracks the event
type (login, logout, password change, etc) and the user. The UserEvent
is tied to a UserSession, which contains the user's IP address and
browser metadata. Finally, the IpGeolocation model contains the
geolocation information for IPs, including the city, country, ISP, and
whether the IP is a proxy.

This tracking will be used for a few purposes:

* Letting users view their account history, to detect things like logins
  from unrecognized IPs, failed logins attempts, password changes, etc.
* Rate limiting failed login attempts.
* Detecting sockpuppet accounts using their login history.
* Detecting unauthorized account sharing.
2021-01-08 22:34:37 -06:00
evazion
94e125709c users: add Restricted user level.
Add a Restricted user level. Restricted users are level 10, below
Members. New users start out as Restricted if they sign up from a proxy
or an IP recently used by another user.

Restricted users can't update or edit any public content on the site
until they verify their email address, at which point they're promoted
to Member. Restricted users are only allowed to do personal actions
like keep favorites, keep favgroups and saved searches, mark dmails as
read or deleted, or mark forum posts as read.

The restricted state already existed before, the only change here is
that now it's an actual user level instead of a hidden state. Before it
was based on two hidden flags on the user, the `requires_verification`
flag (set when a user signs up from a proxy, etc), and the `is_verified`
flag (set after the user verifies their email). Making it a user level
means that now the Restricted status will be shown publicly.

Introducing a new level below Member means that we have to change every
`is_member?` check to `!is_anonymous` for every place where we used
`is_member?` to check that the current user is logged in.
2021-01-07 17:10:29 -06:00
evazion
9dc788c0ce users: improve sockpuppet detection on signup.
Require new accounts to verify their email address if any of the
following conditions are true:

* Their IP is a proxy.
* Their IP is under a partial IP ban.
* They're creating a new account while logged in to another account.
* Somebody recently created an account from the same IP in the last week.

Changes from before:

* Allow logged in users to view the signup page and create new accounts.
  Creating a new account while logged in to your old account is now
  allowed, but it requires email verification. This is a honeypot.
* Creating multiple accounts from the same IP is now allowed, but they
  require email verification. Previously the same IP check was only for
  the last day (now it's the last week), and only for an exact IP match
  (now it's a subnet match, /24 for IPv4 or /64 for IPv6).
* New account verification is disabled for private IPs (e.g. 127.0.0.1,
  192.168.0.1), to make development or running personal boorus easier
  (fixes #4618).
2020-12-27 23:41:07 -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
8d87b1a0c0 models: fix deprecated errors[:base] << "message" calls.
Replace the idiom `errors[:base] << "message"` with
`errors.add(:base, "message")`. The former is deprecated in Rails 6.1.
2020-12-13 04:10:48 -06:00
evazion
b9dda5bd21 users: fix /users?name=<username> being case sensitive.
Things like https://danbooru.donmai.us/users?name=Evazion didn't work.
2020-12-01 19:18:39 -06:00
evazion
88bbd1e3f0 users: add username tooltips. 2020-07-13 17:09:03 -05:00
evazion
16011771f1 ip bans: rename ban types to full and partial.
Rename ban types from "normal" and "signup" to "full" and "partial".
2020-04-07 14:59:46 -05:00
evazion
b2ee1f0766 ip bans: add hit counter, deleted flag, new ban type.
* Make IP bans soft deletable.
* Add a hit counter to track how many times an IP ban has blocked someone.
* Add a last hit timestamp to track when the IP ban last blocked someone.
* Add a new type of IP ban, the signup ban. Signup bans restrict new
  signups from editing anything until they've verified their email
  address.
2020-04-06 14:13:22 -05:00
evazion
b7bd6c8fdd users: require email verification for signups from proxies.
Require users who signup using proxies to verify their email addresses
before they can perform any edits. For verification purposes, the email
must be a nondisposable address from a whitelist of trusted email
providers.
2020-03-24 17:25:35 -05:00
evazion
4a5bec71f6 signup: don't send welcome emails to invalid addresses.
Fix attempting to send welcome emails when user didn't provide a valid
email address.
2020-03-24 00:26:47 -05:00
evazion
e79910431f emails: validate that email addresses are deliverable.
Reject email addresses that known to be undeliverable during signup.
Some users signup with invalid email addresses, which causes the welcome
email (which contains the email confirmation link) to bounce. Too many
bounces hurt our ability to send mail.

We check that an email address is undeliverable by checking if the
domain has a mail server and if the server returns an invalid address
error when attempting to send mail. This isn't foolproof since some
servers don't return an error if the address doesn't exist. If the
checks fail we know the address is bad, but if the checks pass that
doesn't guarantee the address is good. However, this is still good
enough to filter out bad addresses for popular providers like Gmail and
Microsoft that do return nonexistent address errors.

The address existence check requires being able to connect to mail
servers over port 25. This may fail if your network blocks port 25,
which many home ISPs and hosting providers do by default.
2020-03-23 23:45:18 -05:00
evazion
5c6d26ea24 pundit: convert users to pundit. 2020-03-20 18:03:00 -05:00
evazion
0ef9d6e417 emails: send welcome mail on signup. 2020-03-14 19:18:28 -05:00
evazion
258f4a8b95 users: move emails to separate table.
* Move emails from users table to email_addresses table.
* Validate that addresses are formatted correctly and are unique across
  users. Existing invalid emails are grandfathered in.
* Add is_verified flag (the address has been confirmed by the user).
* Add is_deliverable flag (an undeliverable address is an address that bounces).
* Normalize addresses to prevent registering multiple accounts with the
  same email address (using tricks like Gmail's plus addressing).
2020-03-12 21:18:53 -05:00
evazion
39ad0d2210 settings: rename "disable responsive mode" to "enable desktop mode"
Desktop mode is a better description. Most users don't know what responsive mode means.
2020-03-06 19:37:11 -06:00
evazion
e47d0e0d05 models: set more creator names explicitly.
Set creators explicitly for bans, BURs, comment votes, and posts.
2020-02-23 17:26:08 -06:00
evazion
2564e885c8 controllers: refactor only param includes.
Add extra includes needed by the `only` param inside `respond_with`.
2020-02-15 06:17:22 -06:00
BrokenEagle
63b3503bfc Add ability to use nested only parameter
- The only string works much the same as before with its comma separation
-- Nested includes are indicated with square brackets "[ ]"
-- The nested include is the value immediately preceding the square brackets
-- The only string is the comma separated string inside those brackets
- Default includes are split between format types when necessary
-- This prevents unnecessary includes from being added on page load
- Available includes are those items which are allowed to be accessible to the user
-- Some aren't because they are sensitive, such as the creator of a flag
-- Some aren't because the number of associated items is too large
- The amount of times the same model can be included to prevent recursions
-- One exception is the root model may include the same model once
--- e.g. the user model can include the inviter which is also the user model
-- Another exception is if the include is a has_many association
--- e.g. artist urls can include the artist, and then artist urls again
2020-02-12 23:58:53 +00:00
evazion
c7185724d5 controllers: set @current_item globally in respond_with. 2020-01-31 02:43:08 -06:00
evazion
fbe39148cd users: remove opt_out_tracking account setting.
This setting doesn't do anything any more. It was only ever used to
disable tracking of some (but not all) data in Mixpanel, then later in
Matomo.
2020-01-26 19:16:38 -06:00
BrokenEagle
75ac11166c Added universal redirect on the index action
- Only controllers with show actions will redirect on the index action
- Parameter checking is individualized per controller for the redirect check
2020-01-25 20:48:01 +00:00
evazion
cae9a5d7e3 Drop dmail filters.
Few people used dmail filters (~900 users in 5 years) and even fewer
used them correctly. Most people used them to try to block dmail spam,
but usually they either blocked too much (by adding common words that
are present in nearly all dmails, causing all mails to them to be
filtered) or too little (blocking specific email addresses or urls,
which usually are never seen again after the spammer is banned).
Nowadays the spam detection system does a better job of filtering spam.
2020-01-21 00:10:20 -06:00
evazion
4a7322b197 users: rework privacy mode into private favorites (fix #4257).
* Rename 'privacy mode' to 'private favorites'.
* Make the private favorites setting only hide favorites, not favgroups
  and not the user's uploads on their profile page.
* Make the favgroup is_public flag default to true instead of false and
  fix existing favgroups to be public if the user didn't have privacy mode
  enabled before.
* List _all_ public favgroups on the /favorite_groups index, not just
  favgroups belonging to the current user.
* Add a /users/<id>/favorite_groups endpoint.
2020-01-17 22:24:29 -06:00
BrokenEagle
223a6df5d8 Add API data to show/index views 2020-01-04 22:02:44 +00:00
evazion
309821bf73 rubocop: fix various style issues. 2019-12-22 21:23:37 -06:00
evazion
48e0569832 login: add /login & /logout routes, rename 'sign in' to 'login'.
* Replace /session/new with /login and /session/sign_out with /logout.
* Rename 'sign in' to 'login'.

This changes are to make urls cleaner and terminology more consistent.
2019-12-14 15:27:13 -06:00
evazion
6e5e07022d Have single result searches go to the show pages for certain controllers #4204 2019-11-04 14:24:10 -06:00
evazion
a5ab25d0ba pagination: avoid counting pages outside searches.
Replace this common pattern in controllers:

    @tags = Tag.search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])

with this:

    @tags = Tag.paginated_search(params)

`search_count` is used to skip doing a full page count when we're not
doing a search (on the assumption that the number of results will be
high when not constrained by a search). We didn't do this consistently
though. Refactor to do this in every controller.
2019-10-07 22:02:03 -05:00
evazion
bf51d68f15 users: add dark mode account setting (fix #4158). 2019-09-19 13:20:06 -05:00
evazion
22fd90eee9 api: support the expiry param on all GET requests. 2019-09-08 15:32:31 -05:00
evazion
3f7e05316d api: refactor default options for xml responses.
In xml responses, if the result is an empty array we want the response
to look like this:

   <posts type="array"/>

not like this (the default):

   <nil-classes type="array"/>

This refactors controllers so that this is done automatically instead of
having to manually call `@things.to_xml(root: "things")` everywhere. We
do this by overriding the behavior of `respond_with` in `ApplicationResponder`
to set the `root` option by default in xml responses.
2019-09-08 15:32:31 -05:00
evazion
4fb24c812f Fix #4155: Fix profile/settings metadata discrepancy. 2019-09-05 00:14:11 -05:00
evazion
95b8420ba2 Drop unused common_searches code. 2019-09-01 15:54:00 -05:00
evazion
f673552743 users: add /settings page.
Add /settings page and link to it instead of /users/:id/edit.
2019-09-01 15:00:27 -05:00
evazion
ff92b32f02 users: add /profile page (fix #4151).
* Add /profile, /profile.json endpoints.
* Make "My Account" link to /profile.
* Add 'User ID' field to profile page.
2019-09-01 15:00:23 -05:00