| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | package util | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-11-13 19:48:51 +01:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"codeberg.org/gruf/go-kv" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2023-04-28 16:45:21 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtscontext" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO: add more templated html pages here for different error types | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NotFoundHandler serves a 404 html page through the provided gin context, | 
					
						
							|  |  |  | // if accept is 'text/html', or just returns a json error if 'accept' is empty | 
					
						
							|  |  |  | // or application/json. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // When serving html, NotFoundHandler calls the provided InstanceGet function | 
					
						
							|  |  |  | // to fetch the apimodel representation of the instance, for serving in the | 
					
						
							|  |  |  | // 404 header and footer. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If an error is returned by InstanceGet, the function will panic. | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | func NotFoundHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string, errWithCode gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	switch accept { | 
					
						
							|  |  |  | 	case string(TextHTML): | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		ctx := c.Request.Context() | 
					
						
							|  |  |  | 		instance, err := instanceGet(ctx) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		template404Page(c, | 
					
						
							|  |  |  | 			instance, | 
					
						
							|  |  |  | 			gtscontext.RequestID(ctx), | 
					
						
							|  |  |  | 		) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2023-11-27 14:00:57 +00:00
										 |  |  | 		JSON(c, http.StatusNotFound, map[string]string{ | 
					
						
							|  |  |  | 			"error": errWithCode.Safe(), | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // genericErrorHandler is a more general version of the NotFoundHandler, which can | 
					
						
							|  |  |  | // be used for serving either generic error pages with some rendered help text, | 
					
						
							|  |  |  | // or just some error json if the caller prefers (or has no preference). | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), accept string, errWithCode gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	switch accept { | 
					
						
							|  |  |  | 	case string(TextHTML): | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		ctx := c.Request.Context() | 
					
						
							|  |  |  | 		instance, err := instanceGet(ctx) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		templateErrorPage(c, | 
					
						
							|  |  |  | 			instance, | 
					
						
							|  |  |  | 			errWithCode.Code(), | 
					
						
							|  |  |  | 			errWithCode.Safe(), | 
					
						
							|  |  |  | 			gtscontext.RequestID(ctx), | 
					
						
							|  |  |  | 		) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2023-11-27 14:00:57 +00:00
										 |  |  | 		JSON(c, errWithCode.Code(), map[string]string{ | 
					
						
							|  |  |  | 			"error": errWithCode.Safe(), | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | // ErrorHandler takes the provided gin context and errWithCode | 
					
						
							|  |  |  | // and tries to serve a helpful error to the caller. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // It will do content negotiation to figure out if the caller prefers | 
					
						
							|  |  |  | // to see an html page with the error rendered there. If not, or if | 
					
						
							|  |  |  | // something goes wrong during the function, it will recover and just | 
					
						
							|  |  |  | // try to serve an appropriate application/json content-type error. | 
					
						
							| 
									
										
										
										
											2023-05-12 08:16:41 +00:00
										 |  |  | // To override the default response type, specify `offers`. | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2023-11-13 19:48:51 +01:00
										 |  |  | // If the requester already hung up on the request, or the server | 
					
						
							|  |  |  | // timed out a very slow request, ErrorHandler will overwrite the | 
					
						
							|  |  |  | // given errWithCode with a 408 or 499 error to indicate that the | 
					
						
							|  |  |  | // failure wasn't due to something we did, and will avoid trying | 
					
						
							|  |  |  | // to write extensive bytes to the caller by just aborting. | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2023-11-13 19:48:51 +01:00
										 |  |  | // For 499, see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#nginx. | 
					
						
							|  |  |  | func ErrorHandler( | 
					
						
							|  |  |  | 	c *gin.Context, | 
					
						
							|  |  |  | 	errWithCode gtserror.WithCode, | 
					
						
							|  |  |  | 	instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode), | 
					
						
							| 
									
										
										
										
											2023-11-27 14:00:57 +00:00
										 |  |  | 	offers ...string, | 
					
						
							| 
									
										
										
										
											2023-11-13 19:48:51 +01:00
										 |  |  | ) { | 
					
						
							|  |  |  | 	if ctxErr := c.Request.Context().Err(); ctxErr != nil { | 
					
						
							|  |  |  | 		// Context error means either client has left already, | 
					
						
							|  |  |  | 		// or server has timed out a very slow request. | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// Rewrap the error with something less scary, | 
					
						
							|  |  |  | 		// and just abort the request gracelessly. | 
					
						
							|  |  |  | 		err := errWithCode.Unwrap() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if errors.Is(ctxErr, context.DeadlineExceeded) { | 
					
						
							|  |  |  | 			// We timed out the request. | 
					
						
							|  |  |  | 			errWithCode = gtserror.NewErrorRequestTimeout(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Be correct and write "close". | 
					
						
							|  |  |  | 			// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#close | 
					
						
							|  |  |  | 			// and: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 | 
					
						
							|  |  |  | 			c.Header("Connection", "close") | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// Client timed out the request. | 
					
						
							|  |  |  | 			errWithCode = gtserror.NewErrorClientClosedRequest(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | 		c.AbortWithStatus(errWithCode.Code()) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Set the error on the gin context so that it can be logged | 
					
						
							|  |  |  | 	// in the gin logger middleware (internal/middleware/logger.go). | 
					
						
							| 
									
										
										
										
											2022-09-17 19:12:12 +02:00
										 |  |  | 	c.Error(errWithCode) //nolint:errcheck | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | 	// Discover if we're allowed to serve a nice html error page, | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	// or if we should just use a json. Normally we would want to | 
					
						
							|  |  |  | 	// check for a returned error, but if an error occurs here we | 
					
						
							|  |  |  | 	// can just fall back to default behavior (serve json error). | 
					
						
							| 
									
										
										
										
											2023-06-03 13:58:57 +02:00
										 |  |  | 	// Prefer provided offers, fall back to JSON or HTML. | 
					
						
							|  |  |  | 	accept, _ := NegotiateAccept(c, append(offers, JSONOrHTMLAcceptHeaders...)...) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 08:32:20 +01:00
										 |  |  | 	if errWithCode.Code() == http.StatusNotFound { | 
					
						
							| 
									
										
										
										
											2023-06-02 15:19:43 +02:00
										 |  |  | 		// Use our special not found handler with useful status text. | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | 		NotFoundHandler(c, instanceGet, accept, errWithCode) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		genericErrorHandler(c, instanceGet, accept, errWithCode) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-11 10:39:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-12 08:16:41 +00:00
										 |  |  | // WebErrorHandler is like ErrorHandler, but will display HTML over JSON by default. | 
					
						
							|  |  |  | func WebErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context) (*apimodel.InstanceV1, gtserror.WithCode)) { | 
					
						
							|  |  |  | 	ErrorHandler(c, errWithCode, instanceGet, TextHTML, AppJSON) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-11 10:39:39 +02:00
										 |  |  | // OAuthErrorHandler is a lot like ErrorHandler, but it specifically returns errors | 
					
						
							|  |  |  | // that are compatible with https://datatracker.ietf.org/doc/html/rfc6749#section-5.2, | 
					
						
							|  |  |  | // but serializing errWithCode.Error() in the 'error' field, and putting any help text | 
					
						
							|  |  |  | // from the error in the 'error_description' field. This means you should be careful not | 
					
						
							|  |  |  | // to pass any detailed errors (that might contain sensitive information) into the | 
					
						
							|  |  |  | // errWithCode.Error() field, since the client will see this. Use your noggin! | 
					
						
							|  |  |  | func OAuthErrorHandler(c *gin.Context, errWithCode gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 	l := log.WithContext(c.Request.Context()). | 
					
						
							|  |  |  | 		WithFields(kv.Fields{ | 
					
						
							|  |  |  | 			{"path", c.Request.URL.Path}, | 
					
						
							|  |  |  | 			{"error", errWithCode.Error()}, | 
					
						
							|  |  |  | 			{"help", errWithCode.Safe()}, | 
					
						
							|  |  |  | 		}...) | 
					
						
							| 
									
										
										
										
											2022-06-11 10:39:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	statusCode := errWithCode.Code() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if statusCode == http.StatusInternalServerError { | 
					
						
							|  |  |  | 		l.Error("Internal Server Error") | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		l.Debug("handling OAuth error") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 14:00:57 +00:00
										 |  |  | 	JSON(c, statusCode, map[string]string{ | 
					
						
							| 
									
										
										
										
											2022-06-11 10:39:39 +02:00
										 |  |  | 		"error":             errWithCode.Error(), | 
					
						
							|  |  |  | 		"error_description": errWithCode.Safe(), | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-13 13:53:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // NotFoundAfterMove returns code 404 to the caller and writes a helpful error message. | 
					
						
							|  |  |  | // Specifically used for accounts trying to access endpoints they cannot use while moving. | 
					
						
							|  |  |  | func NotFoundAfterMove(c *gin.Context) { | 
					
						
							|  |  |  | 	const errMsg = "your account has Moved or is currently Moving; you cannot use this endpoint" | 
					
						
							|  |  |  | 	JSON(c, http.StatusForbidden, map[string]string{ | 
					
						
							|  |  |  | 		"error": errMsg, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ForbiddenAfterMove returns code 403 to the caller and writes a helpful error message. | 
					
						
							|  |  |  | // Specifically used for accounts trying to take actions on endpoints they cannot do while moving. | 
					
						
							|  |  |  | func ForbiddenAfterMove(c *gin.Context) { | 
					
						
							|  |  |  | 	const errMsg = "your account has Moved or is currently Moving; you cannot take create or update type actions" | 
					
						
							|  |  |  | 	JSON(c, http.StatusForbidden, map[string]string{ | 
					
						
							|  |  |  | 		"error": errMsg, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |