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

Prevent NPE when cache service is disabled #19703

Merged
merged 2 commits into from May 21, 2022

Conversation

zeripath
Copy link
Contributor

The cache service can be disabled - at which point ctx.Cache will be nil
and the use of it will cause an NPE.

The main part of this PR is that the cache is used for restricting
resending of activation mails and without this we cache we cannot
restrict this. Whilst this code could be re-considered to use the db and
probably should be, I think we can simply disable this code in the case
that the cache is disabled.

There are also several bug fixes in the /nodeinfo API endpoint.

Fix #18749

Signed-off-by: Andrew Thornton art27@cantab.net

The cache service can be disabled - at which point ctx.Cache will be nil
and the use of it will cause an NPE.

The main part of this PR is that the cache is used for restricting
resending of activation mails and without this we cache we cannot
restrict this. Whilst this code could be re-considered to use the db and
probably should be, I think we can simply disable this code in the case
that the cache is disabled.

There are also several bug fixes in the /nodeinfo API endpoint.

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath added this to the 1.17.0 milestone May 14, 2022
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label May 14, 2022
@6543
Copy link
Member

6543 commented May 14, 2022

I think we should create some "noop cache" that we would just can use instead if option is filped

as fix it's ok

@codecov-commenter

This comment was marked as off-topic.

@Gusted
Copy link
Contributor

Gusted commented May 14, 2022

I don't feel comfortable approving this PR, this feels too error-prone to have such NPE against because we forgot we need to check if CacheService is enabled. @6543 suggestions seems reasonable.

@lunny
Copy link
Member

lunny commented May 15, 2022

Maybe you mean #19708

@wxiaoguang
Copy link
Contributor

wxiaoguang commented May 15, 2022

IIRC this problem has been discussed in discord long time ago.

The agreement at that time was: always use a builtin cache if there is no external cache (eg: use the builtin cache called twoqueue) , or make some logic (email sending protection) work correctly without cache.

Around 02/14/2022:

02/14/2022
Oh, that's reasonable, but it might become an non-obvious knowledge to end users.
That's why I propose to force the cache to be enabled with fallback.
Otherwise if someone disables the cache by mistake, then spam emails would become a security problem. I would be more careful about such case.

02/14/2022
yeah ... then we do the rest of your plan - 2. 2Q as default + (memory = twoqueue with memory-legacy for the old memory), and 3. we make this work properly

@6543

This comment was marked as outdated.

@6543
Copy link
Member

6543 commented May 15, 2022

@6543
Copy link
Member

6543 commented May 15, 2022

@wxiaoguang we do have memcache enabled by default! https://docs.gitea.io/en-us/config-cheat-sheet/#cache-cache

@wxiaoguang
Copy link
Contributor

wxiaoguang commented May 15, 2022

@wxiaoguang we do have memcache enabled by default! https://docs.gitea.io/en-us/config-cheat-sheet/#cache-cache

Yes, but the panic is caused by some users who disable the cache manually. The related issue has the details:

That's why the agreement on 02/14/2022 came: either always enable the cache (remove the option from app.ini), or make all logic correct without cache (protect users from email spam attacking even if there is no cache)


(no blocker from my side, I just try to mention the plan and risk which has been discussed before, while always using a builtin cache as default is the best choice in my mind).

@lunny
Copy link
Member

lunny commented May 15, 2022

@wxiaoguang we do have memcache enabled by default! https://docs.gitea.io/en-us/config-cheat-sheet/#cache-cache

Yes, but the panic is caused by some users who disable the cache manually. The related issue has the details:

That's why the agreement on 02/14/2022 came: either always enable the cache (remove the option from app.ini), or make all logic correct without cache (protect users from email spam attacking even if there is no cache)

(no blocker from my side, I just try to mention the plan and risk which has been discussed before, while always using a builtin cache as default is the best choice in my mind).

Hm, the email part should be refactor.

@zeripath
Copy link
Contributor Author

The NPEs have been useful for spotting bugs in the past.

For example the problems in nodeinfo

zeripath added a commit to zeripath/gitea that referenced this pull request May 15, 2022
Extract from go-gitea#19703

Signed-off-by: Andrew Thornton <art27@cantab.net>
lunny pushed a commit that referenced this pull request May 16, 2022
Extract from #19703

Signed-off-by: Andrew Thornton <art27@cantab.net>
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 21, 2022
@lunny lunny merged commit 468387e into go-gitea:main May 21, 2022
@zeripath zeripath deleted the fix-18749-prevent-panic-when-no-cache branch May 21, 2022 16:29
zjjhot added a commit to zjjhot/gitea that referenced this pull request May 21, 2022
* giteaofficial/main:
  Prevent NPE when cache service is disabled (go-gitea#19703)
  Detect truncated utf-8 characters at the end of content as still representing utf-8 (go-gitea#19773)
  Add silentcodeg to MAINTAINERS (go-gitea#19771)
  Allows repo search to match against "owner/repo" pattern strings (go-gitea#19754)
  Update JS dependencies (go-gitea#19767)
  Nuke the incorrect permission report on /api/v1/notifications (go-gitea#19761)
zeripath added a commit to zeripath/gitea that referenced this pull request May 22, 2022
Backport go-gitea#19703

The cache service can be disabled - at which point ctx.Cache will be nil
and the use of it will cause an NPE.

The main part of this PR is that the cache is used for restricting
resending of activation mails and without this we cache we cannot
restrict this. Whilst this code could be re-considered to use the db and
probably should be, I think we can simply disable this code in the case
that the cache is disabled.

Signed-off-by: Andrew Thornton <art27@cantab.net>
@6543 6543 added the backport/done All backports for this PR have been created label May 24, 2022
lunny pushed a commit that referenced this pull request May 25, 2022
Backport #19703

The cache service can be disabled - at which point ctx.Cache will be nil
and the use of it will cause an NPE.

The main part of this PR is that the cache is used for restricting
resending of activation mails and without this we cache we cannot
restrict this. Whilst this code could be re-considered to use the db and
probably should be, I think we can simply disable this code in the case
that the cache is disabled.

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: Lauris BH <lauris@nix.lv>
zeripath added a commit to zeripath/gitea that referenced this pull request Jun 20, 2022
## [1.16.9](https://github.com/go-gitea/gitea/releases/tag/1.16.9) - 2022-06-20

* BUGFIXES
  * Fix permission check for delete tag (go-gitea#19985) (go-gitea#20001)
  * Only log non ErrNotExist errors in git.GetNote  (go-gitea#19884) (go-gitea#19905)
  *  Use exact search instead of fuzzy search for branch filter dropdown (go-gitea#19885) (go-gitea#19893)
  * Set Setpgid on child git processes (go-gitea#19865) (go-gitea#19881)
  * Import git from alpine 3.16 repository as 2.30.4 is needed for `safe.directory = '*'` to work but alpine 3.13 has 2.30.3 (go-gitea#19876)
  * Ensure responses are context.ResponseWriters (go-gitea#19843) (go-gitea#19859)
  * Fix count bug (go-gitea#19850)
  * Fix raw endpoint PDF file headers (go-gitea#19825) (go-gitea#19826)
  * Make WIP prefixes case insensitive, e.g. allow `Draft` as a WIP prefix (go-gitea#19780) (go-gitea#19811)
  * Fix NotificationUnreadCount (go-gitea#19802)
  * Prevent NPE when cache service is disabled (go-gitea#19703) (go-gitea#19783)
  * Detect truncated utf-8 characters at the end of content as still representing utf-8 (go-gitea#19773) (go-gitea#19774)
  * Fix doctor pq: syntax error at or near "." quote user table name (go-gitea#19765) (go-gitea#19770)
  * Fix bug (go-gitea#19757)

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath mentioned this pull request Jun 20, 2022
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 24, 2022
…19721)

Extract from go-gitea#19703

Signed-off-by: Andrew Thornton <art27@cantab.net>
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 24, 2022
The cache service can be disabled - at which point ctx.Cache will be nil
and the use of it will cause an NPE.

The main part of this PR is that the cache is used for restricting
resending of activation mails and without this we cache we cannot
restrict this. Whilst this code could be re-considered to use the db and
probably should be, I think we can simply disable this code in the case
that the cache is disabled.

There are also several bug fixes in the /nodeinfo API endpoint.

Signed-off-by: Andrew Thornton <art27@cantab.net>
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
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. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting cache.ENABLED=false causes 500 panic
8 participants