fix: scope tokens per host, harden Gitea support, port upstream fixes

Fork fixes:
- Global token is GitHub-only; never sent to Gitea/Forgejo hosts
- Gitea: download assets via browser_download_url (+token) so private repos work
- Gitea URL parsing keeps port and sub-path installs, strips browser sub-pages
- GitHub detected by hostname, not substring
- Gitea releases: follow pagination (X-Total-Count), skip drafts
- Gitea token validation against the repo; 404 is no longer "valid"
- Settings tab and migration log use this plugin's own id/folder
- Check-only mode no longer reinstalls/enables a missing plugin folder
- reloadPlugin reports load failures (enablePlugin resolves false)
- Repository / release-notes links work for both providers

Ported from upstream BRAT 2.2.0:
- Release sort: semver tags first, date fallback
- Null-safe version comparison, JSON shape guards
- Drop obsidian-daily-notes-interface (read daily note format directly)
- Notice.messageEl, CSS without !important, console.debug/error
This commit is contained in:
olivier committed 2026-10-25 20:39:33 -04:00
1 parent 91247d31e2
commit d3f3fe926a
21 files changed
+548 -495

No files matched your search

+19 -44
View File
@@ -6,7 +6,7 @@ import {
SecretComponent,
Setting,
} from "obsidian";
import { parseWithProvider } from "src/providers";
import { normalizeRepoAddress, parseWithProvider } from "src/providers";
import type { ReleaseVersion } from "src/providers/PluginProvider";
import {
ProviderRateLimitError,
@@ -49,9 +49,6 @@ export default class AddNewPluginModal extends Modal {
addPluginButton: ButtonComponent | null = null;
cancelButton: ButtonComponent | null = null;
// Gitea-specific: stored per-plugin
giteaInstanceUrl: string;
constructor(
plugin: BratPlugin,
betaPlugins: BetaPlugins,
@@ -70,12 +67,20 @@ export default class AddNewPluginModal extends Modal {
this.openSettingsTabAfterwards = openSettingsTabAfterwards;
this.updateVersion = updateVersion;
this.enableAfterInstall = plugin.settings.enableAfterInstall;
this.giteaInstanceUrl = "";
}
async submitForm(): Promise<void> {
if (this.address === "") return;
const scrubbedAddress = this.scrubAddress(this.address);
const scrubbedAddress = normalizeRepoAddress(this.address);
if (!scrubbedAddress) {
toastMessage(
this.plugin,
"Invalid address. Expected: user/repo (GitHub) or https://host/user/repo (Gitea).",
10,
);
this.resetButtons();
return;
}
const existingFrozenPlugin =
this.plugin.settings.pluginSubListFrozenVersion.find(
@@ -204,35 +209,18 @@ export default class AddNewPluginModal extends Modal {
}
/**
* Clean up a user-supplied address. For GitHub, strip protocol/domain.
* For Gitea, keep the full URL.
* Clean up a user-supplied address: `user/repo` for GitHub, the repository
* URL for Gitea. Incomplete input is returned trimmed, unchanged.
*/
private scrubAddress(address: string): string {
const trimmed = address.trim();
// Bare "user/repo" → GitHub
if (/^[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+$/.test(trimmed)) {
return trimmed;
}
// GitHub URL → strip to user/repo
if (/github\.com/i.test(trimmed)) {
return trimmed.replace(/https?:\/\/github\.com\//i, "").replace(/\.git$/i, "").replace(/\/$/, "");
}
// Gitea / other → keep full URL, strip trailing .git
return trimmed.replace(/\.git$/i, "").replace(/\/$/, "");
return normalizeRepoAddress(address) ?? address.trim();
}
/**
* Check if an address looks like a valid repository URL (any provider)
* Check if an address names an owner and a repository (any provider)
*/
private isValidRepository(address: string): boolean {
const clean = address.trim().replace(/\.git$/i, "");
// GitHub format: user/repo or github.com/user/repo
const githubPattern =
/^(?:https?:\/\/github\.com\/)?([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+)$/i;
// Gitea format: hostname/owner/repo
const giteaPattern =
/^https?:\/\/[a-zA-Z0-9.-]+\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+$/i;
return githubPattern.test(clean) || giteaPattern.test(clean);
return normalizeRepoAddress(address) !== null;
}
onOpen(): void {
@@ -545,22 +533,9 @@ export default class AddNewPluginModal extends Modal {
const scrubbedAddress = this.scrubAddress(this.address);
try {
// Get token value
let tokenToUse = "";
if (this.secretName) {
const tokenValue = this.plugin.app.secretStorage.getSecret(
this.secretName,
);
if (tokenValue) tokenToUse = tokenValue;
} else if (this.plugin.settings.globalTokenName) {
const globalToken = this.plugin.app.secretStorage.getSecret(
this.plugin.settings.globalTokenName,
);
if (globalToken) tokenToUse = globalToken;
}
// Use provider to fetch versions
const { provider, parsed } = parseWithProvider(scrubbedAddress);
const tokenToUse = this.betaPlugins.resolveToken(parsed, this.secretName);
const versions = await provider.listReleases(parsed, tokenToUse);
if (versions && versions.length > 0) {
@@ -622,7 +597,7 @@ export default class AddNewPluginModal extends Modal {
toastMessage(this.plugin, error.message, 20);
} else {
validationStatusEl?.setText(
`Unexpected error: ${error}. Enable debug mode for details.`,
`Unexpected error: ${String(error)}. Enable debug mode for details.`,
);
validationStatusEl?.addClass("validation-status-error");
this.disableVersionAndButton();
@@ -640,7 +615,7 @@ export default class AddNewPluginModal extends Modal {
// @ts-expect-error
this.plugin.app.setting.open();
// @ts-expect-error
this.plugin.app.setting.openTabById(this.plugin.APP_ID);
this.plugin.app.setting.openTabById(this.plugin.manifest.id);
}
}
}
+11 -3
View File
@@ -1,5 +1,6 @@
import { ButtonComponent, Modal, Setting } from "obsidian";
import { themeSave } from "../features/themes";
import { normalizeRepoAddress } from "../providers";
import type BratPlugin from "../main";
import { existBetaThemeinInList } from "../settings";
import { toastMessage } from "../utils/notifications";
@@ -22,7 +23,15 @@ export default class AddNewTheme extends Modal {
async submitForm(): Promise<void> {
if (this.address === "") return;
const scrubbedAddress = this.address.replace("https://github.com/", "");
const scrubbedAddress = normalizeRepoAddress(this.address);
if (!scrubbedAddress) {
toastMessage(
this.plugin,
"Invalid address. Expected: user/repo (GitHub) or https://host/user/repo (Gitea).",
10,
);
return;
}
if (existBetaThemeinInList(this.plugin, scrubbedAddress)) {
toastMessage(
this.plugin,
@@ -77,7 +86,6 @@ export default class AddNewTheme extends Modal {
.setCta()
.onClick((e: Event) => {
e.preventDefault();
console.log("Add theme button clicked");
if (this.address !== "") void this.submitForm();
});
});
@@ -113,7 +121,7 @@ export default class AddNewTheme extends Modal {
// @ts-expect-error
this.plugin.app.setting.openTab();
// @ts-expect-error
this.plugin.app.setting.openTabById(this.plugin.APP_ID);
this.plugin.app.setting.openTabById(this.plugin.manifest.id);
}
}
}
+6 -3
View File
@@ -6,6 +6,7 @@ import {
} from "../features/communityUtils";
import { themesCheckAndUpdates } from "../features/themes";
import type BratPlugin from "../main";
import { parseWithProvider } from "../providers";
import { toastMessage } from "../utils/notifications";
import AddNewTheme from "./AddNewTheme";
import type { SuggesterItem } from "./GenericFuzzySuggester";
@@ -166,7 +167,7 @@ export default class PluginCommands {
gfs.setSuggesterData(pluginList);
gfs.display((results) => {
void this.plugin.log(`${results.display} plugin disabled`, false);
if (this.plugin.settings.debuggingMode) console.log(results.info);
if (this.plugin.settings.debuggingMode) console.debug(results.info);
// @ts-expect-error
void this.plugin.app.plugins.disablePluginAndSave(
results.info as string,
@@ -202,7 +203,7 @@ export default class PluginCommands {
{
id: "openGitHubZRepository",
icon: "BratIcon",
name: "Plugins: Open the GitHub repository for a plugin",
name: "Plugins: Open the repository for a plugin",
showInRibbon: true,
callback: async () => {
const communityPlugins = await grabCommmunityPluginList(
@@ -226,7 +227,9 @@ export default class PluginCommands {
gfs.setSuggesterData(bratList);
gfs.display((results) => {
if (results.info)
window.open(`https://github.com/${results.info as string}`);
window.open(
parseWithProvider(results.info as string).parsed.webUrl,
);
});
}
},
+4 -4
View File
@@ -5,7 +5,7 @@ import {
Setting,
SettingGroup,
} from "obsidian";
import { parseWithProvider } from "src/providers";
import { isGitHubAddress } from "src/providers";
import { TokenValidator } from "src/utils/TokenValidator";
import { themeDelete } from "../features/themes";
import type BratPlugin from "../main";
@@ -169,7 +169,7 @@ export class BratSettingsTab extends PluginSettingTab {
? this.plugin.app.secretStorage.getSecret(secretName)
: "";
const isSecretMissing = Boolean(secretName && !secretValue);
const isGitea = !!bp?.giteaUrl;
const isGitea = !isGitHubAddress(p);
const pluginDescription = document.createDocumentFragment();
const trackedVersionText = bp?.version
@@ -431,11 +431,11 @@ export class BratSettingsTab extends PluginSettingTab {
tokenSetting.setName("Personal access token").setDesc(
createLink({
prependText:
"Increase rate limits for GitHub, or access private repos. Create one in ",
"GitHub only: increases rate limits and gives access to private repos. Create one in ",
url: "https://github.com/settings/tokens/new?scopes=public_repo",
text: "your GitHub settings",
appendText:
". For Gitea, create a token in your Gitea instance settings.",
". It is never sent to other hosts: for Gitea/Forgejo, set a token on each plugin instead.",
}),
);