Skip to content

Commit

Permalink
403
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Feb 11, 2025
1 parent ab82e44 commit f223f8e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/node-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const BASE64_SUFFIX = ';base64';
async function getResponseForFile(url: string) {
const path = fileURLToPath(url);
try {
await fsPromises.access(path, fsPromises.constants.R_OK);
const stats = await fsPromises.stat(path, {
bigint: true,
});
Expand Down
13 changes: 11 additions & 2 deletions packages/node-fetch/tests/non-http-fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Buffer } from 'node:buffer';
import { unlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { describe, expect, it } from '@jest/globals';
Expand All @@ -20,8 +22,15 @@ describe('File protocol', () => {
expect(response.status).toBe(404);
});
it('returns 403 if file is not accessible', async () => {
const response = await fetchPonyfill(pathToFileURL('/etc/shadow'));
expect(response.status).toBe(403);
const tmpDir = tmpdir();
const path = join(tmpDir, 'forbidden.json');
writeFileSync(path, '{ "test": 1 }', { mode: 0o000 });
try {
const response = await fetchPonyfill(pathToFileURL(path));
expect(response.status).toBe(403);
} finally {
unlinkSync(path);
}
});
});

Expand Down

0 comments on commit f223f8e

Please sign in to comment.