@@ -2,8 +2,11 @@ package webserver
2
2
3
3
import (
4
4
"bytes"
5
+ "crypto/aes"
6
+ "crypto/cipher"
5
7
crand "crypto/rand"
6
8
"encoding/base64"
9
+ "errors"
7
10
"fmt"
8
11
"io"
9
12
"log"
@@ -37,12 +40,58 @@ func parseWhip(pth string) (string, string) {
37
40
return "" , ""
38
41
}
39
42
43
+ var idSecret []byte
44
+ var idCipher cipher.Block
45
+
46
+ func init () {
47
+ idSecret = make ([]byte , 16 )
48
+ _ , err := crand .Read (idSecret )
49
+ if err != nil {
50
+ log .Fatalf ("crand.Read: %v" , err )
51
+ }
52
+ idCipher , err = aes .NewCipher (idSecret )
53
+ if err != nil {
54
+ log .Fatalf ("NewCipher: %v" , err )
55
+ }
56
+ }
57
+
40
58
func newId () string {
41
- b := make ([]byte , 16 )
59
+ b := make ([]byte , idCipher . BlockSize () )
42
60
crand .Read (b )
43
61
return base64 .RawURLEncoding .EncodeToString (b )
44
62
}
45
63
64
+ // we obfuscate ids to avoid exposing the WHIP session URL
65
+ func obfuscate (id string ) (string , error ) {
66
+ v , err := base64 .RawURLEncoding .DecodeString (id )
67
+ if err != nil {
68
+ return "" , err
69
+ }
70
+
71
+ if len (v ) != idCipher .BlockSize () {
72
+ return "" , errors .New ("bad length" )
73
+ }
74
+
75
+ idCipher .Encrypt (v , v )
76
+
77
+ return base64 .RawURLEncoding .EncodeToString (v ), nil
78
+ }
79
+
80
+ func deobfuscate (id string ) (string , error ) {
81
+ v , err := base64 .RawURLEncoding .DecodeString (id )
82
+ if err != nil {
83
+ return "" , err
84
+ }
85
+
86
+ if len (v ) != idCipher .BlockSize () {
87
+ return "" , errors .New ("bad length" )
88
+ }
89
+
90
+ idCipher .Decrypt (v , v )
91
+
92
+ return base64 .RawURLEncoding .EncodeToString (v ), nil
93
+ }
94
+
46
95
func canPresent (perms []string ) bool {
47
96
for _ , p := range perms {
48
97
if p == "present" {
@@ -186,6 +235,13 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
186
235
}
187
236
188
237
id := newId ()
238
+ obfuscated , err := obfuscate (id )
239
+ if err != nil {
240
+ http .Error (w , "Internal Server Error" ,
241
+ http .StatusInternalServerError )
242
+ return
243
+ }
244
+
189
245
c := rtpconn .NewWhipClient (g , id , token )
190
246
191
247
_ , err = group .AddClient (g .Name (), c , creds )
@@ -214,7 +270,7 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
214
270
http .StatusInternalServerError )
215
271
}
216
272
217
- w .Header ().Set ("Location" , path .Join (r .URL .Path , id ))
273
+ w .Header ().Set ("Location" , path .Join (r .URL .Path , obfuscated ))
218
274
w .Header ().Set ("Access-Control-Expose-Headers" ,
219
275
"Location, Content-Type, Link" )
220
276
whipICEServers (w )
@@ -226,8 +282,14 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
226
282
}
227
283
228
284
func whipResourceHandler (w http.ResponseWriter , r * http.Request ) {
229
- pth , id := parseWhip (r .URL .Path )
230
- if pth == "" || id == "" {
285
+ pth , obfuscated := parseWhip (r .URL .Path )
286
+ if pth == "" || obfuscated == "" {
287
+ http .Error (w , "Internal server error" ,
288
+ http .StatusInternalServerError )
289
+ return
290
+ }
291
+ id , err := deobfuscate (obfuscated )
292
+ if err != nil {
231
293
http .Error (w , "Internal server error" ,
232
294
http .StatusInternalServerError )
233
295
return
0 commit comments