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