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

Return json on 500 error from API #11574

Merged
merged 10 commits into from
May 28, 2020

Conversation

6543
Copy link
Member

@6543 6543 commented May 23, 2020

if ctx.InternalServerError(err) is used it will return the default 500 html page

on API a json Error is expected

@6543
Copy link
Member Author

6543 commented May 23, 2020

just noticed this behaviour - it was not documented at all - of coures if you do a code lookup you can see it too :)

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 23, 2020
@techknowlogick techknowlogick added this to the 1.13.0 milestone May 23, 2020
@6543
Copy link
Member Author

6543 commented May 23, 2020

@techknowlogick can we backport this?

@techknowlogick
Copy link
Member

whoops. label didn't apply at first. It's there now:)

Copy link
Member Author

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better error logging

routers/api/v1/misc/markdown.go Outdated Show resolved Hide resolved
routers/api/v1/misc/markdown.go Outdated Show resolved Hide resolved
routers/api/v1/misc/markdown.go Outdated Show resolved Hide resolved
routers/api/v1/misc/markdown.go Outdated Show resolved Hide resolved
routers/api/v1/notify/repo.go Outdated Show resolved Hide resolved
routers/api/v1/repo/topic.go Outdated Show resolved Hide resolved
routers/api/v1/repo/transfer.go Outdated Show resolved Hide resolved
routers/api/v1/repo/transfer.go Outdated Show resolved Hide resolved
routers/api/v1/repo/transfer.go Outdated Show resolved Hide resolved
routers/api/v1/user/app.go Outdated Show resolved Hide resolved
@silverwind
Copy link
Member

Bit of a shame that this needs to be done in every route. The way I'd handle this in JS routing is to let errors throw in the routes and then process them in a "catch-all" route but I guess with missing exceptions, this is not possible in Golang.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels May 24, 2020
@zeripath
Copy link
Contributor

The other option is for the API context to provide its own internalservererror function

@6543
Copy link
Member Author

6543 commented May 24, 2020

we should keep it simple ...

@zeripath
Copy link
Contributor

zeripath commented May 24, 2020

It would be simpler...

in modules/context/api.go add the following function:

// InternalServerError responds with an error message to the client with the error as a message and the file and line of the caller.
func (ctx *APIContext) InternalServerError(err error) {
	log.ErrorWithSkip(1, "InternalServerError: %v", err)

	ctx.JSON(http.StatusInternalServerError, APIError{
		Message: err.Error(),
		URL:     setting.API.SwaggerURL,
	})
}

And whilst you're at it change the function above it to:

// Error responds with an error message to client with given obj as the message.
// If status is 500, also it prints error to log.
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
	var message string
	if err, ok := obj.(error); ok {
		message = err.Error()
	} else {
		message = obj.(string)
	}

	if status == http.StatusInternalServerError {
		log.ErrorWithSkip(1, "%s: %s", title, message)
	}

	ctx.JSON(status, APIError{
		Message: message,
		URL:     setting.API.SwaggerURL,
	})
}

modules/context/api.go Outdated Show resolved Hide resolved
Copy link
Contributor

@zeripath zeripath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much simpler

Co-authored-by: zeripath <[email protected]>
Copy link
Member

@lafriks lafriks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But won't this reveal internal error messages in api? That would be bad from security point of view

@silverwind
Copy link
Member

What secrets would they reveal? Does it matter in an open-source application?

@6543
Copy link
Member Author

6543 commented May 24, 2020

But won't this reveal internal error messages in api? That would be bad from security point of view

@lafriks
they already do it now!!
just in html output and now in json

@lafriks
Copy link
Member

lafriks commented May 24, 2020

I don't think actual error message is returned currently, if it is it should be fixed not to be. Even returning things like paths where Gitea is installed can be dangerous as it could give out information about where files are located. Also knowing what exact unexpected error is happening is so much easier to use it.
This does not matter if app is open source or not.
More info: https://cwe.mitre.org/data/definitions/209.html

@zeripath
Copy link
Contributor

@lafriks the actual error message is returned currently - but yeah we shouldn't do that.
@6543 add a check like:

if macaron.Env != macaron.PROD {

Before setting the message.

@6543
Copy link
Member Author

6543 commented May 26, 2020

@lafriks I think to hide 500 errors on API will need its own pull to correct 500 errors witch should have other http status codes ...

@6543
Copy link
Member Author

6543 commented May 27, 2020

@zeripath #11641 (comment) and 497f226

Copy link
Contributor

@zeripath zeripath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shame about the api.Error endpoint - but if you don't have time to fix those right now - at least we can get a fix in for the ostensible reason for this PR.

These changes now at least don't make the situation worse.

@6543
Copy link
Member Author

6543 commented May 27, 2020

should we merge 🚀?

or wait for #11643

EDIT: #11644 is kind/breaking in my opinion and so #11641 should not be backported

@guillep2k guillep2k changed the title API return json on 500 error Return json on 500 error from API May 28, 2020
@guillep2k guillep2k added the modifies/api This PR adds API routes or modifies them label May 28, 2020
Copy link
Member

@lafriks lafriks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where InternalServerError function is used anymore :)

@6543
Copy link
Member Author

6543 commented May 28, 2020

?

@zeripath
Copy link
Contributor

This PR provides a modules/context.APIContext.InternalServerError(...) method override for a method in modules/context.Context.

An example of a piece of code affected is in routers/api/v1/misc/markdown.go:79:ctx.InternalServerError(err)

Where ctx is actually a modules/context.APIContext and modules/context.APIContext is:

// APIContext is a specific macaron context for API service
type APIContext struct {
	*Context
	Org *APIOrganization
}

As there was previously no InternalServerError method defined for APIContext then the one used for Context was used. This PR stops that by providing a method specific for these to APIContext.

@lafriks
Copy link
Member

lafriks commented May 28, 2020

sorry my bad :)

@6543
Copy link
Member Author

6543 commented May 28, 2020

I would say ready to merge

@guillep2k guillep2k merged commit 9f55769 into go-gitea:master May 28, 2020
@guillep2k
Copy link
Member

Please send backport. Can you check whether the "Allow maintainers to edit this post" checkbox shows and is checked up before posting? It's an ongoing mistery.... 🕵️

@6543 6543 deleted the api_return-json-error branch May 28, 2020 17:10
@6543
Copy link
Member Author

6543 commented May 28, 2020

I agree It's an ongoing mistery!

6543 added a commit to 6543-forks/gitea that referenced this pull request May 28, 2020
* add API specific InternalServerError()

Co-authored-by: zeripath <[email protected]>

* return 500 error msg only if not Production mode

* Revert "return 500 error msg only if not Production mode"

This reverts commit 8467b2c.

* InternalServerError

Co-authored-by: zeripath <[email protected]>
6543 added a commit to 6543-forks/gitea that referenced this pull request May 28, 2020
* add API specific InternalServerError()

Co-authored-by: zeripath <[email protected]>

* return 500 error msg only if not Production mode

* Revert "return 500 error msg only if not Production mode"

This reverts commit 8467b2c.

* InternalServerError

Co-authored-by: zeripath <[email protected]>
@6543
Copy link
Member Author

6543 commented May 28, 2020

have created backport to v1.11 too - old untuched code allowed it easely ;)

@zeripath zeripath added backport/done All backports for this PR have been created backport/v1.11 labels May 28, 2020
zeripath pushed a commit that referenced this pull request May 28, 2020
Backport #11574

add API specific InternalServerError()

InternalServerError
zeripath pushed a commit that referenced this pull request May 28, 2020
Backport #11574

add API specific InternalServerError()

InternalServerError
ydelafollye pushed a commit to ydelafollye/gitea that referenced this pull request Jul 31, 2020
* add API specific InternalServerError()

Co-authored-by: zeripath <[email protected]>

* return 500 error msg only if not Production mode

* Revert "return 500 error msg only if not Production mode"

This reverts commit 8467b2c.

* InternalServerError

Co-authored-by: zeripath <[email protected]>
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants