Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): send model install tokens in header

joshistoast4460bb152576
diff --git a/invokeai/app/api/routers/model_manager.py b/invokeai/app/api/routers/model_manager.py
index 24acb154b5..5815f7e6c2 100644
--- a/invokeai/app/api/routers/model_manager.py
+++ b/invokeai/app/api/routers/model_manager.py
@@ -11,7 +11,7 @@ from tempfile import TemporaryDirectory
from typing import List, Optional, Type
import huggingface_hub
-from fastapi import Body, Path, Query, Response, UploadFile
+from fastapi import Body, Header, Path, Query, Response, UploadFile
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.routing import APIRouter
from PIL import Image
@@ -761,6 +761,11 @@ async def install_model(
source: str = Query(description="Model source to install, can be a local path, repo_id, or remote URL"),
inplace: Optional[bool] = Query(description="Whether or not to install a local model in place", default=False),
access_token: Optional[str] = Query(description="access token for the remote resource", default=None),
+ source_access_token: Optional[str] = Header(
+ alias="X-Model-Source-Access-Token",
+ description="access token for the remote resource",
+ default=None,
+ ),
config: ModelRecordChanges = Body(
description="Object containing fields that override auto-probed values in the model config record, such as name, description and prediction_type ",
examples=[{"name": "string", "description": "string"}],
@@ -800,7 +805,7 @@ async def install_model(
result: ModelInstallJob = installer.heuristic_import(
source=source,
config=config,
- access_token=access_token,
+ access_token=source_access_token or access_token,
inplace=bool(inplace),
)
logger.info(f"Started installation of {source}")
diff --git a/invokeai/frontend/webv2/src/workbench/models/api.test.ts b/invokeai/frontend/webv2/src/workbench/models/api.test.ts
new file mode 100644
index 0000000000..adde067b3d
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/models/api.test.ts
@@ -0,0 +1,39 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({
+ apiFetchJson: vi.fn(),
+}));
+
+vi.mock('../backend/http', () => ({
+ apiFetch: vi.fn(),
+ apiFetchJson: mocks.apiFetchJson,
+ buildApiUrl: (path: string) => path,
+}));
+
+describe('installModel', () => {
+ beforeEach(() => {
+ mocks.apiFetchJson.mockReset();
+ mocks.apiFetchJson.mockResolvedValue({
+ id: 1,
+ source: 'https://example.test/model.safetensors',
+ status: 'waiting',
+ });
+ });
+
+ it('sends remote source credentials in a header instead of the URL', async () => {
+ const { installModel } = await import('./api');
+
+ await installModel({
+ accessToken: 'secret-token',
+ config: { name: 'Model' },
+ source: 'https://example.test/model',
+ });
+
+ const [url, init] = mocks.apiFetchJson.mock.calls[0] as [string, RequestInit];
+
+ expect(url).toBe('/api/v2/models/install?source=https%3A%2F%2Fexample.test%2Fmodel');
+ expect(url).not.toContain('secret-token');
+ expect(init.headers).toEqual({ 'X-Model-Source-Access-Token': 'secret-token' });
+ expect(JSON.parse(init.body as string)).toEqual({ name: 'Model' });
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/models/api.ts b/invokeai/frontend/webv2/src/workbench/models/api.ts
index 4f43c5c1cf..388bf56d8f 100644
--- a/invokeai/frontend/webv2/src/workbench/models/api.ts
+++ b/invokeai/frontend/webv2/src/workbench/models/api.ts
@@ -92,17 +92,19 @@ export const installModel = ({
source,
}: InstallModelRequest): Promise<ModelInstallJob> => {
const params = new URLSearchParams({ source });
+ const headers: HeadersInit = {};
if (inplace !== undefined) {
params.set('inplace', String(inplace));
}
if (accessToken) {
- params.set('access_token', accessToken);
+ headers['X-Model-Source-Access-Token'] = accessToken;
}
return apiFetchJson<ModelInstallJob>(`${MODELS_BASE}/install?${params.toString()}`, {
body: JSON.stringify(config ?? {}),
+ headers,
method: 'POST',
});
};