Skip to content

Commit

Permalink
fix: base64 encode identity schema URLs
Browse files Browse the repository at this point in the history
Previously, identity schema IDs with special characters could lead to broken URLs. This patch introduces a change where identity schema IDs are base64 encoded to address this issue. Schema IDs that are not base64 encoded will continue working.
  • Loading branch information
aeneasr committed Feb 26, 2022
1 parent 50ac851 commit ad44e4d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion schema/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ func (h *Handler) getByID(w http.ResponseWriter, r *http.Request, ps httprouter.
return
}

s, err := ss.GetByID(ps.ByName("id"))
id := ps.ByName("id")
if dec, err := base64.RawURLEncoding.DecodeString(ps.ByName("id")); err == nil {
id = string(dec)
}

s, err := ss.GetByID(id)
if err != nil {
h.r.Writer().WriteError(w, r, errors.WithStack(herodot.ErrNotFound.WithDebugf("%+v", err)))
return
Expand Down
11 changes: 11 additions & 0 deletions schema/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func TestHandler(t *testing.T) {
URL: urlx.ParseOrPanic("file://./stub"),
RawURL: "file://./stub",
},
{
ID: "preset://email",
URL: urlx.ParseOrPanic("file://./stub/identity-2.schema.json"),
RawURL: "file://./stub/identity-2.schema.json",
},
}

getSchemaById := func(id string) *schema.Schema {
Expand Down Expand Up @@ -137,6 +142,12 @@ func TestHandler(t *testing.T) {
require.JSONEq(t, string(file), string(server))
})

t.Run("case=get encoded schema", func(t *testing.T) {
server := getFromTSById("cHJlc2V0Oi8vZW1haWw", http.StatusOK)
file := getFromFS("preset://email")
require.JSONEq(t, string(file), string(server))
})

t.Run("case=get unreachable schema", func(t *testing.T) {
reason := getFromTSById("unreachable", http.StatusInternalServerError)
require.Contains(t, string(reason), "could not be found or opened")
Expand Down
3 changes: 2 additions & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"context"
"encoding/base64"
"io/ioutil"
"net/url"
"strings"
Expand Down Expand Up @@ -100,5 +101,5 @@ type Schema struct {
}

func (s *Schema) SchemaURL(host *url.URL) *url.URL {
return urlx.AppendPaths(host, SchemasPath, s.ID)
return urlx.AppendPaths(host, SchemasPath, base64.RawURLEncoding.EncodeToString([]byte(s.ID)))
}

0 comments on commit ad44e4d

Please sign in to comment.