Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-http-response-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/studio": patch
---

fix: add HTTP response status validation to fetch() calls
6 changes: 5 additions & 1 deletion apps/studio/src/services/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export class ApplicationService extends AbstractService {

let content = '';
if (url) {
content = await fetch(url).then((res) => res.text());
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
}
content = await res.text();
} else if (base64) {
content = this.svcs.formatSvc.decodeBase64(base64);
} else if (share) {
Expand Down
13 changes: 11 additions & 2 deletions apps/studio/src/services/editor.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ export class EditorService extends AbstractService {
content = await file.text();
}
if (!content && (/^https?:\/\//).test(uri)) {
content = await fetch(uri).then((res) => res.text());
const res = await fetch(uri);
if (!res.ok) {
throw new Error(`Failed to fetch ${uri}: ${res.status} ${res.statusText}`);
}
content = await res.text();
}

if (typeof content !== 'string') {
Expand Down Expand Up @@ -375,7 +379,12 @@ export class EditorService extends AbstractService {
globalThis.history.pushState({}, '', `${currentUrl}?url=${url}`);

return fetch(url)
.then(res => res.text())
.then(res => {
if (!res.ok) {
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
}
return res.text();
})
.then(async text => {
const language = this.inferLanguageFromUri(url, text);
const projectRoot = (() => {
Expand Down
6 changes: 5 additions & 1 deletion apps/studio/src/services/parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ export class ParserService extends AbstractService {
},
async read(uri: any): Promise<string> {
const url = String(uri);
const content = await fetch(url).then((res) => res.text());
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
}
const content = await res.text();
filesState.getState().updateFile(url, {
uri: url,
name: url.split('/').pop() || url,
Expand Down
63 changes: 63 additions & 0 deletions apps/studio/src/services/tests/editor-http-validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createServices } from '../';

import type { EditorService } from '../editor.service';

// Mock global fetch
const originalFetch = globalThis.fetch;

describe('EditorService - HTTP response validation', () => {
let editorSvc: EditorService;

beforeAll(async () => {
const services = await createServices();
editorSvc = services.editorSvc;
});

afterEach(() => {
globalThis.fetch = originalFetch;
});

describe('.importFromURL', () => {
test('should throw an error when fetch returns 404', async () => {
globalThis.fetch = jest.fn().mockResolvedValue({
ok: false,
status: 404,
statusText: 'Not Found',
text: jest.fn().mockResolvedValue('<html>Not Found</html>'),
});

const url = 'https://example.com/nonexistent.yaml';
await expect(editorSvc.importFromURL(url)).rejects.toThrow(
`Failed to fetch ${url}: 404 Not Found`,
);
});

test('should throw an error when fetch returns 500', async () => {
globalThis.fetch = jest.fn().mockResolvedValue({
ok: false,
status: 500,
statusText: 'Internal Server Error',
text: jest.fn().mockResolvedValue('Server Error'),
});

const url = 'https://example.com/broken.yaml';
await expect(editorSvc.importFromURL(url)).rejects.toThrow(
`Failed to fetch ${url}: 500 Internal Server Error`,
);
});

test('should not throw when fetch returns 200', async () => {
const validContent = 'asyncapi: 2.6.0\ninfo:\n title: Test\n version: 1.0.0';
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
statusText: 'OK',
text: jest.fn().mockResolvedValue(validContent),
});

const url = 'https://example.com/valid.yaml';
// Should not throw
await expect(editorSvc.importFromURL(url)).resolves.not.toThrow();
});
});
});
Loading