Skip to content

[fix] stop serving the openapi document to anonymous callers - #4276

Merged
Duansg merged 19 commits into
apache:masterfrom
Duansg:fix-openapi-docs-authz
Aug 14, 2026
Merged

[fix] stop serving the openapi document to anonymous callers#4276
Duansg merged 19 commits into
apache:masterfrom
Duansg:fix-openapi-docs-authz

Conversation

@Duansg

@Duansg Duansg commented Jul 29, 2026

Copy link
Copy Markdown
Member

What's changed?

The OpenAPI/Swagger document endpoints are no longer open to anonymous callers; scoped to admin.

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

  • I have added the necessary e2e tests and all cases have passed.

@Duansg

Duansg commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

/swagger-ui/index.html is still served anonymously by the existing /**/*.html===get exclusion, but the page fetches /v3/api-docs/swagger-config and /v3/api-docs without an Authorization header — swagger-ui's Authorize button only applies to try-it-out calls, not to the spec fetch. The UI will therefore show "Failed to load API definition" for everyone, including admins.

The document itself remains reachable for an admin with a token:

curl -H "Authorization: Bearer $JWT" http://localhost:1157/v3/api-docs

If the community would rather not ship a UI page that cannot load, the follow-up is to set springdoc.api-docs.enabled / springdoc.swagger-ui.enabled to false by default and let deployments opt in; that is a product decision kept out of this PR.

# Conflicts:
#	hertzbeat-startup/src/main/resources/sureness.yml
#	script/docker-compose/hertzbeat-mysql-iotdb/conf/sureness.yml
#	script/docker-compose/hertzbeat-mysql-tdengine/conf/sureness.yml
#	script/docker-compose/hertzbeat-mysql-victoria-metrics/conf/sureness.yml
#	script/docker-compose/hertzbeat-postgresql-greptimedb/conf/sureness.yml
#	script/docker-compose/hertzbeat-postgresql-victoria-metrics/conf/sureness.yml
#	script/sureness.yml
@Duansg
Duansg requested a review from zqr10159 August 9, 2026 14:20

@zqr10159 zqr10159 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Restricting the generated OpenAPI document to administrators is the right security boundary, but the current change leaves /swagger-ui/index.html publicly reachable while the UI cannot attach an administrator token to its initial /v3/api-docs/swagger-config and /v3/api-docs requests. The result is a shipped page that shows “Failed to load API definition” for anonymous users and administrators alike. Please either disable the Swagger UI by default (with an explicit opt-in deployment path), or provide an authenticated loading flow that lets an administrator use it. Keep the document endpoints admin-only in either case.

Duansg and others added 8 commits August 12, 2026 22:46
Scoping the generated document to the admin role left the swagger ui page
reachable anonymously through the /**/*.html exclusion, while the page fetches
/v3/api-docs/swagger-config and /v3/api-docs without an Authorization header -
its Authorize button only applies to try-it-out calls. Everyone, administrators
included, would have seen "Failed to load API definition".

Sureness checks the exclusion tree before any credential check, so the ui page
cannot be pulled back under an rbac rule without narrowing the /**/*.html
exclusion itself, which is a separate and much wider change. Both springdoc
endpoints are therefore off in the shipped configuration and a deployment opts
in explicitly; the admin-only rules stay as the second line of defence for the
deployments that do.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the doc Improvements or additions to documentation label Aug 12, 2026
@Duansg

Duansg commented Aug 12, 2026

Copy link
Copy Markdown
Member Author

What changed

  • Swagger UI authenticates itself. AuthorizedSwaggerIndexTransformer replaces the springdoc SwaggerIndexTransformer bean and appends a requestInterceptor to swagger-initializer.js that attaches the stored HertzBeat token to same-origin requests. springdoc's built-in swagger-ui.csrf.use-local-storage cannot be used here: it writes the stored value verbatim, while HertzBeat keeps the bare JWT and callers add the Bearer prefix. If the initializer ever ships in an unrecognised shape the transformer throws rather than serving an unauthenticated page.
  • One endpoint was missed. springdoc serves grouped documents at /v3/api-docs.yaml/{group}; added - /v3/api-docs.yaml/**===get===[admin] to all copies. The old assertion targeted /v3/api-docs/default.yaml, a path springdoc does not serve.
  • All 9 in-tree sureness.yml copies are in sync.
  • Enabling only swagger-ui.enabled no longer breaks startup. The transformer is built from springdoc beans gated by api-docs.enabled, so the bean condition now matches both switches.
  • Docs updated in English and zh-cn.

Verified

Signed in as an administrator on the same origin, /v3/api-docs/swagger-config and /v3/api-docs are requested with Authorization: Bearer …, both 200, and the UI renders with no prompt. Without that session sureness answers 401 with a Digest challenge; administrator credentials get through, an authenticated non-admin account gets 403. Unit tests cover the rules, the switches, and the transformer; checkstyle clean.

Aug-13-2026 01-58-02

@Duansg
Duansg requested a review from zqr10159 August 12, 2026 18:12

@zqr10159 zqr10159 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The previous Swagger UI regression is resolved. OpenAPI and Swagger UI are now disabled by default across the packaged and deployment configurations; when explicitly enabled, the UI attaches the existing HertzBeat JWT only to same-origin requests, while the document endpoints remain admin-only. The configuration-copy contracts, transformer tests, focused security tests, and current-head CI are all green.

@Duansg
Duansg merged commit cf737db into apache:master Aug 14, 2026
5 checks passed
@Duansg
Duansg deleted the fix-openapi-docs-authz branch August 14, 2026 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend doc Improvements or additions to documentation docker docker-compose script

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants