diff --git a/README.md b/README.md index 0991b60..384b3d6 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,8 @@ idTokenNameKey: "name" idTokenEmailKey: "email" ``` +The `id` may contain only letters, digits, `_` and `.` (e.g. `"100"`, `"sheets_read"`, `"onedrive.v2"`). Hyphens are not allowed because the OAuth `state` parameter uses `-` as a separator. Clients discover the id via the app list endpoint and pass it back on authorize/token/refresh requests. + #### User Identity Keys After a successful token exchange, the proxy calls `testingUrl` to verify the token works and retrieve the user's identity. The four key fields tell the proxy where to find the user's name and email in the provider's response: diff --git a/doc/spec/oss-architecture.md b/doc/spec/oss-architecture.md index f24d8e2..e45f825 100644 --- a/doc/spec/oss-architecture.md +++ b/doc/spec/oss-architecture.md @@ -317,7 +317,7 @@ The page renders localized text (based on `Accept-Language`) and includes a Java ### Behavioral Notes -**State parameter format:** The state value is `{appID}-{redirectURL}`. It is split on the first hyphen, so app IDs must not contain hyphens. The desktop client receives this state from the OAuth provider callback and passes it back to `/v1/token`. +**State parameter format:** The state value is `{appID}-{redirectURL}`. It is split on the first hyphen; app IDs are restricted to `[A-Za-z0-9_.]`, so they never contain hyphens. The desktop client receives this state from the OAuth provider callback and passes it back to `/v1/token`. **Scope validation:** Token exchange verifies that all scopes configured in `creds.yaml` are present in the provider's response. If the provider returns fewer scopes (e.g., user denied a permission), the exchange fails with an error listing the missing scopes. @@ -428,7 +428,7 @@ idTokenEmailKey: "email" | Field | Required | Description | |-------|----------|-------------| -| `id` | Yes | App identifier (must be "100") | +| `id` | Yes | App identifier — letters, digits, `_` and `.` only (e.g. `"100"`, `"sheets_read"`); no hyphens | | `type` | Yes | App type label (e.g., "sheets", "onedrive") | | `clientId` | Yes | OAuth2 client ID | | `clientSecret` | Yes | OAuth2 client secret (kept server-side) | @@ -448,7 +448,7 @@ idTokenEmailKey: "email" | `idTokenNameKey` | No | JWT claim key for name (fallback if userinfo unavailable) | | `idTokenEmailKey` | No | JWT claim key for email (fallback if userinfo unavailable) | -Currently only a single app configuration (ID "100") is supported per instance. +Currently only a single app configuration is supported per instance. Its `id` may contain letters, digits, `_` and `.`. ## 7. Security Model diff --git a/internal/config.go b/internal/config.go index 169121e..0759306 100644 --- a/internal/config.go +++ b/internal/config.go @@ -8,6 +8,7 @@ import ( "io" "os" "reflect" + "regexp" "slices" "strings" "time" @@ -166,17 +167,19 @@ func LoadApps(filePath string, logger *zap.Logger) (map[string]AppConfig, error) return apps, nil } +// appIDPattern restricts app IDs to letters, digits, '_' and '.'. Hyphens are +// excluded because the OAuth state format ({appID}-{redirectURL}) is split on +// the first hyphen. +var appIDPattern = regexp.MustCompile(`^[A-Za-z0-9_.]+$`) + func validateApps(apps map[string]AppConfig) error { if len(apps) != 1 { return fmt.Errorf("exactly one app must be defined in creds.yaml, got %d", len(apps)) } - if _, ok := apps["100"]; !ok { - return fmt.Errorf("app with id '100' must be defined in creds.yaml") - } for _, app := range apps { - if strings.Contains(app.ID, "-") { - return fmt.Errorf("app id cannot contain hyphens: %s", app.ID) + if !appIDPattern.MatchString(app.ID) { + return fmt.Errorf("app id %q must match %s", app.ID, appIDPattern) } if err := validateAppFields(app); err != nil { diff --git a/internal/config_test.go b/internal/config_test.go index 853903c..cf3c006 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -3,6 +3,9 @@ package proxy import ( + "os" + "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -129,25 +132,56 @@ func TestLoadApps_MissingFile(t *testing.T) { r.Contains(err.Error(), "read creds file") } -func TestValidateApps_NoDashes(t *testing.T) { - r := require.New(t) +func requireValidAppID(t *testing.T, id string) { + t.Helper() + apps := map[string]AppConfig{id: fullTestApp(id)} + require.NoError(t, validateApps(apps), "expected app id %q to be accepted", id) +} - apps := map[string]AppConfig{ - "bad-id": {ID: "bad-id"}, - } +func requireInvalidAppID(t *testing.T, id string) { + t.Helper() + apps := map[string]AppConfig{id: fullTestApp(id)} err := validateApps(apps) - r.Error(err) + require.Error(t, err, "expected app id %q to be rejected", id) + require.Contains(t, err.Error(), appIDPattern.String()) +} + +func TestValidateApps_AcceptsAllowedAppIDs(t *testing.T) { + requireValidAppID(t, "100") // pure numeric, the historical default + requireValidAppID(t, "0") // single character + requireValidAppID(t, "sheets_read") // README example + requireValidAppID(t, "onedrive.v2") // README example + requireValidAppID(t, "Sheets_read.v2") // mixed case with every allowed symbol + requireValidAppID(t, "_.") // symbols only +} + +func TestValidateApps_RejectsDisallowedAppIDs(t *testing.T) { + requireInvalidAppID(t, "my-app") // hyphen breaks the {appID}-{redirectURL} state format + requireInvalidAppID(t, "-100") // leading hyphen + requireInvalidAppID(t, "sheets read") // space + requireInvalidAppID(t, " 100") // leading whitespace + requireInvalidAppID(t, "100\n") // trailing newline + requireInvalidAppID(t, "sheets/read") // slash + requireInvalidAppID(t, "sheets!") // punctuation outside the allowed set + requireInvalidAppID(t, "a&b=c") // URL query delimiters + requireInvalidAppID(t, "café") // non-ASCII letter } -func TestValidateApps_RequiresID100(t *testing.T) { +func TestLoadApps_UnquotedNumericID(t *testing.T) { r := require.New(t) - apps := map[string]AppConfig{ - "200": fullTestApp("200"), - } - err := validateApps(apps) - r.Error(err) - r.Contains(err.Error(), "id '100'") + orig, err := os.ReadFile("testdata/creds.yaml") + r.NoError(err) + content := strings.Replace(string(orig), `id: "100"`, "id: 4217", 1) + r.NotEqual(string(orig), content) + + path := filepath.Join(t.TempDir(), "creds.yaml") + r.NoError(os.WriteFile(path, []byte(content), 0o600)) + + apps, err := LoadApps(path, zap.NewNop()) + r.NoError(err) + r.Contains(apps, "4217") + r.Equal("4217", apps["4217"].ID) } func TestValidateApps_RequiresEmailScope(t *testing.T) {