Add raw-token validity checks API behind a feature flag

Problem

Scanners such as Tokinator hold raw token values and have no way to ask GitLab whether they are still active. The spike in https://gitlab.com/gitlab-org/gitlab/-/work_items/632986 settled on a synchronous REST endpoint. !268642 (merged) added the domain module; this MR adds the route.

Changes

Added endpoint

POST /api/v4/secret_detection/validity_checks_raw_token: hidden, experiment, Ultimate, behind the wip flag validity_checks_raw_token_api.

  • Takes up to 100 tokens (id, token_type, raw_token) and returns one result per id. Raw values are never echoed or stored. The API log omits the whole tokens array, so a raw value in a malformed request is not logged either. Whitespace around a raw value is stripped before the lookup, so a token captured with a trailing newline is still found.
  • Requires client_name, a caller-declared name for the calling system, such as tokinator. Several systems, such as Tokinator and Omamori, can share one user account, so the user alone does not identify the caller. The value is not verified and is never used for authorization or rate limits. The service accepts it but does not use it until the audit event in https://gitlab.com/gitlab-org/gitlab/-/work_items/637859.
  • A before block resolves the project from auth_project_id, checks the flag and the ability, then applies the rate limits. The service checks the ability again.
  • Responses: 404 for role, licence, plan or flag denials, and for any request while the flag is off. 403 for a token without the permission or api scope, which applies with the flag off too. 400 for duplicate ids, blank or null values, a null or empty tokens array, a missing or malformed client_name, and tokens that are not objects.
  • The paired HTTP Router change, gitlab-org/cells/http-router!1510 (merged), is merged.

Rate limit

60 requests a minute per caller and 120 per top-level namespace, checked in that order. A throttled request returns 429 with Retry-After. Only callers with the ability count toward either limit.

  • Hardcoded, as https://gitlab.com/gitlab-org/gitlab/-/work_items/637858 asks, and documented in doc/rate_limits/non_configurable.md. I will revisit the limits before productizing.
  • Rule.new rather than staged_rule: the endpoint has no existing traffic to observe and is already flag-gated.

Fine-grained PATs

A new assignable verify_secret_detection_raw_token permission, with the project boundary resolved from auth_project_id. It ships now because Tokinator will use a fine-grained token for Customer Zero testing, so it appears in the token form and docs while the endpoint is still hidden.

Project policy

On GitLab.com, paid_plan_excluding_trials prevents the ability unless the top-level namespace is on a paid plan. Free trials and the Open Source Program are refused.

Reviewer focus

The route holds no authorization logic beyond the before block. Role, licence and plan are decided by the project policy.

Threat Modeling

See https://gitlab.com/gitlab-org/gitlab/-/work_items/637856#note_4223768884.

Validation

  • Request spec: 52 examples, 0 failures, 1 pending.
  • EE rate limit registry spec: 27 examples, 0 failures.
  • Raw-token examples in the project policy spec: 8 examples, 0 failures.
  • Role definitions spec, which now covers the role split for this ability: 78 examples, 0 failures.
  • RuboCop and rake gitlab:permissions:validate pass.
  • I skipped the local OpenAPI docs pre-push hook because my local database config reorders an unrelated enum. The pipeline check is authoritative.

Local verification

I called the endpoint on a GDK with an Ultimate licence and the flag enabled for one private project. Every request sent "client_name": "tokinator" unless the row says otherwise.

Caller and request Result
Developer, api token: an active PAT, a revoked PAT and an unknown type in one batch 200 with active, inactive and unsupported_token_type
The same request with "client_name": "omamori" 200 with the same results
Guest, api token 404
Fine-grained token with the permission 200
Fine-grained token without the permission, flag on or off 403 insufficient_granular_scope
client_name missing, null, blank, containing a space, or 65 characters long 400
Empty tokens array 400
tokens as an array of bare strings instead of objects 400
A token with the raw value under a key other than raw_token 400
"raw_token": null 400
Any request against a project without the flag, with or without client_name 404
61st request in a minute from one caller 429 with Retry-After: 60 and RateLimit-Name: validity_checks_raw_token_api
Another caller while the first is throttled 200

I did not exercise the namespace limit, which needs more than 120 requests a minute across at least three callers.

Example request:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --header "Content-Type: application/json" \
  --data '{
    "auth_project_id": "gitlab-org/gitlab-shell",
    "client_name": "tokinator",
    "tokens": [
      { "id": "1", "token_type": "gitlab_personal_access_token", "raw_token": "<raw_token_value>" },
      { "id": "2", "token_type": "not_a_rule", "raw_token": "x" }
    ]
  }' \
  --url "https://gdk.test:3443/api/v4/secret_detection/validity_checks_raw_token"

Tokens I called the endpoint with:

  • Three Developers' PATs with the api scope: one for the calls above, one to reach the rate limit, and one as the other caller.
  • A Guest's PAT with the api scope.
  • A Developer's fine-grained PAT with the verify_secret_detection_raw_token permission on the project.
  • A Developer's fine-grained PAT with a different permission on the project.

Raw tokens I submitted for checking: an active PAT, a revoked PAT, and a value with an unsupported type.

This MR includes the rate limits from !268639 (closed), now closed. The audit event follows in https://gitlab.com/gitlab-org/gitlab/-/work_items/637859.

Related to https://gitlab.com/gitlab-org/gitlab/-/work_items/637856 and https://gitlab.com/gitlab-org/gitlab/-/work_items/637857.

Edited by Jennifer Li

Merge request reports

Loading
Loading