mattwebbio / orbital-sync

Synchronize multiple Pi-hole instances
http://orbitalsync.com
MIT License
550 stars 21 forks source link

Fix error "Not authorized" when uploading archives #359

Closed Hazegard closed 1 month ago

Hazegard commented 2 months ago

The persistentlogin=off snippet in the request makes the server to respond the cookie persistentlogin, which will then be send in the POST teleporter.php request when uploading backup, which will cause the pihole server to respond: "Not authorized" when this cookie is sent.

Removing this snippet in the login request removes the persistentlogin in the response. Conesquently, the archive upload is accepted by the pihole sever.

=====

When using orbital-sync, I have the following logs:

pihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Signing in to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: ✔️   Successfully signed in to https://example.com/admin!
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Downloading backup from https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: ✔️   Backup from https://example.com/admin completed!
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Signing in to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Signing in to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Signing in to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: ✔️   Successfully signed in to https://example.com/admin!
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Uploading backup to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: {"host":"https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:58:04 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:58:04 AM: ✔️   Successfully signed in to https://example.com/admin!
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Uploading backup to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: {"host":"https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:58:04 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:58:04 AM: ✔️   Successfully signed in to https://example.com/admin!
 ihole-sync  | 6/30/2024, 8:58:04 AM: ➡️   Uploading backup to https://example.com/admin...
 ihole-sync  | 6/30/2024, 8:58:04 AM: {"host":"https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:58:04 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:58:04 AM: ⚠ Failure: 0/3 hosts synced.
pihole-sync  | 6/30/2024, 8:58:04 AM: Waiting 1 minutes...
pihole-sync  | 6/30/2024, 8:59:04 AM: ➡️   Signing in to https://example.com/admin!
pihole-sync  | 6/30/2024, 8:59:04 AM: ➡️   Uploading backup to https://example.com/admin!
pihole-sync  | 6/30/2024, 8:59:04 AM: ➡️   Uploading backup to https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:59:04 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:59:04 AM: {"host":"https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:59:04 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:59:05 AM: ✔️"  Successfully signed in to https://example.com/admin!
pihole-sync  | 6/30/2024, 8:59:05 AM: ➡️"  Uploading backup to https://example.com","path":"/admin","status":200,"responseBody":"Not authorized"}
pihole-sync  | 6/30/2024, 8:59:05 AM: ⚠ Error: Failed to upload backup to "https://example.com/admin".
pihole-sync  | 6/30/2024, 8:59:05 AM: ⚠ Failure: 0/3 hosts synced.
pihole-sync  | 6/30/2024, 8:59:05 AM: Waiting 1 minutes...

I tracked the upload failure, and it is related to the persistentlogin cookie being sent with the upload request:

image

When I try the same request without this cookie, the upload works:

image

This cookie comes from the &persistentlogin=off part of the request sent during the login (https://github.com/mattwebbio/orbital-sync/blob/master/src/client/v5/index.ts#L42)

When this part is appended to the request, the persistentlogin cookie is received (and is then sent during the backup upload request):

image

When i remove this part, the cookie is not set and the upload works: image

mattwebbio commented 2 months ago

This is amazing! Thanks so much for figuring this out :)

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.52%. Comparing base (103155c) to head (c9eea45).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #359 +/- ## ======================================= Coverage 97.52% 97.52% ======================================= Files 10 10 Lines 283 283 Branches 95 97 +2 ======================================= Hits 276 276 Misses 5 5 Partials 2 2 ``` | [Flag](https://app.codecov.io/gh/mattwebbio/orbital-sync/pull/359/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Matt+Webb) | Coverage Δ | | |---|---|---| | [integration](https://app.codecov.io/gh/mattwebbio/orbital-sync/pull/359/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Matt+Webb) | `52.96% <ø> (ø)` | | | [unit](https://app.codecov.io/gh/mattwebbio/orbital-sync/pull/359/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Matt+Webb) | `96.46% <ø> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Matt+Webb#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.