api.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. "last_connected",
  281. "establishment_duration",
  282. "upstream_bytes_fragmented",
  283. "upstream_min_bytes_written",
  284. "upstream_max_bytes_written",
  285. "upstream_min_delayed",
  286. "upstream_max_delayed",
  287. }
  288. // connectedAPIRequestHandler implements the "connected" API request.
  289. // Clients make the connected request once a tunnel connection has been
  290. // established and at least once per day. The last_connected input value,
  291. // which should be a connected_timestamp output from a previous connected
  292. // response, is used to calculate unique user stats.
  293. // connected_timestamp is truncated as a privacy measure.
  294. func connectedAPIRequestHandler(
  295. support *SupportServices,
  296. geoIPData GeoIPData,
  297. authorizedAccessTypes []string,
  298. params common.APIParameters) ([]byte, error) {
  299. err := validateRequestParams(support.Config, params, connectedRequestParams)
  300. if err != nil {
  301. return nil, common.ContextError(err)
  302. }
  303. // Update, for server_tunnel logging, upstream fragmentor metrics, as the
  304. // client may have performed more upstream fragmentation since the
  305. // previous metrics reported by the handshake request. Also, additional
  306. // fields reported only in the connected request, are added to
  307. // server_tunnel here.
  308. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  309. // applies here.
  310. sessionID, _ := getStringRequestParam(params, "client_session_id")
  311. err = support.TunnelServer.UpdateClientAPIParameters(
  312. sessionID, copyUpdateOnConnectedParams(params))
  313. if err != nil {
  314. return nil, common.ContextError(err)
  315. }
  316. log.LogRawFieldsWithTimestamp(
  317. getRequestLogFields(
  318. "connected",
  319. geoIPData,
  320. authorizedAccessTypes,
  321. params,
  322. connectedRequestParams))
  323. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  324. connectedResponse := protocol.ConnectedResponse{
  325. ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
  326. Padding: strings.Repeat(" ", pad_response),
  327. }
  328. responsePayload, err := json.Marshal(connectedResponse)
  329. if err != nil {
  330. return nil, common.ContextError(err)
  331. }
  332. return responsePayload, nil
  333. }
  334. var statusRequestParams = append(
  335. []requestParamSpec{
  336. {"session_id", isHexDigits, 0},
  337. {"connected", isBooleanFlag, requestParamLogFlagAsBool}},
  338. baseRequestParams...)
  339. var remoteServerListStatParams = []requestParamSpec{
  340. {"session_id", isHexDigits, requestParamOptional},
  341. {"propagation_channel_id", isHexDigits, requestParamOptional},
  342. {"sponsor_id", isHexDigits, requestParamOptional},
  343. {"client_version", isAnyString, requestParamOptional},
  344. {"client_platform", isAnyString, requestParamOptional},
  345. {"client_build_rev", isAnyString, requestParamOptional},
  346. {"client_download_timestamp", isISO8601Date, 0},
  347. {"url", isAnyString, 0},
  348. {"etag", isAnyString, 0},
  349. }
  350. var failedTunnelStatParams = append(
  351. []requestParamSpec{
  352. {"server_entry_ip_address", isIPAddress, requestParamNotLogged},
  353. {"session_id", isHexDigits, 0},
  354. {"last_connected", isLastConnected, 0},
  355. {"client_failed_timestamp", isISO8601Date, 0},
  356. {"tunnel_error", isAnyString, 0}},
  357. baseRequestParams...)
  358. // statusAPIRequestHandler implements the "status" API request.
  359. // Clients make periodic status requests which deliver client-side
  360. // recorded data transfer and tunnel duration stats.
  361. // Note from psi_web implementation: no input validation on domains;
  362. // any string is accepted (regex transform may result in arbitrary
  363. // string). Stats processor must handle this input with care.
  364. func statusAPIRequestHandler(
  365. support *SupportServices,
  366. geoIPData GeoIPData,
  367. authorizedAccessTypes []string,
  368. params common.APIParameters) ([]byte, error) {
  369. err := validateRequestParams(support.Config, params, statusRequestParams)
  370. if err != nil {
  371. return nil, common.ContextError(err)
  372. }
  373. sessionID, _ := getStringRequestParam(params, "client_session_id")
  374. statusData, err := getJSONObjectRequestParam(params, "statusData")
  375. if err != nil {
  376. return nil, common.ContextError(err)
  377. }
  378. // Logs are queued until the input is fully validated. Otherwise, stats
  379. // could be double counted if the client has a bug in its request
  380. // formatting: partial stats would be logged (counted), the request would
  381. // fail, and clients would then resend all the same stats again.
  382. logQueue := make([]LogFields, 0)
  383. // Domain bytes transferred stats
  384. // Older clients may not submit this data
  385. // Clients are expected to send host_bytes/domain_bytes stats only when
  386. // configured to do so in the handshake reponse. Legacy clients may still
  387. // report "(OTHER)" host_bytes when no regexes are set. Drop those stats.
  388. domainBytesExpected, err := support.TunnelServer.ExpectClientDomainBytes(sessionID)
  389. if err != nil {
  390. return nil, common.ContextError(err)
  391. }
  392. if domainBytesExpected && statusData["host_bytes"] != nil {
  393. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  394. if err != nil {
  395. return nil, common.ContextError(err)
  396. }
  397. for domain, bytes := range hostBytes {
  398. domainBytesFields := getRequestLogFields(
  399. "domain_bytes",
  400. geoIPData,
  401. authorizedAccessTypes,
  402. params,
  403. statusRequestParams)
  404. domainBytesFields["domain"] = domain
  405. domainBytesFields["bytes"] = bytes
  406. logQueue = append(logQueue, domainBytesFields)
  407. }
  408. }
  409. // Limitation: for "persistent" stats, host_id and geolocation is time-of-sending
  410. // not time-of-recording.
  411. // Remote server list download persistent stats.
  412. // Older clients may not submit this data.
  413. if statusData["remote_server_list_stats"] != nil {
  414. remoteServerListStats, err := getJSONObjectArrayRequestParam(statusData, "remote_server_list_stats")
  415. if err != nil {
  416. return nil, common.ContextError(err)
  417. }
  418. for _, remoteServerListStat := range remoteServerListStats {
  419. err := validateRequestParams(support.Config, remoteServerListStat, remoteServerListStatParams)
  420. if err != nil {
  421. return nil, common.ContextError(err)
  422. }
  423. // remote_server_list defaults to using the common params from the
  424. // outer statusRequestParams
  425. remoteServerListFields := getRequestLogFields(
  426. "remote_server_list",
  427. geoIPData,
  428. authorizedAccessTypes,
  429. params,
  430. statusRequestParams)
  431. for name, value := range remoteServerListStat {
  432. remoteServerListFields[name] = value
  433. }
  434. logQueue = append(logQueue, remoteServerListFields)
  435. }
  436. }
  437. // Failed tunnel persistent stats.
  438. // Older clients may not submit this data.
  439. if statusData["failed_tunnel_stats"] != nil {
  440. failedTunnelStats, err := getJSONObjectArrayRequestParam(statusData, "failed_tunnel_stats")
  441. if err != nil {
  442. return nil, common.ContextError(err)
  443. }
  444. for _, failedTunnelStat := range failedTunnelStats {
  445. // failed_tunnel supplies a full set of common params, but the
  446. // server secret must use the corect value from the outer
  447. // statusRequestParams
  448. failedTunnelStat["server_secret"] = params["server_secret"]
  449. err := validateRequestParams(support.Config, failedTunnelStat, failedTunnelStatParams)
  450. if err != nil {
  451. return nil, common.ContextError(err)
  452. }
  453. failedTunnelFields := getRequestLogFields(
  454. "failed_tunnel",
  455. geoIPData,
  456. authorizedAccessTypes,
  457. failedTunnelStat,
  458. failedTunnelStatParams)
  459. logQueue = append(logQueue, failedTunnelFields)
  460. }
  461. }
  462. for _, logItem := range logQueue {
  463. log.LogRawFieldsWithTimestamp(logItem)
  464. }
  465. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  466. return make([]byte, pad_response), nil
  467. }
  468. // clientVerificationAPIRequestHandler is just a compliance stub
  469. // for older Android clients that still send verification requests
  470. func clientVerificationAPIRequestHandler(
  471. support *SupportServices,
  472. geoIPData GeoIPData,
  473. authorizedAccessTypes []string,
  474. params common.APIParameters) ([]byte, error) {
  475. return make([]byte, 0), nil
  476. }
  477. var tacticsParams = []requestParamSpec{
  478. {tactics.STORED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  479. {tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME, nil, requestParamOptional | requestParamJSON},
  480. }
  481. var tacticsRequestParams = append(
  482. append(
  483. []requestParamSpec{{"session_id", isHexDigits, 0}},
  484. tacticsParams...),
  485. baseRequestParams...)
  486. func getTacticsAPIParameterValidator(config *Config) common.APIParameterValidator {
  487. return func(params common.APIParameters) error {
  488. return validateRequestParams(config, params, tacticsRequestParams)
  489. }
  490. }
  491. func getTacticsAPIParameterLogFieldFormatter() common.APIParameterLogFieldFormatter {
  492. return func(geoIPData common.GeoIPData, params common.APIParameters) common.LogFields {
  493. logFields := getRequestLogFields(
  494. tactics.TACTICS_METRIC_EVENT_NAME,
  495. GeoIPData(geoIPData),
  496. nil, // authorizedAccessTypes are not known yet
  497. params,
  498. tacticsRequestParams)
  499. return common.LogFields(logFields)
  500. }
  501. }
  502. type requestParamSpec struct {
  503. name string
  504. validator func(*Config, string) bool
  505. flags uint32
  506. }
  507. const (
  508. requestParamOptional = 1
  509. requestParamNotLogged = 1 << 1
  510. requestParamArray = 1 << 2
  511. requestParamJSON = 1 << 3
  512. requestParamLogStringAsInt = 1 << 4
  513. requestParamLogStringLengthAsInt = 1 << 5
  514. requestParamLogFlagAsBool = 1 << 6
  515. requestParamLogOnlyForFrontedMeek = 1 << 7
  516. requestParamNotLoggedForUnfrontedMeekNonTransformedHeader = 1 << 8
  517. )
  518. // baseRequestParams is the list of required and optional
  519. // request parameters; derived from COMMON_INPUTS and
  520. // OPTIONAL_COMMON_INPUTS in psi_web.
  521. // Each param is expected to be a string, unless requestParamArray
  522. // is specified, in which case an array of string is expected.
  523. var baseRequestParams = []requestParamSpec{
  524. {"server_secret", isServerSecret, requestParamNotLogged},
  525. {"client_session_id", isHexDigits, requestParamNotLogged},
  526. {"propagation_channel_id", isHexDigits, 0},
  527. {"sponsor_id", isHexDigits, 0},
  528. {"client_version", isIntString, requestParamLogStringAsInt},
  529. {"client_platform", isClientPlatform, 0},
  530. {"client_build_rev", isHexDigits, requestParamOptional},
  531. {"relay_protocol", isRelayProtocol, 0},
  532. {"tunnel_whole_device", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  533. {"device_region", isAnyString, requestParamOptional},
  534. {"ssh_client_version", isAnyString, requestParamOptional},
  535. {"upstream_proxy_type", isUpstreamProxyType, requestParamOptional},
  536. {"upstream_proxy_custom_header_names", isAnyString, requestParamOptional | requestParamArray},
  537. {"meek_dial_address", isDialAddress, requestParamOptional | requestParamLogOnlyForFrontedMeek},
  538. {"meek_resolved_ip_address", isIPAddress, requestParamOptional | requestParamLogOnlyForFrontedMeek},
  539. {"meek_sni_server_name", isDomain, requestParamOptional},
  540. {"meek_host_header", isHostHeader, requestParamOptional | requestParamNotLoggedForUnfrontedMeekNonTransformedHeader},
  541. {"meek_transformed_host_name", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  542. {"user_agent", isAnyString, requestParamOptional},
  543. {"tls_profile", isAnyString, requestParamOptional},
  544. {"tls_version", isAnyString, requestParamOptional},
  545. {"server_entry_region", isRegionCode, requestParamOptional},
  546. {"server_entry_source", isServerEntrySource, requestParamOptional},
  547. {"server_entry_timestamp", isISO8601Date, requestParamOptional},
  548. {tactics.APPLIED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  549. {"dial_port_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  550. {"quic_version", isAnyString, requestParamOptional},
  551. {"quic_dial_sni_address", isAnyString, requestParamOptional},
  552. {"upstream_bytes_fragmented", isIntString, requestParamOptional | requestParamLogStringAsInt},
  553. {"upstream_min_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  554. {"upstream_max_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  555. {"upstream_min_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  556. {"upstream_max_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  557. {"padding", isAnyString, requestParamOptional | requestParamLogStringLengthAsInt},
  558. {"pad_response", isIntString, requestParamOptional | requestParamLogStringAsInt},
  559. {"is_replay", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  560. {"egress_region", isRegionCode, requestParamOptional},
  561. {"dial_duration", isIntString, requestParamOptional | requestParamLogStringAsInt},
  562. {"candidate_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  563. {"upstream_ossh_padding", isIntString, requestParamOptional | requestParamLogStringAsInt},
  564. {"meek_cookie_size", isIntString, requestParamOptional | requestParamLogStringAsInt},
  565. {"meek_limit_request", isIntString, requestParamOptional | requestParamLogStringAsInt},
  566. }
  567. func validateRequestParams(
  568. config *Config,
  569. params common.APIParameters,
  570. expectedParams []requestParamSpec) error {
  571. for _, expectedParam := range expectedParams {
  572. value := params[expectedParam.name]
  573. if value == nil {
  574. if expectedParam.flags&requestParamOptional != 0 {
  575. continue
  576. }
  577. return common.ContextError(
  578. fmt.Errorf("missing param: %s", expectedParam.name))
  579. }
  580. var err error
  581. switch {
  582. case expectedParam.flags&requestParamArray != 0:
  583. err = validateStringArrayRequestParam(config, expectedParam, value)
  584. case expectedParam.flags&requestParamJSON != 0:
  585. // No validation: the JSON already unmarshalled; the parameter
  586. // user will validate that the JSON contains the expected
  587. // objects/data.
  588. // TODO: without validation, any valid JSON will be logged
  589. // by getRequestLogFields, even if the parameter user validates
  590. // and rejects the parameter.
  591. default:
  592. err = validateStringRequestParam(config, expectedParam, value)
  593. }
  594. if err != nil {
  595. return common.ContextError(err)
  596. }
  597. }
  598. return nil
  599. }
  600. // copyBaseRequestParams makes a copy of the params which
  601. // includes only the baseRequestParams.
  602. func copyBaseRequestParams(params common.APIParameters) common.APIParameters {
  603. // Note: not a deep copy; assumes baseRequestParams values
  604. // are all scalar types (int, string, etc.)
  605. paramsCopy := make(common.APIParameters)
  606. for _, baseParam := range baseRequestParams {
  607. value := params[baseParam.name]
  608. if value == nil {
  609. continue
  610. }
  611. paramsCopy[baseParam.name] = value
  612. }
  613. return paramsCopy
  614. }
  615. func copyUpdateOnConnectedParams(params common.APIParameters) common.APIParameters {
  616. // Note: not a deep copy
  617. paramsCopy := make(common.APIParameters)
  618. for _, name := range updateOnConnectedParamNames {
  619. value := params[name]
  620. if value == nil {
  621. continue
  622. }
  623. paramsCopy[name] = value
  624. }
  625. return paramsCopy
  626. }
  627. func validateStringRequestParam(
  628. config *Config,
  629. expectedParam requestParamSpec,
  630. value interface{}) error {
  631. strValue, ok := value.(string)
  632. if !ok {
  633. return common.ContextError(
  634. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  635. }
  636. if !expectedParam.validator(config, strValue) {
  637. return common.ContextError(
  638. fmt.Errorf("invalid param: %s: %s", expectedParam.name, strValue))
  639. }
  640. return nil
  641. }
  642. func validateStringArrayRequestParam(
  643. config *Config,
  644. expectedParam requestParamSpec,
  645. value interface{}) error {
  646. arrayValue, ok := value.([]interface{})
  647. if !ok {
  648. return common.ContextError(
  649. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  650. }
  651. for _, value := range arrayValue {
  652. err := validateStringRequestParam(config, expectedParam, value)
  653. if err != nil {
  654. return common.ContextError(err)
  655. }
  656. }
  657. return nil
  658. }
  659. // getRequestLogFields makes LogFields to log the API event following
  660. // the legacy psi_web and current ELK naming conventions.
  661. func getRequestLogFields(
  662. eventName string,
  663. geoIPData GeoIPData,
  664. authorizedAccessTypes []string,
  665. params common.APIParameters,
  666. expectedParams []requestParamSpec) LogFields {
  667. logFields := make(LogFields)
  668. if eventName != "" {
  669. logFields["event_name"] = eventName
  670. }
  671. // In psi_web, the space replacement was done to accommodate space
  672. // delimited logging, which is no longer required; we retain the
  673. // transformation so that stats aggregation isn't impacted.
  674. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  675. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  676. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  677. if len(authorizedAccessTypes) > 0 {
  678. logFields["authorized_access_types"] = authorizedAccessTypes
  679. }
  680. if params == nil {
  681. return logFields
  682. }
  683. for _, expectedParam := range expectedParams {
  684. if expectedParam.flags&requestParamNotLogged != 0 {
  685. continue
  686. }
  687. var tunnelProtocol string
  688. if value, ok := params["relay_protocol"]; ok {
  689. tunnelProtocol, _ = value.(string)
  690. }
  691. if expectedParam.flags&requestParamLogOnlyForFrontedMeek != 0 &&
  692. !protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  693. continue
  694. }
  695. if expectedParam.flags&requestParamNotLoggedForUnfrontedMeekNonTransformedHeader != 0 &&
  696. protocol.TunnelProtocolUsesMeek(tunnelProtocol) &&
  697. !protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  698. // Non-HTTP unfronted meek protocols never tranform the host header.
  699. if protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  700. continue
  701. }
  702. var transformedHostName string
  703. if value, ok := params["meek_transformed_host_name"]; ok {
  704. transformedHostName, _ = value.(string)
  705. }
  706. if transformedHostName != "1" {
  707. continue
  708. }
  709. }
  710. value := params[expectedParam.name]
  711. if value == nil {
  712. // Special case: older clients don't send this value,
  713. // so log a default.
  714. if expectedParam.name == "tunnel_whole_device" {
  715. value = "0"
  716. } else {
  717. // Skip omitted, optional params
  718. continue
  719. }
  720. }
  721. switch v := value.(type) {
  722. case string:
  723. strValue := v
  724. // Special cases:
  725. // - Number fields are encoded as integer types.
  726. // - For ELK performance we record certain domain-or-IP
  727. // fields as one of two different values based on type;
  728. // we also omit port from these host:port fields for now.
  729. // - Boolean fields that come into the api as "1"/"0"
  730. // must be logged as actual boolean values
  731. switch expectedParam.name {
  732. case "meek_dial_address":
  733. host, _, _ := net.SplitHostPort(strValue)
  734. if isIPAddress(nil, host) {
  735. logFields["meek_dial_ip_address"] = host
  736. } else {
  737. logFields["meek_dial_domain"] = host
  738. }
  739. case "upstream_proxy_type":
  740. // Submitted value could be e.g., "SOCKS5" or "socks5"; log lowercase
  741. logFields[expectedParam.name] = strings.ToLower(strValue)
  742. case tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME:
  743. // Due to a client bug, clients may deliever an incorrect ""
  744. // value for speed_test_samples via the web API protocol. Omit
  745. // the field in this case.
  746. default:
  747. if expectedParam.flags&requestParamLogStringAsInt != 0 {
  748. intValue, _ := strconv.Atoi(strValue)
  749. logFields[expectedParam.name] = intValue
  750. } else if expectedParam.flags&requestParamLogStringLengthAsInt != 0 {
  751. logFields[expectedParam.name] = len(strValue)
  752. } else if expectedParam.flags&requestParamLogFlagAsBool != 0 {
  753. // Submitted value could be "0" or "1"
  754. // "0" and non "0"/"1" values should be transformed to false
  755. // "1" should be transformed to true
  756. if strValue == "1" {
  757. logFields[expectedParam.name] = true
  758. } else {
  759. logFields[expectedParam.name] = false
  760. }
  761. } else {
  762. logFields[expectedParam.name] = strValue
  763. }
  764. }
  765. case []interface{}:
  766. if expectedParam.name == tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME {
  767. logFields[expectedParam.name] = makeSpeedTestSamplesLogField(v)
  768. } else {
  769. logFields[expectedParam.name] = v
  770. }
  771. default:
  772. logFields[expectedParam.name] = v
  773. }
  774. }
  775. return logFields
  776. }
  777. // makeSpeedTestSamplesLogField renames the tactics.SpeedTestSample json tag
  778. // fields to more verbose names for metrics.
  779. func makeSpeedTestSamplesLogField(samples []interface{}) []interface{} {
  780. // TODO: use reflection and add additional tags, e.g.,
  781. // `json:"s" log:"timestamp"` to remove hard-coded
  782. // tag value dependency?
  783. logSamples := make([]interface{}, len(samples))
  784. for i, sample := range samples {
  785. logSample := make(map[string]interface{})
  786. if m, ok := sample.(map[string]interface{}); ok {
  787. for k, v := range m {
  788. logK := k
  789. switch k {
  790. case "s":
  791. logK = "timestamp"
  792. case "r":
  793. logK = "server_region"
  794. case "p":
  795. logK = "relay_protocol"
  796. case "t":
  797. logK = "round_trip_time_ms"
  798. case "u":
  799. logK = "bytes_up"
  800. case "d":
  801. logK = "bytes_down"
  802. }
  803. logSample[logK] = v
  804. }
  805. }
  806. logSamples[i] = logSample
  807. }
  808. return logSamples
  809. }
  810. func getStringRequestParam(params common.APIParameters, name string) (string, error) {
  811. if params[name] == nil {
  812. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  813. }
  814. value, ok := params[name].(string)
  815. if !ok {
  816. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  817. }
  818. return value, nil
  819. }
  820. func getInt64RequestParam(params common.APIParameters, name string) (int64, error) {
  821. if params[name] == nil {
  822. return 0, common.ContextError(fmt.Errorf("missing param: %s", name))
  823. }
  824. value, ok := params[name].(float64)
  825. if !ok {
  826. return 0, common.ContextError(fmt.Errorf("invalid param: %s", name))
  827. }
  828. return int64(value), nil
  829. }
  830. func getPaddingSizeRequestParam(params common.APIParameters, name string) (int, error) {
  831. value, err := getInt64RequestParam(params, name)
  832. if err != nil {
  833. return 0, common.ContextError(err)
  834. }
  835. if value < 0 {
  836. value = 0
  837. }
  838. if value > PADDING_MAX_BYTES {
  839. value = PADDING_MAX_BYTES
  840. }
  841. return int(value), nil
  842. }
  843. func getJSONObjectRequestParam(params common.APIParameters, name string) (common.APIParameters, error) {
  844. if params[name] == nil {
  845. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  846. }
  847. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  848. value, ok := params[name].(map[string]interface{})
  849. if !ok {
  850. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  851. }
  852. return common.APIParameters(value), nil
  853. }
  854. func getJSONObjectArrayRequestParam(params common.APIParameters, name string) ([]common.APIParameters, error) {
  855. if params[name] == nil {
  856. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  857. }
  858. value, ok := params[name].([]interface{})
  859. if !ok {
  860. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  861. }
  862. result := make([]common.APIParameters, len(value))
  863. for i, item := range value {
  864. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  865. resultItem, ok := item.(map[string]interface{})
  866. if !ok {
  867. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  868. }
  869. result[i] = common.APIParameters(resultItem)
  870. }
  871. return result, nil
  872. }
  873. func getMapStringInt64RequestParam(params common.APIParameters, name string) (map[string]int64, error) {
  874. if params[name] == nil {
  875. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  876. }
  877. // TODO: can't use common.APIParameters type?
  878. value, ok := params[name].(map[string]interface{})
  879. if !ok {
  880. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  881. }
  882. result := make(map[string]int64)
  883. for k, v := range value {
  884. numValue, ok := v.(float64)
  885. if !ok {
  886. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  887. }
  888. result[k] = int64(numValue)
  889. }
  890. return result, nil
  891. }
  892. func getStringArrayRequestParam(params common.APIParameters, name string) ([]string, error) {
  893. if params[name] == nil {
  894. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  895. }
  896. value, ok := params[name].([]interface{})
  897. if !ok {
  898. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  899. }
  900. result := make([]string, len(value))
  901. for i, v := range value {
  902. strValue, ok := v.(string)
  903. if !ok {
  904. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  905. }
  906. result[i] = strValue
  907. }
  908. return result, nil
  909. }
  910. // Normalize reported client platform. Android clients, for example, report
  911. // OS version, rooted status, and Google Play build status in the clientPlatform
  912. // string along with "Android".
  913. func normalizeClientPlatform(clientPlatform string) string {
  914. if strings.Contains(strings.ToLower(clientPlatform), strings.ToLower(CLIENT_PLATFORM_ANDROID)) {
  915. return CLIENT_PLATFORM_ANDROID
  916. } else if strings.HasPrefix(clientPlatform, CLIENT_PLATFORM_IOS) {
  917. return CLIENT_PLATFORM_IOS
  918. }
  919. return CLIENT_PLATFORM_WINDOWS
  920. }
  921. func isAnyString(config *Config, value string) bool {
  922. return true
  923. }
  924. func isMobileClientPlatform(clientPlatform string) bool {
  925. normalizedClientPlatform := normalizeClientPlatform(clientPlatform)
  926. return normalizedClientPlatform == CLIENT_PLATFORM_ANDROID ||
  927. normalizedClientPlatform == CLIENT_PLATFORM_IOS
  928. }
  929. // Input validators follow the legacy validations rules in psi_web.
  930. func isServerSecret(config *Config, value string) bool {
  931. return subtle.ConstantTimeCompare(
  932. []byte(value),
  933. []byte(config.WebServerSecret)) == 1
  934. }
  935. func isHexDigits(_ *Config, value string) bool {
  936. // Allows both uppercase in addition to lowercase, for legacy support.
  937. return -1 == strings.IndexFunc(value, func(c rune) bool {
  938. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  939. })
  940. }
  941. func isDigits(_ *Config, value string) bool {
  942. return -1 == strings.IndexFunc(value, func(c rune) bool {
  943. return c < '0' || c > '9'
  944. })
  945. }
  946. func isIntString(_ *Config, value string) bool {
  947. _, err := strconv.Atoi(value)
  948. return err == nil
  949. }
  950. func isClientPlatform(_ *Config, value string) bool {
  951. return -1 == strings.IndexFunc(value, func(c rune) bool {
  952. // Note: stricter than psi_web's Python string.whitespace
  953. return unicode.Is(unicode.White_Space, c)
  954. })
  955. }
  956. func isRelayProtocol(_ *Config, value string) bool {
  957. return common.Contains(protocol.SupportedTunnelProtocols, value)
  958. }
  959. func isBooleanFlag(_ *Config, value string) bool {
  960. return value == "0" || value == "1"
  961. }
  962. func isUpstreamProxyType(_ *Config, value string) bool {
  963. value = strings.ToLower(value)
  964. return value == "http" || value == "socks5" || value == "socks4a"
  965. }
  966. func isRegionCode(_ *Config, value string) bool {
  967. if len(value) != 2 {
  968. return false
  969. }
  970. return -1 == strings.IndexFunc(value, func(c rune) bool {
  971. return c < 'A' || c > 'Z'
  972. })
  973. }
  974. func isDialAddress(_ *Config, value string) bool {
  975. // "<host>:<port>", where <host> is a domain or IP address
  976. parts := strings.Split(value, ":")
  977. if len(parts) != 2 {
  978. return false
  979. }
  980. if !isIPAddress(nil, parts[0]) && !isDomain(nil, parts[0]) {
  981. return false
  982. }
  983. if !isDigits(nil, parts[1]) {
  984. return false
  985. }
  986. port, err := strconv.Atoi(parts[1])
  987. if err != nil {
  988. return false
  989. }
  990. return port > 0 && port < 65536
  991. }
  992. func isIPAddress(_ *Config, value string) bool {
  993. return net.ParseIP(value) != nil
  994. }
  995. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  996. func isDomain(_ *Config, value string) bool {
  997. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  998. //
  999. // "ensures that each segment
  1000. // * contains at least one character and a maximum of 63 characters
  1001. // * consists only of allowed characters
  1002. // * doesn't begin or end with a hyphen"
  1003. //
  1004. if len(value) > 255 {
  1005. return false
  1006. }
  1007. value = strings.TrimSuffix(value, ".")
  1008. for _, part := range strings.Split(value, ".") {
  1009. // Note: regexp doesn't support the following Perl expression which
  1010. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  1011. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  1012. return false
  1013. }
  1014. if !isDomainRegex.Match([]byte(part)) {
  1015. return false
  1016. }
  1017. }
  1018. return true
  1019. }
  1020. func isHostHeader(_ *Config, value string) bool {
  1021. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  1022. if strings.Contains(value, ":") {
  1023. return isDialAddress(nil, value)
  1024. }
  1025. return isIPAddress(nil, value) || isDomain(nil, value)
  1026. }
  1027. func isServerEntrySource(_ *Config, value string) bool {
  1028. return common.Contains(protocol.SupportedServerEntrySources, value)
  1029. }
  1030. var isISO8601DateRegex = regexp.MustCompile(
  1031. "(?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})))")
  1032. func isISO8601Date(_ *Config, value string) bool {
  1033. return isISO8601DateRegex.Match([]byte(value))
  1034. }
  1035. func isLastConnected(_ *Config, value string) bool {
  1036. return value == "None" || value == "Unknown" || isISO8601Date(nil, value)
  1037. }