Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/auth/loginpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"regexp"
"strings"
)

// LoginPageURL is the entry point served by DWService.
Expand All @@ -32,7 +33,8 @@ func (c *LoginConfig) AuthURL() string {

// hexBlobRe matches the hex-encoded config blob passed to dwsInitDecodeHex() in
// loadConfiguration(); it decodes to the JSON that seeds dwsConfig.
var hexBlobRe = regexp.MustCompile(`dwsInitDecodeHex\('([0-9a-f]{200,})'\)`)
var hexBlobRe = regexp.MustCompile(`dwsInitDecodeHex\(\s*((?:'[0-9a-f]+'|"[0-9a-f]+")(?:\s*\+\s*(?:'[0-9a-f]+'|"[0-9a-f]+"))*)\s*\)`)
var hexPartRe = regexp.MustCompile(`['"]([0-9a-f]+)['"]`)

type rawLoginConfig struct {
BaseURL string `json:"baseUrl"`
Expand Down Expand Up @@ -67,7 +69,15 @@ func parseLoginConfig(html []byte) (*LoginConfig, error) {
if m == nil {
return nil, fmt.Errorf("login config blob not found on page")
}
raw, err := hex.DecodeString(string(m[1]))
// Accept only literal hex strings joined with '+', never evaluate JavaScript.
var encoded strings.Builder
for _, part := range hexPartRe.FindAllSubmatch(m[1], -1) {
encoded.Write(part[1])
}
if encoded.Len() < 200 {
return nil, fmt.Errorf("login config blob too short")
}
raw, err := hex.DecodeString(encoded.String())
if err != nil {
return nil, fmt.Errorf("decode config hex: %w", err)
}
Expand Down
32 changes: 32 additions & 0 deletions internal/auth/loginpage_concat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package auth

import (
"encoding/hex"
"strings"
"testing"
)

func TestLoginConfigLiteralConcatenation(t *testing.T) {
raw := `{"baseUrl":"https://access.dwservice.net/","requestUrl":"https://access.dwservice.net/","cryptAlgorithmAccept":[{"importKey":{"keyData":"synthetic-server-public-key"}}]}`
encoded := hex.EncodeToString([]byte(raw))
mid := len(encoded) / 2
for _, expr := range []string{
"'" + encoded + "'",
"'" + encoded[:mid] + "'+'" + encoded[mid:] + "'",
"\"" + encoded[:mid] + "\" + \n \"" + encoded[mid:] + "\"",
} {
cfg, err := parseLoginConfig([]byte("loadConfiguration(dwsInitDecodeHex(" + expr + "))"))
if err != nil || cfg.BaseURL != "https://access.dwservice.net/" || cfg.ServerKeyB64 != "synthetic-server-public-key" {
t.Fatal("literal config not decoded correctly")
}
}
for _, expr := range []string{
"'" + encoded[:mid] + "'+callFunction()+'" + encoded[mid:] + "'",
"'" + encoded[:mid] + "'+window.value",
"'" + strings.Repeat("g", 300) + "'",
} {
if _, err := parseLoginConfig([]byte("dwsInitDecodeHex(" + expr + ")")); err == nil {
t.Fatal("nonliteral config accepted")
}
}
}