api.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "crypto/subtle"
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "net"
  26. "regexp"
  27. "runtime/debug"
  28. "strconv"
  29. "strings"
  30. "unicode"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
  34. )
  35. const (
  36. MAX_API_PARAMS_SIZE = 256 * 1024 // 256KB
  37. PADDING_MAX_BYTES = 16 * 1024
  38. CLIENT_PLATFORM_ANDROID = "Android"
  39. CLIENT_PLATFORM_WINDOWS = "Windows"
  40. CLIENT_PLATFORM_IOS = "iOS"
  41. )
  42. // sshAPIRequestHandler routes Psiphon API requests transported as
  43. // JSON objects via the SSH request mechanism.
  44. //
  45. // The API request handlers, handshakeAPIRequestHandler, etc., are
  46. // reused by webServer which offers the Psiphon API via web transport.
  47. //
  48. // The API request parameters and event log values follow the legacy
  49. // psi_web protocol and naming conventions. The API is compatible with
  50. // all tunnel-core clients but are not backwards compatible with all
  51. // legacy clients.
  52. //
  53. func sshAPIRequestHandler(
  54. support *SupportServices,
  55. geoIPData GeoIPData,
  56. authorizedAccessTypes []string,
  57. name string,
  58. requestPayload []byte) ([]byte, error) {
  59. // Notes:
  60. //
  61. // - For SSH requests, MAX_API_PARAMS_SIZE is implicitly enforced
  62. // by max SSH request packet size.
  63. //
  64. // - The param protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS is an
  65. // array of base64-encoded strings; the base64 representation should
  66. // not be decoded to []byte values. The default behavior of
  67. // https://golang.org/pkg/encoding/json/#Unmarshal for a target of
  68. // type map[string]interface{} will unmarshal a base64-encoded string
  69. // to a string, not a decoded []byte, as required.
  70. var params common.APIParameters
  71. err := json.Unmarshal(requestPayload, &params)
  72. if err != nil {
  73. return nil, common.ContextError(
  74. fmt.Errorf("invalid payload for request name: %s: %s", name, err))
  75. }
  76. return dispatchAPIRequestHandler(
  77. support,
  78. protocol.PSIPHON_SSH_API_PROTOCOL,
  79. geoIPData,
  80. authorizedAccessTypes,
  81. name,
  82. params)
  83. }
  84. // dispatchAPIRequestHandler is the common dispatch point for both
  85. // web and SSH API requests.
  86. func dispatchAPIRequestHandler(
  87. support *SupportServices,
  88. apiProtocol string,
  89. geoIPData GeoIPData,
  90. authorizedAccessTypes []string,
  91. name string,
  92. params common.APIParameters) (response []byte, reterr error) {
  93. // Recover from and log any unexpected panics caused by user input
  94. // handling bugs. User inputs should be properly validated; this
  95. // mechanism is only a last resort to prevent the process from
  96. // terminating in the case of a bug.
  97. defer func() {
  98. if e := recover(); e != nil {
  99. if intentionalPanic, ok := e.(IntentionalPanicError); ok {
  100. panic(intentionalPanic)
  101. } else {
  102. log.LogPanicRecover(e, debug.Stack())
  103. reterr = common.ContextError(errors.New("request handler panic"))
  104. }
  105. }
  106. }()
  107. // Before invoking the handlers, enforce some preconditions:
  108. //
  109. // - A handshake request must precede any other requests.
  110. // - When the handshake results in a traffic rules state where
  111. // the client is immediately exhausted, no requests
  112. // may succeed. This case ensures that blocked clients do
  113. // not log "connected", etc.
  114. //
  115. // Only one handshake request may be made. There is no check here
  116. // to enforce that handshakeAPIRequestHandler will be called at
  117. // most once. The SetHandshakeState call in handshakeAPIRequestHandler
  118. // enforces that only a single handshake is made; enforcing that there
  119. // ensures no race condition even if concurrent requests are
  120. // in flight.
  121. if name != protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME {
  122. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  123. // applies here.
  124. sessionID, err := getStringRequestParam(params, "client_session_id")
  125. if err == nil {
  126. // Note: follows/duplicates baseRequestParams validation
  127. if !isHexDigits(support.Config, sessionID) {
  128. err = errors.New("invalid param: client_session_id")
  129. }
  130. }
  131. if err != nil {
  132. return nil, common.ContextError(err)
  133. }
  134. completed, exhausted, err := support.TunnelServer.GetClientHandshaked(sessionID)
  135. if err != nil {
  136. return nil, common.ContextError(err)
  137. }
  138. if !completed {
  139. return nil, common.ContextError(errors.New("handshake not completed"))
  140. }
  141. if exhausted {
  142. return nil, common.ContextError(errors.New("exhausted after handshake"))
  143. }
  144. }
  145. switch name {
  146. case protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME:
  147. return handshakeAPIRequestHandler(support, apiProtocol, geoIPData, params)
  148. case protocol.PSIPHON_API_CONNECTED_REQUEST_NAME:
  149. return connectedAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  150. case protocol.PSIPHON_API_STATUS_REQUEST_NAME:
  151. return statusAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  152. case protocol.PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME:
  153. return clientVerificationAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  154. }
  155. return nil, common.ContextError(fmt.Errorf("invalid request name: %s", name))
  156. }
  157. var handshakeRequestParams = append(
  158. append(
  159. // Note: legacy clients may not send "session_id" in handshake
  160. []requestParamSpec{{"session_id", isHexDigits, requestParamOptional}},
  161. tacticsParams...),
  162. baseRequestParams...)
  163. // handshakeAPIRequestHandler implements the "handshake" API request.
  164. // Clients make the handshake immediately after establishing a tunnel
  165. // connection; the response tells the client what homepage to open, what
  166. // stats to record, etc.
  167. func handshakeAPIRequestHandler(
  168. support *SupportServices,
  169. apiProtocol string,
  170. geoIPData GeoIPData,
  171. params common.APIParameters) ([]byte, error) {
  172. // Note: ignoring "known_servers" params
  173. err := validateRequestParams(support.Config, params, baseRequestParams)
  174. if err != nil {
  175. return nil, common.ContextError(err)
  176. }
  177. sessionID, _ := getStringRequestParam(params, "client_session_id")
  178. sponsorID, _ := getStringRequestParam(params, "sponsor_id")
  179. clientVersion, _ := getStringRequestParam(params, "client_version")
  180. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  181. isMobile := isMobileClientPlatform(clientPlatform)
  182. normalizedPlatform := normalizeClientPlatform(clientPlatform)
  183. var authorizations []string
  184. if params[protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS] != nil {
  185. authorizations, err = getStringArrayRequestParam(params, protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS)
  186. if err != nil {
  187. return nil, common.ContextError(err)
  188. }
  189. }
  190. // Note: no guarantee that PsinetDatabase won't reload between database calls
  191. db := support.PsinetDatabase
  192. httpsRequestRegexes := db.GetHttpsRequestRegexes(sponsorID)
  193. // Flag the SSH client as having completed its handshake. This
  194. // may reselect traffic rules and starts allowing port forwards.
  195. // TODO: in the case of SSH API requests, the actual sshClient could
  196. // be passed in and used here. The session ID lookup is only strictly
  197. // necessary to support web API requests.
  198. activeAuthorizationIDs, authorizedAccessTypes, err := support.TunnelServer.SetClientHandshakeState(
  199. sessionID,
  200. handshakeState{
  201. completed: true,
  202. apiProtocol: apiProtocol,
  203. apiParams: copyBaseRequestParams(params),
  204. expectDomainBytes: len(httpsRequestRegexes) > 0,
  205. },
  206. authorizations)
  207. if err != nil {
  208. return nil, common.ContextError(err)
  209. }
  210. tacticsPayload, err := support.TacticsServer.GetTacticsPayload(
  211. common.GeoIPData(geoIPData), params)
  212. if err != nil {
  213. return nil, common.ContextError(err)
  214. }
  215. var marshaledTacticsPayload []byte
  216. if tacticsPayload != nil {
  217. marshaledTacticsPayload, err = json.Marshal(tacticsPayload)
  218. if err != nil {
  219. return nil, common.ContextError(err)
  220. }
  221. // Log a metric when new tactics are issued. Logging here indicates that
  222. // the handshake tactics mechanism is active; but logging for every
  223. // handshake creates unneccesary log data.
  224. if len(tacticsPayload.Tactics) > 0 {
  225. logFields := getRequestLogFields(
  226. tactics.TACTICS_METRIC_EVENT_NAME,
  227. geoIPData,
  228. authorizedAccessTypes,
  229. params,
  230. handshakeRequestParams)
  231. logFields[tactics.NEW_TACTICS_TAG_LOG_FIELD_NAME] = tacticsPayload.Tag
  232. logFields[tactics.IS_TACTICS_REQUEST_LOG_FIELD_NAME] = false
  233. log.LogRawFieldsWithTimestamp(logFields)
  234. }
  235. }
  236. // The log comes _after_ SetClientHandshakeState, in case that call rejects
  237. // the state change (for example, if a second handshake is performed)
  238. //
  239. // The handshake event is no longer shipped to log consumers, so this is
  240. // simply a diagnostic log. Since the "server_tunnel" event includes all
  241. // common API parameters and "handshake_completed" flag, this handshake
  242. // log is mostly redundant and set to debug level.
  243. log.WithContextFields(
  244. getRequestLogFields(
  245. "",
  246. geoIPData,
  247. authorizedAccessTypes,
  248. params,
  249. baseRequestParams)).Debug("handshake")
  250. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  251. handshakeResponse := protocol.HandshakeResponse{
  252. SSHSessionID: sessionID,
  253. Homepages: db.GetRandomizedHomepages(sponsorID, geoIPData.Country, isMobile),
  254. UpgradeClientVersion: db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
  255. PageViewRegexes: make([]map[string]string, 0),
  256. HttpsRequestRegexes: httpsRequestRegexes,
  257. EncodedServerList: db.DiscoverServers(geoIPData.DiscoveryValue),
  258. ClientRegion: geoIPData.Country,
  259. ServerTimestamp: common.GetCurrentTimestamp(),
  260. ActiveAuthorizationIDs: activeAuthorizationIDs,
  261. TacticsPayload: marshaledTacticsPayload,
  262. Padding: strings.Repeat(" ", pad_response),
  263. }
  264. responsePayload, err := json.Marshal(handshakeResponse)
  265. if err != nil {
  266. return nil, common.ContextError(err)
  267. }
  268. return responsePayload, nil
  269. }
  270. var connectedRequestParams = append(
  271. []requestParamSpec{
  272. {"session_id", isHexDigits, 0},
  273. {"last_connected", isLastConnected, 0},
  274. {"establishment_duration", isIntString, requestParamOptional | requestParamLogStringAsInt}},
  275. baseRequestParams...)
  276. // updateOnConnectedParamNames are connected request parameters which are
  277. // copied to update data logged with server_tunnel: these fields either only
  278. // ship with or ship newer data with connected requests.
  279. var updateOnConnectedParamNames = []string{
  280. "establishment_duration",
  281. "upstream_bytes_fragmented",
  282. "upstream_min_bytes_written",
  283. "upstream_max_bytes_written",
  284. "upstream_min_delayed",
  285. "upstream_max_delayed",
  286. }
  287. // connectedAPIRequestHandler implements the "connected" API request.
  288. // Clients make the connected request once a tunnel connection has been
  289. // established and at least once per day. The last_connected input value,
  290. // which should be a connected_timestamp output from a previous connected
  291. // response, is used to calculate unique user stats.
  292. // connected_timestamp is truncated as a privacy measure.
  293. func connectedAPIRequestHandler(
  294. support *SupportServices,
  295. geoIPData GeoIPData,
  296. authorizedAccessTypes []string,
  297. params common.APIParameters) ([]byte, error) {
  298. err := validateRequestParams(support.Config, params, connectedRequestParams)
  299. if err != nil {
  300. return nil, common.ContextError(err)
  301. }
  302. // Update, for server_tunnel logging, upstream fragmentor metrics, as the
  303. // client may have performed more upstream fragmentation since the
  304. // previous metrics reported by the handshake request. Also,
  305. // establishment_duration, reported only in the connected request, is
  306. // added to server_tunnel here.
  307. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  308. // applies here.
  309. sessionID, _ := getStringRequestParam(params, "client_session_id")
  310. err = support.TunnelServer.UpdateClientAPIParameters(
  311. sessionID, copyUpdateOnConnectedParams(params))
  312. if err != nil {
  313. return nil, common.ContextError(err)
  314. }
  315. log.LogRawFieldsWithTimestamp(
  316. getRequestLogFields(
  317. "connected",
  318. geoIPData,
  319. authorizedAccessTypes,
  320. params,
  321. connectedRequestParams))
  322. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  323. connectedResponse := protocol.ConnectedResponse{
  324. ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
  325. Padding: strings.Repeat(" ", pad_response),
  326. }
  327. responsePayload, err := json.Marshal(connectedResponse)
  328. if err != nil {
  329. return nil, common.ContextError(err)
  330. }
  331. return responsePayload, nil
  332. }
  333. var statusRequestParams = append(
  334. []requestParamSpec{
  335. {"session_id", isHexDigits, 0},
  336. {"connected", isBooleanFlag, requestParamLogFlagAsBool}},
  337. baseRequestParams...)
  338. var remoteServerListStatParams = []requestParamSpec{
  339. {"session_id", isHexDigits, requestParamOptional},
  340. {"propagation_channel_id", isHexDigits, requestParamOptional},
  341. {"sponsor_id", isHexDigits, requestParamOptional},
  342. {"client_version", isAnyString, requestParamOptional},
  343. {"client_platform", isAnyString, requestParamOptional},
  344. {"client_build_rev", isAnyString, requestParamOptional},
  345. {"client_download_timestamp", isISO8601Date, 0},
  346. {"url", isAnyString, 0},
  347. {"etag", isAnyString, 0},
  348. }
  349. var failedTunnelStatParams = append(
  350. []requestParamSpec{
  351. {"session_id", isHexDigits, 0},
  352. {"client_failed_timestamp", isISO8601Date, 0},
  353. {"tunnel_error", isAnyString, 0}},
  354. baseRequestParams...)
  355. // statusAPIRequestHandler implements the "status" API request.
  356. // Clients make periodic status requests which deliver client-side
  357. // recorded data transfer and tunnel duration stats.
  358. // Note from psi_web implementation: no input validation on domains;
  359. // any string is accepted (regex transform may result in arbitrary
  360. // string). Stats processor must handle this input with care.
  361. func statusAPIRequestHandler(
  362. support *SupportServices,
  363. geoIPData GeoIPData,
  364. authorizedAccessTypes []string,
  365. params common.APIParameters) ([]byte, error) {
  366. err := validateRequestParams(support.Config, params, statusRequestParams)
  367. if err != nil {
  368. return nil, common.ContextError(err)
  369. }
  370. sessionID, _ := getStringRequestParam(params, "client_session_id")
  371. statusData, err := getJSONObjectRequestParam(params, "statusData")
  372. if err != nil {
  373. return nil, common.ContextError(err)
  374. }
  375. // Logs are queued until the input is fully validated. Otherwise, stats
  376. // could be double counted if the client has a bug in its request
  377. // formatting: partial stats would be logged (counted), the request would
  378. // fail, and clients would then resend all the same stats again.
  379. logQueue := make([]LogFields, 0)
  380. // Domain bytes transferred stats
  381. // Older clients may not submit this data
  382. // Clients are expected to send host_bytes/domain_bytes stats only when
  383. // configured to do so in the handshake reponse. Legacy clients may still
  384. // report "(OTHER)" host_bytes when no regexes are set. Drop those stats.
  385. domainBytesExpected, err := support.TunnelServer.ExpectClientDomainBytes(sessionID)
  386. if err != nil {
  387. return nil, common.ContextError(err)
  388. }
  389. if domainBytesExpected && statusData["host_bytes"] != nil {
  390. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  391. if err != nil {
  392. return nil, common.ContextError(err)
  393. }
  394. for domain, bytes := range hostBytes {
  395. domainBytesFields := getRequestLogFields(
  396. "domain_bytes",
  397. geoIPData,
  398. authorizedAccessTypes,
  399. params,
  400. statusRequestParams)
  401. domainBytesFields["domain"] = domain
  402. domainBytesFields["bytes"] = bytes
  403. logQueue = append(logQueue, domainBytesFields)
  404. }
  405. }
  406. // Remote server list download stats. Older clients may not submit this data.
  407. // Limitation: for "persistent" stats, geolocation is time-of-sending not time-of-recording.
  408. if statusData["remote_server_list_stats"] != nil {
  409. remoteServerListStats, err := getJSONObjectArrayRequestParam(statusData, "remote_server_list_stats")
  410. if err != nil {
  411. return nil, common.ContextError(err)
  412. }
  413. for _, remoteServerListStat := range remoteServerListStats {
  414. err := validateRequestParams(support.Config, remoteServerListStat, remoteServerListStatParams)
  415. if err != nil {
  416. return nil, common.ContextError(err)
  417. }
  418. // remote_server_list defaults to using the common params from the outer statusRequestParams
  419. remoteServerListFields := getRequestLogFields(
  420. "remote_server_list",
  421. geoIPData,
  422. authorizedAccessTypes,
  423. params,
  424. statusRequestParams)
  425. for name, value := range remoteServerListStat {
  426. remoteServerListFields[name] = value
  427. }
  428. logQueue = append(logQueue, remoteServerListFields)
  429. }
  430. }
  431. // Failed tunnel stats. Older clients may not submit this data.
  432. // Limitation: for "persistent" stats, geolocation is time-of-sending not time-of-recording.
  433. if statusData["failed_tunnel_stats"] != nil {
  434. failedTunnelStats, err := getJSONObjectArrayRequestParam(statusData, "failed_tunnel_stats")
  435. if err != nil {
  436. return nil, common.ContextError(err)
  437. }
  438. for _, failedTunnelStat := range failedTunnelStats {
  439. err := validateRequestParams(support.Config, failedTunnelStat, failedTunnelStatParams)
  440. if err != nil {
  441. return nil, common.ContextError(err)
  442. }
  443. failedTunnelFields := getRequestLogFields(
  444. "failed_tunnel",
  445. geoIPData,
  446. authorizedAccessTypes,
  447. failedTunnelStat,
  448. failedTunnelStatParams)
  449. logQueue = append(logQueue, failedTunnelFields)
  450. }
  451. }
  452. for _, logItem := range logQueue {
  453. log.LogRawFieldsWithTimestamp(logItem)
  454. }
  455. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  456. return make([]byte, pad_response), nil
  457. }
  458. // clientVerificationAPIRequestHandler is just a compliance stub
  459. // for older Android clients that still send verification requests
  460. func clientVerificationAPIRequestHandler(
  461. support *SupportServices,
  462. geoIPData GeoIPData,
  463. authorizedAccessTypes []string,
  464. params common.APIParameters) ([]byte, error) {
  465. return make([]byte, 0), nil
  466. }
  467. var tacticsParams = []requestParamSpec{
  468. {tactics.STORED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  469. {tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME, nil, requestParamOptional | requestParamJSON},
  470. }
  471. var tacticsRequestParams = append(
  472. append(
  473. []requestParamSpec{{"session_id", isHexDigits, 0}},
  474. tacticsParams...),
  475. baseRequestParams...)
  476. func getTacticsAPIParameterValidator(config *Config) common.APIParameterValidator {
  477. return func(params common.APIParameters) error {
  478. return validateRequestParams(config, params, tacticsRequestParams)
  479. }
  480. }
  481. func getTacticsAPIParameterLogFieldFormatter() common.APIParameterLogFieldFormatter {
  482. return func(geoIPData common.GeoIPData, params common.APIParameters) common.LogFields {
  483. logFields := getRequestLogFields(
  484. tactics.TACTICS_METRIC_EVENT_NAME,
  485. GeoIPData(geoIPData),
  486. nil, // authorizedAccessTypes are not known yet
  487. params,
  488. tacticsRequestParams)
  489. return common.LogFields(logFields)
  490. }
  491. }
  492. type requestParamSpec struct {
  493. name string
  494. validator func(*Config, string) bool
  495. flags uint32
  496. }
  497. const (
  498. requestParamOptional = 1
  499. requestParamNotLogged = 2
  500. requestParamArray = 4
  501. requestParamJSON = 8
  502. requestParamLogStringAsInt = 16
  503. requestParamLogStringLengthAsInt = 32
  504. requestParamLogFlagAsBool = 64
  505. )
  506. // baseRequestParams is the list of required and optional
  507. // request parameters; derived from COMMON_INPUTS and
  508. // OPTIONAL_COMMON_INPUTS in psi_web.
  509. // Each param is expected to be a string, unless requestParamArray
  510. // is specified, in which case an array of string is expected.
  511. var baseRequestParams = []requestParamSpec{
  512. {"server_secret", isServerSecret, requestParamNotLogged},
  513. {"client_session_id", isHexDigits, requestParamNotLogged},
  514. {"propagation_channel_id", isHexDigits, 0},
  515. {"sponsor_id", isHexDigits, 0},
  516. {"client_version", isIntString, requestParamLogStringAsInt},
  517. {"client_platform", isClientPlatform, 0},
  518. {"client_build_rev", isHexDigits, requestParamOptional},
  519. {"relay_protocol", isRelayProtocol, 0},
  520. {"tunnel_whole_device", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  521. {"device_region", isAnyString, requestParamOptional},
  522. {"ssh_client_version", isAnyString, requestParamOptional},
  523. {"upstream_proxy_type", isUpstreamProxyType, requestParamOptional},
  524. {"upstream_proxy_custom_header_names", isAnyString, requestParamOptional | requestParamArray},
  525. {"meek_dial_address", isDialAddress, requestParamOptional},
  526. {"meek_resolved_ip_address", isIPAddress, requestParamOptional},
  527. {"meek_sni_server_name", isDomain, requestParamOptional},
  528. {"meek_host_header", isHostHeader, requestParamOptional},
  529. {"meek_transformed_host_name", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  530. {"user_agent", isAnyString, requestParamOptional},
  531. {"tls_profile", isAnyString, requestParamOptional},
  532. {"server_entry_region", isRegionCode, requestParamOptional},
  533. {"server_entry_source", isServerEntrySource, requestParamOptional},
  534. {"server_entry_timestamp", isISO8601Date, requestParamOptional},
  535. {tactics.APPLIED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  536. {"dial_port_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  537. {"quic_version", isAnyString, requestParamOptional},
  538. {"quic_dial_sni_address", isAnyString, requestParamOptional},
  539. {"padding", isAnyString, requestParamOptional | requestParamLogStringLengthAsInt},
  540. {"pad_response", isIntString, requestParamOptional | requestParamLogStringAsInt},
  541. {"is_replay", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  542. {"egress_region", isRegionCode, requestParamOptional},
  543. {"dial_duration", isIntString, requestParamOptional | requestParamLogStringAsInt},
  544. {"upstream_bytes_fragmented", isIntString, requestParamOptional | requestParamLogStringAsInt},
  545. {"upstream_min_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  546. {"upstream_max_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  547. {"upstream_min_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  548. {"upstream_max_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  549. }
  550. func validateRequestParams(
  551. config *Config,
  552. params common.APIParameters,
  553. expectedParams []requestParamSpec) error {
  554. for _, expectedParam := range expectedParams {
  555. value := params[expectedParam.name]
  556. if value == nil {
  557. if expectedParam.flags&requestParamOptional != 0 {
  558. continue
  559. }
  560. return common.ContextError(
  561. fmt.Errorf("missing param: %s", expectedParam.name))
  562. }
  563. var err error
  564. switch {
  565. case expectedParam.flags&requestParamArray != 0:
  566. err = validateStringArrayRequestParam(config, expectedParam, value)
  567. case expectedParam.flags&requestParamJSON != 0:
  568. // No validation: the JSON already unmarshalled; the parameter
  569. // user will validate that the JSON contains the expected
  570. // objects/data.
  571. // TODO: without validation, any valid JSON will be logged
  572. // by getRequestLogFields, even if the parameter user validates
  573. // and rejects the parameter.
  574. default:
  575. err = validateStringRequestParam(config, expectedParam, value)
  576. }
  577. if err != nil {
  578. return common.ContextError(err)
  579. }
  580. }
  581. return nil
  582. }
  583. // copyBaseRequestParams makes a copy of the params which
  584. // includes only the baseRequestParams.
  585. func copyBaseRequestParams(params common.APIParameters) common.APIParameters {
  586. // Note: not a deep copy; assumes baseRequestParams values
  587. // are all scalar types (int, string, etc.)
  588. paramsCopy := make(common.APIParameters)
  589. for _, baseParam := range baseRequestParams {
  590. value := params[baseParam.name]
  591. if value == nil {
  592. continue
  593. }
  594. paramsCopy[baseParam.name] = value
  595. }
  596. return paramsCopy
  597. }
  598. func copyUpdateOnConnectedParams(params common.APIParameters) common.APIParameters {
  599. // Note: not a deep copy
  600. paramsCopy := make(common.APIParameters)
  601. for _, name := range updateOnConnectedParamNames {
  602. value := params[name]
  603. if value == nil {
  604. continue
  605. }
  606. paramsCopy[name] = value
  607. }
  608. return paramsCopy
  609. }
  610. func validateStringRequestParam(
  611. config *Config,
  612. expectedParam requestParamSpec,
  613. value interface{}) error {
  614. strValue, ok := value.(string)
  615. if !ok {
  616. return common.ContextError(
  617. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  618. }
  619. if !expectedParam.validator(config, strValue) {
  620. return common.ContextError(
  621. fmt.Errorf("invalid param: %s: %s", expectedParam.name, strValue))
  622. }
  623. return nil
  624. }
  625. func validateStringArrayRequestParam(
  626. config *Config,
  627. expectedParam requestParamSpec,
  628. value interface{}) error {
  629. arrayValue, ok := value.([]interface{})
  630. if !ok {
  631. return common.ContextError(
  632. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  633. }
  634. for _, value := range arrayValue {
  635. err := validateStringRequestParam(config, expectedParam, value)
  636. if err != nil {
  637. return common.ContextError(err)
  638. }
  639. }
  640. return nil
  641. }
  642. // getRequestLogFields makes LogFields to log the API event following
  643. // the legacy psi_web and current ELK naming conventions.
  644. func getRequestLogFields(
  645. eventName string,
  646. geoIPData GeoIPData,
  647. authorizedAccessTypes []string,
  648. params common.APIParameters,
  649. expectedParams []requestParamSpec) LogFields {
  650. logFields := make(LogFields)
  651. if eventName != "" {
  652. logFields["event_name"] = eventName
  653. }
  654. // In psi_web, the space replacement was done to accommodate space
  655. // delimited logging, which is no longer required; we retain the
  656. // transformation so that stats aggregation isn't impacted.
  657. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  658. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  659. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  660. if len(authorizedAccessTypes) > 0 {
  661. logFields["authorized_access_types"] = authorizedAccessTypes
  662. }
  663. if params == nil {
  664. return logFields
  665. }
  666. for _, expectedParam := range expectedParams {
  667. if expectedParam.flags&requestParamNotLogged != 0 {
  668. continue
  669. }
  670. value := params[expectedParam.name]
  671. if value == nil {
  672. // Special case: older clients don't send this value,
  673. // so log a default.
  674. if expectedParam.name == "tunnel_whole_device" {
  675. value = "0"
  676. } else {
  677. // Skip omitted, optional params
  678. continue
  679. }
  680. }
  681. switch v := value.(type) {
  682. case string:
  683. strValue := v
  684. // Special cases:
  685. // - Number fields are encoded as integer types.
  686. // - For ELK performance we record certain domain-or-IP
  687. // fields as one of two different values based on type;
  688. // we also omit port from these host:port fields for now.
  689. // - Boolean fields that come into the api as "1"/"0"
  690. // must be logged as actual boolean values
  691. switch expectedParam.name {
  692. case "meek_dial_address":
  693. host, _, _ := net.SplitHostPort(strValue)
  694. if isIPAddress(nil, host) {
  695. logFields["meek_dial_ip_address"] = host
  696. } else {
  697. logFields["meek_dial_domain"] = host
  698. }
  699. case "upstream_proxy_type":
  700. // Submitted value could be e.g., "SOCKS5" or "socks5"; log lowercase
  701. logFields[expectedParam.name] = strings.ToLower(strValue)
  702. case tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME:
  703. // Due to a client bug, clients may deliever an incorrect ""
  704. // value for speed_test_samples via the web API protocol. Omit
  705. // the field in this case.
  706. default:
  707. if expectedParam.flags&requestParamLogStringAsInt != 0 {
  708. intValue, _ := strconv.Atoi(strValue)
  709. logFields[expectedParam.name] = intValue
  710. } else if expectedParam.flags&requestParamLogStringLengthAsInt != 0 {
  711. logFields[expectedParam.name] = len(strValue)
  712. } else if expectedParam.flags&requestParamLogFlagAsBool != 0 {
  713. // Submitted value could be "0" or "1"
  714. // "0" and non "0"/"1" values should be transformed to false
  715. // "1" should be transformed to true
  716. if strValue == "1" {
  717. logFields[expectedParam.name] = true
  718. } else {
  719. logFields[expectedParam.name] = false
  720. }
  721. } else {
  722. logFields[expectedParam.name] = strValue
  723. }
  724. }
  725. case []interface{}:
  726. if expectedParam.name == tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME {
  727. logFields[expectedParam.name] = makeSpeedTestSamplesLogField(v)
  728. } else {
  729. logFields[expectedParam.name] = v
  730. }
  731. default:
  732. logFields[expectedParam.name] = v
  733. }
  734. }
  735. return logFields
  736. }
  737. // makeSpeedTestSamplesLogField renames the tactics.SpeedTestSample json tag
  738. // fields to more verbose names for metrics.
  739. func makeSpeedTestSamplesLogField(samples []interface{}) []interface{} {
  740. // TODO: use reflection and add additional tags, e.g.,
  741. // `json:"s" log:"timestamp"` to remove hard-coded
  742. // tag value dependency?
  743. logSamples := make([]interface{}, len(samples))
  744. for i, sample := range samples {
  745. logSample := make(map[string]interface{})
  746. if m, ok := sample.(map[string]interface{}); ok {
  747. for k, v := range m {
  748. logK := k
  749. switch k {
  750. case "s":
  751. logK = "timestamp"
  752. case "r":
  753. logK = "server_region"
  754. case "p":
  755. logK = "relay_protocol"
  756. case "t":
  757. logK = "round_trip_time_ms"
  758. case "u":
  759. logK = "bytes_up"
  760. case "d":
  761. logK = "bytes_down"
  762. }
  763. logSample[logK] = v
  764. }
  765. }
  766. logSamples[i] = logSample
  767. }
  768. return logSamples
  769. }
  770. func getStringRequestParam(params common.APIParameters, name string) (string, error) {
  771. if params[name] == nil {
  772. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  773. }
  774. value, ok := params[name].(string)
  775. if !ok {
  776. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  777. }
  778. return value, nil
  779. }
  780. func getInt64RequestParam(params common.APIParameters, name string) (int64, error) {
  781. if params[name] == nil {
  782. return 0, common.ContextError(fmt.Errorf("missing param: %s", name))
  783. }
  784. value, ok := params[name].(float64)
  785. if !ok {
  786. return 0, common.ContextError(fmt.Errorf("invalid param: %s", name))
  787. }
  788. return int64(value), nil
  789. }
  790. func getPaddingSizeRequestParam(params common.APIParameters, name string) (int, error) {
  791. value, err := getInt64RequestParam(params, name)
  792. if err != nil {
  793. return 0, common.ContextError(err)
  794. }
  795. if value < 0 {
  796. value = 0
  797. }
  798. if value > PADDING_MAX_BYTES {
  799. value = PADDING_MAX_BYTES
  800. }
  801. return int(value), nil
  802. }
  803. func getJSONObjectRequestParam(params common.APIParameters, name string) (common.APIParameters, error) {
  804. if params[name] == nil {
  805. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  806. }
  807. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  808. value, ok := params[name].(map[string]interface{})
  809. if !ok {
  810. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  811. }
  812. return common.APIParameters(value), nil
  813. }
  814. func getJSONObjectArrayRequestParam(params common.APIParameters, name string) ([]common.APIParameters, error) {
  815. if params[name] == nil {
  816. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  817. }
  818. value, ok := params[name].([]interface{})
  819. if !ok {
  820. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  821. }
  822. result := make([]common.APIParameters, len(value))
  823. for i, item := range value {
  824. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  825. resultItem, ok := item.(map[string]interface{})
  826. if !ok {
  827. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  828. }
  829. result[i] = common.APIParameters(resultItem)
  830. }
  831. return result, nil
  832. }
  833. func getMapStringInt64RequestParam(params common.APIParameters, name string) (map[string]int64, error) {
  834. if params[name] == nil {
  835. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  836. }
  837. // TODO: can't use common.APIParameters type?
  838. value, ok := params[name].(map[string]interface{})
  839. if !ok {
  840. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  841. }
  842. result := make(map[string]int64)
  843. for k, v := range value {
  844. numValue, ok := v.(float64)
  845. if !ok {
  846. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  847. }
  848. result[k] = int64(numValue)
  849. }
  850. return result, nil
  851. }
  852. func getStringArrayRequestParam(params common.APIParameters, name string) ([]string, error) {
  853. if params[name] == nil {
  854. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  855. }
  856. value, ok := params[name].([]interface{})
  857. if !ok {
  858. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  859. }
  860. result := make([]string, len(value))
  861. for i, v := range value {
  862. strValue, ok := v.(string)
  863. if !ok {
  864. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  865. }
  866. result[i] = strValue
  867. }
  868. return result, nil
  869. }
  870. // Normalize reported client platform. Android clients, for example, report
  871. // OS version, rooted status, and Google Play build status in the clientPlatform
  872. // string along with "Android".
  873. func normalizeClientPlatform(clientPlatform string) string {
  874. if strings.Contains(strings.ToLower(clientPlatform), strings.ToLower(CLIENT_PLATFORM_ANDROID)) {
  875. return CLIENT_PLATFORM_ANDROID
  876. } else if strings.HasPrefix(clientPlatform, CLIENT_PLATFORM_IOS) {
  877. return CLIENT_PLATFORM_IOS
  878. }
  879. return CLIENT_PLATFORM_WINDOWS
  880. }
  881. func isAnyString(config *Config, value string) bool {
  882. return true
  883. }
  884. func isMobileClientPlatform(clientPlatform string) bool {
  885. normalizedClientPlatform := normalizeClientPlatform(clientPlatform)
  886. return normalizedClientPlatform == CLIENT_PLATFORM_ANDROID ||
  887. normalizedClientPlatform == CLIENT_PLATFORM_IOS
  888. }
  889. // Input validators follow the legacy validations rules in psi_web.
  890. func isServerSecret(config *Config, value string) bool {
  891. return subtle.ConstantTimeCompare(
  892. []byte(value),
  893. []byte(config.WebServerSecret)) == 1
  894. }
  895. func isHexDigits(_ *Config, value string) bool {
  896. // Allows both uppercase in addition to lowercase, for legacy support.
  897. return -1 == strings.IndexFunc(value, func(c rune) bool {
  898. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  899. })
  900. }
  901. func isDigits(_ *Config, value string) bool {
  902. return -1 == strings.IndexFunc(value, func(c rune) bool {
  903. return c < '0' || c > '9'
  904. })
  905. }
  906. func isIntString(_ *Config, value string) bool {
  907. _, err := strconv.Atoi(value)
  908. return err == nil
  909. }
  910. func isClientPlatform(_ *Config, value string) bool {
  911. return -1 == strings.IndexFunc(value, func(c rune) bool {
  912. // Note: stricter than psi_web's Python string.whitespace
  913. return unicode.Is(unicode.White_Space, c)
  914. })
  915. }
  916. func isRelayProtocol(_ *Config, value string) bool {
  917. return common.Contains(protocol.SupportedTunnelProtocols, value)
  918. }
  919. func isBooleanFlag(_ *Config, value string) bool {
  920. return value == "0" || value == "1"
  921. }
  922. func isUpstreamProxyType(_ *Config, value string) bool {
  923. value = strings.ToLower(value)
  924. return value == "http" || value == "socks5" || value == "socks4a"
  925. }
  926. func isRegionCode(_ *Config, value string) bool {
  927. if len(value) != 2 {
  928. return false
  929. }
  930. return -1 == strings.IndexFunc(value, func(c rune) bool {
  931. return c < 'A' || c > 'Z'
  932. })
  933. }
  934. func isDialAddress(_ *Config, value string) bool {
  935. // "<host>:<port>", where <host> is a domain or IP address
  936. parts := strings.Split(value, ":")
  937. if len(parts) != 2 {
  938. return false
  939. }
  940. if !isIPAddress(nil, parts[0]) && !isDomain(nil, parts[0]) {
  941. return false
  942. }
  943. if !isDigits(nil, parts[1]) {
  944. return false
  945. }
  946. port, err := strconv.Atoi(parts[1])
  947. if err != nil {
  948. return false
  949. }
  950. return port > 0 && port < 65536
  951. }
  952. func isIPAddress(_ *Config, value string) bool {
  953. return net.ParseIP(value) != nil
  954. }
  955. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  956. func isDomain(_ *Config, value string) bool {
  957. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  958. //
  959. // "ensures that each segment
  960. // * contains at least one character and a maximum of 63 characters
  961. // * consists only of allowed characters
  962. // * doesn't begin or end with a hyphen"
  963. //
  964. if len(value) > 255 {
  965. return false
  966. }
  967. value = strings.TrimSuffix(value, ".")
  968. for _, part := range strings.Split(value, ".") {
  969. // Note: regexp doesn't support the following Perl expression which
  970. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  971. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  972. return false
  973. }
  974. if !isDomainRegex.Match([]byte(part)) {
  975. return false
  976. }
  977. }
  978. return true
  979. }
  980. func isHostHeader(_ *Config, value string) bool {
  981. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  982. if strings.Contains(value, ":") {
  983. return isDialAddress(nil, value)
  984. }
  985. return isIPAddress(nil, value) || isDomain(nil, value)
  986. }
  987. func isServerEntrySource(_ *Config, value string) bool {
  988. return common.Contains(protocol.SupportedServerEntrySources, value)
  989. }
  990. var isISO8601DateRegex = regexp.MustCompile(
  991. "(?P<year>[0-9]{4})-(?P<month>[0-9]{1,2})-(?P<day>[0-9]{1,2})T(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2}):(?P<second>[0-9]{2})(\\.(?P<fraction>[0-9]+))?(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))")
  992. func isISO8601Date(_ *Config, value string) bool {
  993. return isISO8601DateRegex.Match([]byte(value))
  994. }
  995. func isLastConnected(_ *Config, value string) bool {
  996. return value == "None" || value == "Unknown" || isISO8601Date(nil, value)
  997. }