forgejo/services/pull
Mathieu Fenniak f93d2cb261 ci: detect and prevent empty case statements in Go code (#11593)
One of the security patches released 2026-03-09 [fixed a vulnerability](d1c7b04d09) caused by a misapplication of Go `case` statements, where the implementation would have been correct if Go `case` statements automatically fall through to the next case block, but they do not.  This PR adds a semgrep rule which detects any empty `case` statement and raises an error, in order to prevent this coding mistake in the future.

For example, code like this will now trigger a build error:
```go
	switch setting.Protocol {
	case setting.HTTPUnix:
	case setting.FCGI:
	case setting.FCGIUnix:
	default:
		defaultLocalURL := string(setting.Protocol) + "://"
	}
```

Example error:
```
    cmd/web.go
   ❯❯❱ semgrep.config.forgejo-switch-empty-case
          switch has a case block with no content. This is treated as "break" by Go, but developers may
          confuse it for "fallthrough".  To fix this error, disambiguate by using "break" or
          "fallthrough".

          279┆ switch setting.Protocol {
          280┆ case setting.HTTPUnix:
          281┆ case setting.FCGI:
          282┆ case setting.FCGIUnix:
          283┆ default:
          284┆   defaultLocalURL := string(setting.Protocol) + "://"
          285┆   if setting.HTTPAddr == "0.0.0.0" {
          286┆           defaultLocalURL += "localhost"
          287┆   } else {
          288┆           defaultLocalURL += setting.HTTPAddr
```

As described in the error output, this error can be fixed by explicitly listing `break` (the real Go behaviour, to do nothing in the block), or by listing `fallthrough` (if the intent was to fall through).

All existing code triggering this detection has been changed to `break` (or, rarely, irrelevant cases have been removed), which should maintain the same code functionality.  While performing this fixup, a light analysis was performed on each case and they *appeared* correct, but with ~65 cases I haven't gone into extreme depth.

Tests are present for the semgrep rule in `.semgrep/tests/go.go`.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11593
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-03-10 02:50:28 +01:00
..
check.go chore: move code for manual merges into merge_manual.go (#10141) 2025-11-28 07:57:31 +01:00
check_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
comment.go feat: Use receive.hideRefs (#10015) 2025-11-10 14:36:15 +01:00
commit_status.go fix: correctly compute required commit status (#10747) 2026-01-12 10:53:40 +01:00
commit_status_test.go fix: correctly compute required commit status (#10747) 2026-01-12 10:53:40 +01:00
edits.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
lfs.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
main_test.go chore: move all test blank imports in a single package (#10662) 2026-01-02 05:32:32 +01:00
merge.go fix: correct Reviewed-on URL in merge message for subpath deployments (#11240) 2026-02-11 18:12:29 +01:00
merge_ff_only.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
merge_manual.go chore: move code for manual merges into merge_manual.go (#10141) 2025-11-28 07:57:31 +01:00
merge_merge.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
merge_prepare.go fix: short-circuit to avoid rebasing (#8622) 2025-07-23 07:30:30 +02:00
merge_rebase.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
merge_squash.go fix(ui)!: remove squash merge committer trailer admin option (#11096) 2026-02-07 12:58:26 +01:00
merge_test.go fix(ui)!: remove squash merge committer trailer admin option (#11096) 2026-02-07 12:58:26 +01:00
patch.go chore: fix typos throughout the codebase (#10753) 2026-01-26 22:57:33 +01:00
patch_test.go feat: improved performances when checking for conflicts on pull requests (#7727) 2025-05-16 12:40:38 +00:00
patch_unmerged.go ci: detect and prevent empty case statements in Go code (#11593) 2026-03-10 02:50:28 +01:00
pull.go chore: fix typos throughout the codebase (#10753) 2026-01-26 22:57:33 +01:00
pull_test.go fix: correct Reviewed-on URL in merge message for subpath deployments (#11240) 2026-02-11 18:12:29 +01:00
review.go fix: pull request review comment position (#9914) 2025-10-31 16:17:23 +01:00
review_test.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
temp_repo.go chore: handle error types consistently (#9873) 2026-03-06 00:48:06 +01:00
update.go 2025-11-21 combined security patches (#10037) 2025-11-21 05:23:43 +01:00
update_rebase.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00