Skip to content

Commit

Permalink
Merge pull request #9969 from nextcloud/backport/9964/stable3.6
Browse files Browse the repository at this point in the history
[stable3.6] fix(autoconfig): Refactor DNS query for testing
  • Loading branch information
ChristophWurst committed Aug 6, 2024
2 parents 9326f6b + 8e2cdc7 commit 60c0880
Show file tree
Hide file tree
Showing 10 changed files with 16,515 additions and 13 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/update-public-suffix-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: MIT
name: Update public suffix list

on:
workflow_dispatch:
schedule:
- cron: "5 2 * * *"

jobs:
update-public-suffix-list:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
branches: ['main', 'stable3.7']

name: update-public-suffix-list-${{ matrix.branches }}

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ matrix.branches }}
submodules: true

- name: Download public suffix list
run: curl --output resources/resources/public_suffix_list.dat https://publicsuffix.org/list/public_suffix_list.dat

- name: Create Pull Request
uses: peter-evans/create-pull-request@c5a7806660adbe173f04e3e038b0ccdcd758773c
with:
token: ${{ secrets.COMMAND_BOT_PAT }}
commit-message: 'fix(dns): Update public suffix list'
committer: GitHub <noreply@github.com>
author: nextcloud-command <nextcloud-command@users.noreply.github.com>
signoff: true
branch: 'fix/dns/update-public-suffix-list-${{ matrix.branches }}'
title: '[${{ matrix.branches }}] fix(dns): Update public suffix list'
body: |
Auto-generated update of https://publicsuffix.org/
labels: |
dependencies
3. to review
reviewers: ChristophWurst
373 changes: 373 additions & 0 deletions LICENSES/MPL-2.0.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ezyang/htmlpurifier": "4.17.0",
"gravatarphp/gravatar": "dev-master#6b9f6a45477ce48285738d9d0c3f0dbf97abe263",
"hamza221/html2text": "^1.0",
"jeremykendall/php-domain-parser": "^6.0",
"nextcloud/horde-managesieve": "^1.0",
"nextcloud/horde-smtp": "^1.0.2",
"nextcloud/kitinerary": "^1.0",
Expand Down
95 changes: 94 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions lib/Dns/Resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Dns;

use Pdp\Domain;
use Pdp\Rules;
use function dns_get_record;

class Resolver {

public function resolve(string $hostname, int $type): array|false {
return dns_get_record($hostname, $type);
}

public function isSuffix(string $hostname): bool {
$publicSuffixList = Rules::fromPath(__DIR__ . '/../../resources/public_suffix_list.dat');
$domain = Domain::fromIDNA2008($hostname);

$result = $publicSuffixList->resolve($domain);

return $result->secondLevelDomain()->toString() === '';
}

}
15 changes: 12 additions & 3 deletions lib/Service/AutoConfig/IspDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

use Exception;
use Horde_Mail_Rfc822_Address;
use OCA\Mail\Dns\Resolver;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
use SimpleXMLElement;
use function explode;
use function str_replace;
use function strtolower;

Expand All @@ -40,6 +42,7 @@ class IspDb {

/** @var LoggerInterface */
private $logger;
private Resolver $dnsResolver;

/** @returns string[] */
public function getUrls(): array {
Expand All @@ -52,8 +55,11 @@ public function getUrls(): array {
];
}

public function __construct(IClientService $clientService, LoggerInterface $logger) {
public function __construct(IClientService $clientService,
Resolver $dnsResolver,
LoggerInterface $logger) {
$this->client = $clientService->newClient();
$this->dnsResolver = $dnsResolver;
$this->logger = $logger;
}

Expand Down Expand Up @@ -160,11 +166,14 @@ public function query(string $domain, Horde_Mail_Rfc822_Address $email, bool $tr
}
}

if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
if ($tryMx && ($dns = $this->dnsResolver->resolve($domain, DNS_MX))) {
$domain = $dns[0]['target'];
if (!($config = $this->query($domain, $email, false))) {
[, $domain] = explode('.', $domain, 2);
$config = $this->query($domain, $email, false);
// Only try second-level domains and deeper
if (!$this->dnsResolver->isSuffix($domain)) {
$config = $this->query($domain, $email, false);
}
}
}
return $config;
Expand Down
Loading

0 comments on commit 60c0880

Please sign in to comment.