committed by
GitHub
10 changed files with 628 additions and 7 deletions
@ -0,0 +1,43 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import org.eclipse.jgit.errors.UnsupportedCredentialItem; |
|||
import org.eclipse.jgit.transport.CredentialItem; |
|||
import org.eclipse.jgit.transport.URIish; |
|||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
|||
|
|||
class HostScopedCredentialsProvider extends UsernamePasswordCredentialsProvider { |
|||
|
|||
private final String allowedHost; |
|||
|
|||
HostScopedCredentialsProvider(String username, String password, String allowedHost) { |
|||
super(username, password == null ? "" : password); |
|||
this.allowedHost = allowedHost; |
|||
} |
|||
|
|||
@Override |
|||
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem { |
|||
if (uri == null || allowedHost == null) { |
|||
return false; |
|||
} |
|||
String requestedHost = uri.getHost(); |
|||
if (requestedHost == null || !allowedHost.equalsIgnoreCase(requestedHost)) { |
|||
return false; |
|||
} |
|||
return super.get(uri, items); |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import org.eclipse.jgit.transport.URIish; |
|||
import org.thingsboard.common.util.SsrfProtectionValidator; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
|
|||
import java.net.URISyntaxException; |
|||
import java.util.Set; |
|||
|
|||
public final class RepositoryUriValidator { |
|||
|
|||
private static final Set<String> ALLOWED_SCHEMES = Set.of("http", "https", "ssh"); |
|||
|
|||
private RepositoryUriValidator() { |
|||
} |
|||
|
|||
public static void validate(RepositorySettings settings) { |
|||
if (settings == null || settings.isLocalOnly()) { |
|||
return; |
|||
} |
|||
String uri = settings.getRepositoryUri(); |
|||
if (uri == null || uri.isBlank()) { |
|||
throw new IllegalArgumentException("Repository URI is required"); |
|||
} |
|||
URIish parsed; |
|||
try { |
|||
parsed = new URIish(uri.trim()); |
|||
} catch (URISyntaxException e) { |
|||
throw new IllegalArgumentException("Repository URI is invalid: " + e.getMessage(), e); |
|||
} |
|||
// URIish returns null scheme for scp-style git@host:path; treat as ssh
|
|||
String scheme = parsed.getScheme(); |
|||
String effectiveScheme = scheme == null ? "ssh" : scheme.toLowerCase(); |
|||
if (!ALLOWED_SCHEMES.contains(effectiveScheme)) { |
|||
throw new IllegalArgumentException("Repository URI scheme '" + effectiveScheme + "' is not allowed. " + |
|||
"Allowed schemes: http, https, ssh"); |
|||
} |
|||
String host = parsed.getHost(); |
|||
if (host == null || host.isEmpty()) { |
|||
throw new IllegalArgumentException("Repository URI is missing a hostname"); |
|||
} |
|||
// Validate the host extracted by URIish — the same parser JGit uses for transport.
|
|||
// Using java.net.URI here would risk parser-disagreement SSRF bypasses
|
|||
// (e.g. http://evil.com#@127.0.0.1/ where URI.getHost() and URIish.getHost() differ).
|
|||
try { |
|||
SsrfProtectionValidator.validateHost(host); |
|||
} catch (RuntimeException e) { |
|||
throw new IllegalArgumentException("Repository URI is not allowed: " + e.getMessage() + |
|||
". If this host should be accessible, contact the platform administrator about SSRF allow-list.", e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,103 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import org.eclipse.jgit.transport.CredentialItem; |
|||
import org.eclipse.jgit.transport.URIish; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.net.URISyntaxException; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
class HostScopedCredentialsProviderTest { |
|||
|
|||
@Test |
|||
void sameHostReturnsCredentials() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", "pass", "github.com"); |
|||
URIish uri = new URIish("https://github.com/owner/repo.git"); |
|||
CredentialItem.Username userItem = new CredentialItem.Username(); |
|||
CredentialItem.Password passItem = new CredentialItem.Password(); |
|||
|
|||
boolean result = provider.get(uri, userItem, passItem); |
|||
|
|||
assertThat(result).isTrue(); |
|||
assertThat(userItem.getValue()).isEqualTo("user"); |
|||
assertThat(new String(passItem.getValue())).isEqualTo("pass"); |
|||
} |
|||
|
|||
@Test |
|||
void crossHostReturnsFalseAndDoesNotPopulateItems() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", "pass", "github.com"); |
|||
URIish redirectTarget = new URIish("https://attacker.example.com/owner/repo.git"); |
|||
CredentialItem.Username userItem = new CredentialItem.Username(); |
|||
CredentialItem.Password passItem = new CredentialItem.Password(); |
|||
|
|||
boolean result = provider.get(redirectTarget, userItem, passItem); |
|||
|
|||
assertThat(result).isFalse(); |
|||
assertThat(userItem.getValue()).isNull(); |
|||
assertThat(passItem.getValue()).isNull(); |
|||
} |
|||
|
|||
@Test |
|||
void hostComparisonIsCaseInsensitive() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", "pass", "GitHub.COM"); |
|||
URIish uri = new URIish("https://github.com/owner/repo.git"); |
|||
CredentialItem.Username userItem = new CredentialItem.Username(); |
|||
|
|||
boolean result = provider.get(uri, userItem); |
|||
|
|||
assertThat(result).isTrue(); |
|||
assertThat(userItem.getValue()).isEqualTo("user"); |
|||
} |
|||
|
|||
@Test |
|||
void crossHostToPrivateIpIsRefused() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", "pass", "github.com"); |
|||
URIish internalRedirect = new URIish("http://10.0.0.5/owner/repo.git"); |
|||
CredentialItem.Username userItem = new CredentialItem.Username(); |
|||
|
|||
boolean result = provider.get(internalRedirect, userItem); |
|||
|
|||
assertThat(result).isFalse(); |
|||
assertThat(userItem.getValue()).isNull(); |
|||
} |
|||
|
|||
@Test |
|||
void nullAllowedHostRefuses() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", "pass", null); |
|||
URIish uri = new URIish("https://github.com/owner/repo.git"); |
|||
CredentialItem.Username userItem = new CredentialItem.Username(); |
|||
|
|||
boolean result = provider.get(uri, userItem); |
|||
|
|||
assertThat(result).isFalse(); |
|||
assertThat(userItem.getValue()).isNull(); |
|||
} |
|||
|
|||
@Test |
|||
void nullPasswordIsNormalizedToEmpty() throws URISyntaxException { |
|||
HostScopedCredentialsProvider provider = new HostScopedCredentialsProvider("user", null, "github.com"); |
|||
URIish uri = new URIish("https://github.com/owner/repo.git"); |
|||
CredentialItem.Password passItem = new CredentialItem.Password(); |
|||
|
|||
boolean result = provider.get(uri, passItem); |
|||
|
|||
assertThat(result).isTrue(); |
|||
assertThat(new String(passItem.getValue())).isEmpty(); |
|||
} |
|||
} |
|||
@ -0,0 +1,227 @@ |
|||
/** |
|||
* Copyright © 2016-2026 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import org.junit.jupiter.api.AfterEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.parallel.ResourceLock; |
|||
import org.junit.jupiter.params.ParameterizedTest; |
|||
import org.junit.jupiter.params.provider.ValueSource; |
|||
import org.thingsboard.common.util.SsrfProtectionValidator; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
|
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThatNoException; |
|||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|||
|
|||
@ResourceLock("SsrfProtectionValidator") // shares static state with SsrfProtectionValidatorTest
|
|||
class RepositoryUriValidatorTest { |
|||
|
|||
@AfterEach |
|||
void reset() { |
|||
SsrfProtectionValidator.setEnabled(false); |
|||
SsrfProtectionValidator.setAllowedHosts(Collections.emptyList()); |
|||
SsrfProtectionValidator.setAdditionalBlockedHosts(Collections.emptyList()); |
|||
} |
|||
|
|||
private static RepositorySettings settings(String uri) { |
|||
RepositorySettings s = new RepositorySettings(); |
|||
s.setRepositoryUri(uri); |
|||
return s; |
|||
} |
|||
|
|||
private static RepositorySettings localOnly(String uri) { |
|||
RepositorySettings s = settings(uri); |
|||
s.setLocalOnly(true); |
|||
return s; |
|||
} |
|||
|
|||
// --- localOnly skip ---
|
|||
|
|||
@Test |
|||
void localOnlySkipsAllValidation() { |
|||
// Even file:// and unresolvable hosts pass when localOnly=true
|
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(localOnly("file:///etc/passwd"))); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(localOnly("anything-not-a-uri"))); |
|||
} |
|||
|
|||
@Test |
|||
void nullSettingsIsNoOp() { |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(null)); |
|||
} |
|||
|
|||
// --- null / blank URI ---
|
|||
|
|||
@Test |
|||
void nullUriIsRejected() { |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings(null))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("Repository URI is required"); |
|||
} |
|||
|
|||
@Test |
|||
void blankUriIsRejected() { |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings(" "))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("Repository URI is required"); |
|||
} |
|||
|
|||
// --- scheme whitelist ---
|
|||
|
|||
@ParameterizedTest |
|||
@ValueSource(strings = { |
|||
"file:///tmp/repo", |
|||
"git://github.com/foo/bar", |
|||
"ftp://server/repo" |
|||
}) |
|||
void disallowedSchemesAreRejected(String uri) { |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings(uri))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("scheme"); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@ValueSource(strings = { |
|||
"http://example.com/foo.git", |
|||
"https://example.com/foo.git", |
|||
"ssh://git@example.com/foo.git", |
|||
"git@github.com:owner/repo.git" // scp-style → SSH inferred
|
|||
}) |
|||
void allowedSchemesPassWhenSsrfDisabled(String uri) { |
|||
SsrfProtectionValidator.setEnabled(false); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings(uri))); |
|||
} |
|||
|
|||
// --- host checks ---
|
|||
|
|||
@Test |
|||
void emptyHostIsRejected() { |
|||
// http:/// (no host)
|
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("https:///path"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("hostname"); |
|||
} |
|||
|
|||
// --- SSRF integration: HTTP/HTTPS ---
|
|||
|
|||
@Test |
|||
void httpPrivateIpRejectedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("https://10.0.0.5/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void httpLocalhostRejectedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("http://localhost/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void httpPublicIpAllowedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("https://8.8.8.8/foo.git"))); |
|||
} |
|||
|
|||
// --- SSRF integration: SSH (uses validateHost) ---
|
|||
|
|||
@Test |
|||
void sshPrivateIpRejectedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("ssh://git@192.168.1.10/foo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void scpStyleSshPrivateIpRejectedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("git@10.0.0.1:owner/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void sshInternalSuffixRejectedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("ssh://git@server.internal/foo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void sshPublicHostAllowedWhenSsrfEnabled() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("ssh://git@github.com/foo/bar.git"))); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("git@github.com:foo/bar.git"))); |
|||
} |
|||
|
|||
// --- allow-list interaction ---
|
|||
|
|||
@Test |
|||
void sshHostInAllowListPassesEvenWhenPrivate() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
SsrfProtectionValidator.setAllowedHosts(List.of("10.0.0.0/8")); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("ssh://git@10.1.2.3/foo.git"))); |
|||
} |
|||
|
|||
@Test |
|||
void httpHostInAllowListPassesEvenWhenPrivate() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
SsrfProtectionValidator.setAllowedHosts(List.of("internal-git.corp")); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("https://internal-git.corp/foo.git"))); |
|||
} |
|||
|
|||
// --- Parser-consistency: host must come from URIish (the parser JGit transport uses) ---
|
|||
|
|||
@Test |
|||
void userInfoBeforeInternalHostIsRejected() { |
|||
// http://github.com@127.0.0.1/ — URIish parses userinfo=github.com, host=127.0.0.1.
|
|||
// JGit transport will connect to 127.0.0.1. Validator must see the same host.
|
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("http://github.com@127.0.0.1/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void userInfoBeforePrivateCidrHostIsRejected() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("https://fake-user:fake-pass@10.0.0.5/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
|
|||
@Test |
|||
void userInfoBeforePublicHostIsAllowed() { |
|||
// The userinfo is just credentials — host is what matters.
|
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatNoException().isThrownBy(() -> RepositoryUriValidator.validate(settings("https://user:pass@github.com/repo.git"))); |
|||
} |
|||
|
|||
@Test |
|||
void ipv6LoopbackLiteralIsRejected() { |
|||
SsrfProtectionValidator.setEnabled(true); |
|||
assertThatThrownBy(() -> RepositoryUriValidator.validate(settings("https://[::1]/repo.git"))) |
|||
.isInstanceOf(IllegalArgumentException.class) |
|||
.hasMessageContaining("not allowed"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue