π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[server] move ua parsing from client to server side
Priyanshu Dangareef4f32f33e4c
diff --git a/server/ente/cast/entity.go b/server/ente/cast/entity.goindex 4d24c18bf2..232614caa4 100644--- a/server/ente/cast/entity.go+++ b/server/ente/cast/entity.go@@ -11,8 +11,7 @@ type CastRequest struct {}type RegisterDeviceRequest struct {- PublicKey string `json:"publicKey" binding:"required"`- DeviceName *string `json:"deviceName"`+ PublicKey string `json:"publicKey" binding:"required"`}type AuthContext struct {diff --git a/server/go.mod b/server/go.modindex 1ed3fb50c7..bb75ffa3a3 100644--- a/server/go.mod+++ b/server/go.mod@@ -30,6 +30,7 @@ require (github.com/prometheus/common v0.26.0github.com/robfig/cron/v3 v3.0.1github.com/sirupsen/logrus v1.8.3+ github.com/slipros/devicedetector v1.2.1github.com/spf13/viper v1.8.1github.com/stretchr/testify v1.10.0github.com/stripe/stripe-go/v72 v72.37.0@@ -47,16 +48,17 @@ require (cloud.google.com/go/compute/metadata v0.3.0 // indirectgithub.com/bytedance/sonic v1.9.1 // indirectgithub.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect+ github.com/dlclark/regexp2 v1.11.5 // indirectgithub.com/fxamacker/cbor/v2 v2.6.0 // indirectgithub.com/gabriel-vasile/mimetype v1.4.2 // indirectgithub.com/go-webauthn/x v0.1.9 // indirectgithub.com/goccy/go-json v0.10.2 // indirectgithub.com/golang-jwt/jwt/v5 v5.2.2 // indirect- github.com/google/go-cmp v0.6.0 // indirectgithub.com/google/go-tpm v0.9.0 // indirectgithub.com/google/s2a-go v0.1.4 // indirectgithub.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirectgithub.com/klauspost/cpuid/v2 v2.2.4 // indirect+ github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 // indirectgithub.com/pelletier/go-toml/v2 v2.0.8 // indirectgithub.com/twitchyliquid64/golang-asm v0.15.1 // indirectgithub.com/x448/float16 v0.8.4 // indirectdiff --git a/server/go.sum b/server/go.sumindex 8423534a4d..bc7c449caf 100644--- a/server/go.sum+++ b/server/go.sum@@ -127,6 +127,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmgithub.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=github.com/dhui/dktest v0.3.2 h1:nZSDcnkpbotzT/nEHNsO+JCKY8i1Qoki1AYOpeLRb6M=github.com/dhui/dktest v0.3.2/go.mod h1:l1/ib23a/CmxAe7yixtrYPc8Iy90Zy2udyaHINM5p58=+github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=+github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=github.com/dlmiddlecote/sqlstats v1.0.2 h1:gSU11YN23D/iY50A2zVYwgXgy072khatTsIW6UPjUtI=github.com/dlmiddlecote/sqlstats v1.0.2/go.mod h1:0CWaIh/Th+z2aI6Q9Jpfg/o21zmGxWhbByHgQSCUQvY=github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=@@ -440,6 +442,8 @@ github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUgithub.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=+github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 h1:YocNLcTBdEdvY3iDK6jfWXvEaM5OCKkjxPKoJRdB3Gg=+github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=@@ -537,6 +541,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwdgithub.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=github.com/sirupsen/logrus v1.8.3 h1:DBBfY8eMYazKEJHb3JKpSPfpgd2mBCoNFlQx6C5fftU=github.com/sirupsen/logrus v1.8.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=+github.com/slipros/devicedetector v1.2.1 h1:2hZ7GLfzY3twx7LyJbO5JEDQb4txNpwAVUqfElSqyk8=+github.com/slipros/devicedetector v1.2.1/go.mod h1:JjVAl2QmDLPrUOk1O/hoCqMQh4dC1U5zqpFVSdzpX/M=github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=@@ -1081,6 +1087,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+Cgopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=+gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=+gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=diff --git a/server/pkg/controller/cast/controller.go b/server/pkg/controller/cast/controller.goindex dac719ddb5..3b0304939b 100644--- a/server/pkg/controller/cast/controller.go+++ b/server/pkg/controller/cast/controller.go@@ -8,6 +8,7 @@ import (castRepo "github.com/ente/museum/pkg/repo/cast""github.com/ente/museum/pkg/utils/auth""github.com/ente/museum/pkg/utils/network"+ "github.com/ente/museum/pkg/utils/ua""github.com/ente/stacktrace""github.com/gin-gonic/gin""github.com/google/uuid"@@ -29,7 +30,12 @@ func NewController(castRepo *castRepo.Repository,}func (c *Controller) RegisterDevice(ctx *gin.Context, request *cast.RegisterDeviceRequest) (string, error) {- return c.CastRepo.AddCode(ctx, request.PublicKey, network.GetClientIP(ctx), request.DeviceName)+ ipAddress := network.GetClientIP(ctx)+ deviceName, err := ua.GetDeviceType(ctx.GetHeader("User-Agent"))+ if err != nil {+ deviceName = ipAddress+ }+ return c.CastRepo.AddCode(ctx, request.PublicKey, ipAddress, deviceName)}func (c *Controller) GetAllDevices(ctx *gin.Context, userID int64) ([]cast.CastInfo, error) {diff --git a/server/pkg/repo/cast/repo.go b/server/pkg/repo/cast/repo.goindex 1c7488c9e3..509e7b43cc 100644--- a/server/pkg/repo/cast/repo.go+++ b/server/pkg/repo/cast/repo.go@@ -17,7 +17,7 @@ type Repository struct {DB *sql.DB}-func (r *Repository) AddCode(ctx context.Context, pubKey string, ip string, deviceName *string) (string, error) {+func (r *Repository) AddCode(ctx context.Context, pubKey string, ip string, deviceName string) (string, error) {codeValue, err := random.GenerateAlphaNumString(6)if err != nil {return "", errdiff --git a/server/pkg/utils/ua/ua.go b/server/pkg/utils/ua/ua.gonew file mode 100644index 0000000000..02bbffddd7--- /dev/null+++ b/server/pkg/utils/ua/ua.go@@ -0,0 +1,25 @@+package ua++import (+ "errors"++ . "github.com/slipros/devicedetector"+ "golang.org/x/text/cases"+ "golang.org/x/text/language"+)++func GetDeviceType(ua string) (string, error) {+ dd, dderr := NewDeviceDetector()+ if dderr != nil {+ return "", dderr+ }+ info := dd.Parse(ua)+ if info == nil {+ return "", errors.New("failed to parse user agent")+ }+ if info.Type == "" {+ return "", nil+ }+ titleCaser := cases.Title(language.English)+ return titleCaser.String(info.Type), nil+}diff --git a/server/pkg/utils/ua/ua_test.go b/server/pkg/utils/ua/ua_test.gonew file mode 100644index 0000000000..dd7756ba4c--- /dev/null+++ b/server/pkg/utils/ua/ua_test.go@@ -0,0 +1,23 @@+package ua++import "testing"++func TestGetDeviceType(t *testing.T) {+ deviceType, err := GetDeviceType("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1")+ if err != nil {+ t.Fatalf("GetDeviceType returned error: %v", err)+ }+ if deviceType != "Smartphone" {+ t.Fatalf("GetDeviceType = %q, want %q", deviceType, "Smartphone")+ }+}++func TestGetDeviceTypeReturnsErrorForUnparsableUserAgent(t *testing.T) {+ deviceType, err := GetDeviceType("")+ if err == nil {+ t.Fatal("GetDeviceType returned nil error")+ }+ if deviceType != "" {+ t.Fatalf("GetDeviceType = %q, want empty string", deviceType)+ }+}diff --git a/web/apps/cast/package.json b/web/apps/cast/package.jsonindex 2e6074ae36..d28d9900fc 100644--- a/web/apps/cast/package.json+++ b/web/apps/cast/package.json@@ -5,8 +5,7 @@"dependencies": {"ente-accounts": "*","ente-base": "*",- "ente-media": "*",- "node-device-detector": "2.2.5"+ "ente-media": "*"},"devDependencies": {"@types/chromecast-caf-receiver": "6.0.26",diff --git a/web/apps/cast/src/services/pair.ts b/web/apps/cast/src/services/pair.tsindex 9c171c860f..f432e268c0 100644--- a/web/apps/cast/src/services/pair.ts+++ b/web/apps/cast/src/services/pair.ts@@ -4,11 +4,8 @@ import log from "ente-base/log";import { apiURL } from "ente-base/origins";import { wait } from "ente-utils/promise";import { nullToUndefined } from "ente-utils/transform";-import DeviceDetector from "node-device-detector";import { z } from "zod";-const deviceDetector = new DeviceDetector({ skipBotDetection: true });-export interface Registration {/** A pairing code shown on the screen. A client can use this to connect. */pairingCode: string;@@ -94,7 +91,6 @@ export const register = async (): Promise<Registration> => {try {pairingCode = await registerDevice({publicKey,- deviceName: deviceNameFromUserAgent(navigator.userAgent),});} catch (e) {log.error("Failed to register public key with server", e);@@ -114,7 +110,6 @@ export const register = async (): Promise<Registration> => {*/const registerDevice = async (params: {publicKey: string;- deviceName?: string;}) => {const res = await fetch(await apiURL("/cast/device-info"), {method: "POST",@@ -126,14 +121,6 @@ const registerDevice = async (params: {.deviceCode;};-const deviceNameFromUserAgent = (userAgent: string) => {- const { device, os } = deviceDetector.detect(userAgent);- return device.model || toTitleCase(device.type) || os.name || undefined;-};--const toTitleCase = (value: string) =>- value ? value[0]!.toUpperCase() + value.slice(1) : "";-/*** The structure of the (decrypted) payload that is published (e.g.) by* `publishCastPayload` on the photos web/desktop app.diff --git a/web/package-lock.json b/web/package-lock.jsonindex efee1be60d..c41c4e135c 100644--- a/web/package-lock.json+++ b/web/package-lock.json@@ -133,8 +133,7 @@"dependencies": {"ente-accounts": "*","ente-base": "*",- "ente-media": "*",- "node-device-detector": "2.2.5"+ "ente-media": "*"},"devDependencies": {"@types/chromecast-caf-receiver": "6.0.26",@@ -8526,16 +8525,6 @@}}},- "node_modules/node-device-detector": {- "version": "2.2.5",- "resolved": "https://registry.npmjs.org/node-device-detector/-/node-device-detector-2.2.5.tgz",- "integrity": "sha512-5HZI7nMDHGNnFMjWcmSnoJM5X57ZnmyTCIiW6AxYrJGCbpNjgiV8u8z3HAUSykf6PbXgI1/BqY3dlNova+1LNg==",- "license": "MIT",- "engines": {- "node": ">= 10.x",- "npm": ">= 6.x"- }- },"node_modules/node-exports-info": {"version": "1.6.0","resolved": "https://registry.npmjs.org/node-exports-info/-/node-exports-info-1.6.0.tgz",