Commit Graph
35 Commits
Author SHA1 Message Date
Facundo Menzella 6b5c430082 chore: Enable force_unwrapping for SwiftLint (#4820)
* enable force_unwrapping

* avoid force unwrap

* fixed all errors

* remove extra disable
2025-02-26 12:40:49 +01:00
Toni Rico 0a5e2aa8ce [Trusted Entitlements] Enable Trusted Entitlements by default (#4672)
* [Trusted Entitlements] Enable Trusted Entitlements by default

* Disable response verification during tests by default
2025-01-16 16:49:00 +01:00
NachoSoto 2bcd0ce328 5.0.0 changes: support SK2 by default 2024-07-01 14:37:21 -05:00
Cesar de la Vega d5f4f487ed Add name to VerificationResult (#3792)
Needed for Diagnostics
2024-04-10 13:03:32 +02:00
NachoSoto 1cea9978aa Trusted Entitlements: tests for signature verification without header hash (#3505)
Follow up to #3424. This adds coverage to ensure that the backend
continues to sign correctly for old SDK versions that don't support
this.
2023-12-21 12:47:34 -08:00
NachoSoto fb76e662e8 Trusted Entitlements: add support for signing request headers (#3424)
This adds support for including arbitrary headers in the signature
verification, therefore preventing tampering of them.
2023-12-21 11:53:10 -08:00
NachoSoto ed80980d08 HTTPClient: verbose logs for request IDs (#3320)
See https://github.com/RevenueCat/khepri/pull/6798.
This can help debug issues as it helps us cross-reference requests on
our end.

### Changes:
- Logging `X-Request-ID` and `X-Amzn-Trace-ID` headers if present (only
in verbose mode)
- Logging these even for failed requests
- Also logging `request_handled_by_load_shedder` for failed requests
- To accomplish that. `HTTPURLResponse` now conforms to our
`HTTPResponseType`, this allows us to extract headers in an uniform way

### Example:
```
[network] DEBUG: ℹ️ API request completed: GET '/v1/subscribers/$RCAnonymousID%3Ad9dc4f3b33054cf28c0f2ec9da0f48d5' (304)
Request-ID: 'be534c50-5231-403b-be2f-7c077861751d'; Amzn-Trace-ID: 'Root=1-6532c6ca-7ef80f961ec9c4145484df13'
```
2023-10-25 20:24:23 -07:00
NachoSoto 833524314d HTTPClient: extracted HTTPRequestPath protocol (#2986)
This paves the way to add a second set of paths with a separate server
URL to be able to shed requests to offerings and product entitlement
mapping to a separate server.

### Changes:
- New `HTTPRequestPath` with the same properties as `HTTPRequest.Path` +
a new `serverHostURL`
- Moved proxy logic from `SystemInfo`/`HTTPClient` to `HTTPRequestPath`
- Simplified `HTTPRequest` implementation
2023-08-10 15:23:13 -07:00
NachoSoto f7e36fb993 Trusted Entitlements: add support for signing POST body (#2753)
### Changes:
- `HTTPClient` has a new `X-Post-Params-Hash` request header
- Replaced `HTTPRequest.Method.post`'s `Encodable` value with a more
specific `HTTPRequestBody` (like `HTTPResponseBody`)
- `HTTPRequestBody` allows overriding `contentForSignature` (it defaults
to `[]`) which then `Signing` uses for the hash
- Fixed `LogInOperation` not passing the `VerificationResult` to the
result `CustomerInfo`. This is captured in unit and integration tests
now.
- Add support for signing `POST` requests in `Path.logIn` and
`Path.postReceiptData`

Depends on https://github.com/RevenueCat/khepri/pull/6246
2023-07-13 07:45:16 -07:00
NachoSoto be5ce74cd5 TrustedEntitlements: new VerificationResult.isVerified (#2788)
The documentation was slightly updated, because we also need to accept
`.verifiedOnDevice`.
2023-07-11 11:03:14 -07:00
NachoSoto 460f6fd6da Trusted Entitlements: improved documentation (#2782)
I've also added a link to our new documentation page:
https://rev.cat/trusted-entitlements

![Screenshot 2023-07-11 at 08 47
19](https://github.com/RevenueCat/purchases-ios/assets/685609/1f7cfdd9-7806-4f78-8630-ec860146ac73)
2023-07-11 09:27:20 -07:00
NachoSoto 1cab844f2b Trusted Entitlements: re-enable public API (#2621) 2023-07-11 07:34:39 -07:00
NachoSoto 007f87588f Signing: inject ClockType to ensure hardcoded signatures don't fail when intermediate key expires (#2771)
Thanks to @aboedo for catching this and providing this simple idea.
2023-07-10 08:30:55 -07:00
NachoSoto 068bb62ebe Trusted Entitlements: added support for unauthenticated endpoints (#2761) 2023-07-07 08:23:47 -07:00
NachoSoto 51bbbb1046 Trusted Entitlements: update handling of escaped URLs (#2758)
The backend no longer unescapes URLs when creating signatures.
2023-07-06 13:06:55 -07:00
NachoSoto bd3ac5faf6 Trusted Entitlements: produce verification failures for static endpoints with no signature (#2752)
Equivalent to https://github.com/RevenueCat/purchases-android/pull/1119
2023-07-06 10:08:45 -07:00
NachoSoto 95a5a97c1c Trusted Entitlements: add API key and HTTPRequest.Path to signature (#2746)
Depends on #2747.

### Changes:
- Changed `Signing` to a `final class`, so it can be instantiated with
the api key.
-  Updated all `SigningTests` to include the correct signatures
2023-07-05 16:39:04 -07:00
NachoSoto e0ffea5411 Signing: extract and verify intermediate key (#2715)
Last step of the new Signature format. Follow up to #2679 and #2698.

This reverts the public key change in #2679, since that was the
intermediate key.
This now extracts the new intermediate public key from the signature,
and verifies it using the public key.
2023-06-30 12:44:32 -07:00
NachoSoto 3f538fc6f9 Trusted Entitlements: update handling of 304 responses (#2698)
### Changes:
- `ETagManager` no longer stores the verification mode
- Created new `VerifiedHTTPResponse`, which has the
`VerificationResult`, extracted from `HTTPResponse`
- Removed `VerificationResult.from(cache:response:)`
- Changed `SignatureVerificationHTTPClientTests` to test using the real
`ETagManager`, so they become more "end to end" unit tests. A lot of
them were relying on implementation details of the mock instead of the
real `ETagManager`.
- Removed several `HTTPClient` tests that were checking a behavior that
was impossible (for example, no verification result despite it being
enabled), or that checked behavior based on the cached
`VerificationResult`
2023-06-30 12:24:53 -07:00
NachoSoto d457cb01d0 Trusted Entitlements: new Signature format (#2679)
### New format:
 - 32 bytes: intermediate public key
 - 4 bytes: Expiration (in days since epoch)
- 64 bytes: intermediate public key signature, signed with the root
private key
 - 16 bytes: salt
 - 64 bytes: payload signature:
   - salt
   - nonce (if present)
   - request time (as int string)
   - etag (if present)
   - payload

This also adds support for optional nonces for "static" signatures,
which is required for #2667.
2023-06-30 11:50:52 -07:00
NachoSoto 4a154fc99a VerificationResult: CustomDebugStringConvertible (#2739)
I'm debugging some code and it's harder to follow it when we just print
`VerificationResult(rawValue: 1)`
2023-06-29 09:14:12 -07:00
NachoSoto f651f61519 Remove unused Signing.loadPublicKey(with:) (#2714) 2023-06-27 05:17:10 -07:00
NachoSoto 6986a2f95e NetworkError.signatureVerificationFailed: added status code to error userInfo (#2657) 2023-06-16 07:55:21 -07:00
NachoSoto 5aff9a8456 Signature Verification: added test-only log for debugging invalid signatures (#2658) 2023-06-16 07:43:09 -07:00
NachoSoto 06b0f3d723 Add InternalDangerousSettings.forceSignatureFailures (#2635)
Similar to #2486. This will be used for SDK-3181.

```
- WARN: ⚠️ Returning fake signature verification failure for 'GET /v1/subscribers/$RCAnonymousID:fc0508bea4a74a41bfc9a69db30d925f'
- ERROR: 😿‼️ Request failed signature verification. Request to /v1/subscribers/$RCAnonymousID%3Afc0508bea4a74a41bfc9a69db30d925f failed verification
```
2023-06-13 13:43:16 -07:00
NachoSoto 6d9125b807 Signing: removed API for loading key from a file (#2638)
This is a leftover from when we were going to embed the certificate as a
file, but it's not needed anymore.
2023-06-13 11:16:49 -07:00
NachoSoto 398293dd4f Added new VerificationResult.verifiedOnDevice (#2379)
This will be used to represent data that has been created and verified
locally.
2023-04-10 08:37:46 -07:00
NachoSoto 4cacc9d7f7 Trusted Entitlements: changed all APIs to internal (#2350)
This isn't ready quite ready to be released yet. With this change,
`main` can be released once again.

I've added a comment to all the changes so we can easily find them when
ready to be made `public` again.
2023-03-17 14:41:55 -07:00
NachoSoto c617b392a2 ETagManager: don't use ETags if response verification failed (#2347)
Scenario:
- Response 1: 200 / `VerificationResult.failed`
- Response 2: 304 / `VerificationResult.verified`

Before this change, that was resulting in `VerificationResult.verified`,
even though it was using the unverified response body.
This fixes that so that we don't use the ETag for a response that failed
validation, even in `ResponseVerificationMode.informational`.
2023-03-17 11:37:35 -07:00
NachoSoto d4ecc7b298 DispatchTimeInterval & Date: avoid 32-bit overflows, fix watchOS crashes (#2342)
Fixes #2338, [SDKONCALL-237], [SDK-2974].

`Int` is actually 32 bits in 32-bit platforms, like `watchOS` still is
(sort of).
This fixes 2 potential `Int` overflows: `Date.millisecondsSince1970` and
`DispatchTimeInterval` arithmetic using `nanoseconds`.

See also #2340.

[SDKONCALL-237]:
https://revenuecats.atlassian.net/browse/SDKONCALL-237?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2023-03-16 13:24:16 -07:00
NachoSoto 3e221e8d97 VerificationResult.notRequested: removed caching reference (#2337)
This is no longer true since #2330.
2023-03-14 18:59:58 -07:00
NachoSoto f5071b60b3 Finished signature verification HTTPClient tests (#2333)
See
https://docs.google.com/spreadsheets/d/1kNcFNB8U47N5_vm8Ww57izMgdp60mdzUxZFK-cm46KU/edit#gid=0
<img width="585" alt="Screenshot 2023-03-13 at 15 59 42"
src="https://user-images.githubusercontent.com/685609/224851071-ea150f7c-e282-4234-bbe2-b404da12208a.png">

### Changes:
- Extracted signature verification `HTTPClient` tests into a separate
file
- Added helper stubbing methods to simplify tests
- Created `InformationalSignatureVerificationHTTPClientTests` and
`EnforcedSignatureVerificationHTTPClientTests`
- Changed `VerificationResult.from(cache:response:)` to match new
expected tests
- Finished coverage for `ResponseVerificationMode.informational` tests
2023-03-14 09:12:43 -07:00
NachoSoto 0475dac6a9 ETagManager: don't ignore failed etags with Signing.VerificationMode.informational (#2331)
Essentially we don't want to modify any behavior on `.informational`
mode. Everything will continue behaving normally, and simply report
verification failures.
2023-03-13 12:24:02 -07:00
NachoSoto 3af5fba011 Made Configuration.EntitlementVerificationMode.enforced unavailable (#2329)
Depends on #2326 and #2328.

This allows us to keep all the code, except users won't be able to
configure the SDK with this option.
Note that `Signing.ResponseVerificationMode.enforced` _is_ available, so
we can still test all that behavior, including from integration tests.
2023-03-13 11:42:52 -07:00
NachoSoto cb3593d9b7 Refactor: reorganized files in new Security and Misc folders (#2326)
Thanks to @aboedo for the suggestion.

### Changes
- Moved signature verification files and tests to new `Security` folder
- Moved `CacheFetchPolicy` inside `Caching`
- Split files inside `Misc` into subfolders
2023-03-13 11:25:36 -07:00