Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change ip to resolve the ip's SSRF risk #436

Merged
merged 2 commits into from
Jul 23, 2024
Merged
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
2 changes: 0 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"body-parser": "1.20.1",
"cors": "2.8.5",
"dayjs": "1.11.6",
"ip": "1.1.9",
"lodash": "^4.17.21",
"open": "^8.4.0",
"serve-static": "1.15.0",
Expand All @@ -39,7 +38,6 @@
"devDependencies": {
"@types/body-parser": "1.19.2",
"@types/cors": "2.8.13",
"@types/ip": "1.1.0",
"@types/lodash": "^4.17.0",
"@types/node": "^16",
"@types/serve-static": "1.15.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk/server/apis/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SDK } from '@rsdoctor/types';
import ip from 'ip';
import { BaseAPI } from './base';
import { Router } from '../router';
import { getLocalIpAddress } from '../utils';

export class ProjectAPI extends BaseAPI {
@Router.get(SDK.ServerAPI.API.Env)
Expand All @@ -11,7 +11,7 @@ export class ProjectAPI extends BaseAPI {
const { server } = this.ctx;

return {
ip: ip.address(),
ip: getLocalIpAddress(),
port: server.port,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import serve from 'serve-static';
import { Bundle } from '@rsdoctor/utils/common';
import assert from 'assert';
import bodyParser from 'body-parser';
import ip from 'ip';
import cors from 'cors';
import { PassThrough } from 'stream';
import { Socket } from './socket';
Expand All @@ -13,6 +12,7 @@ import * as APIs from './apis';
import { chalk, logger } from '@rsdoctor/utils/logger';
import { openBrowser } from '@/sdk/utils/openBrowser';
import path from 'path';
import { getLocalIpAddress } from './utils';
export * from './utils';

export class RsdoctorServer implements SDK.RsdoctorServerInstance {
Expand Down Expand Up @@ -45,7 +45,7 @@ export class RsdoctorServer implements SDK.RsdoctorServerInstance {
}

public get host(): string {
const host = ip.address();
const host = getLocalIpAddress();
return host;
}

Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/src/sdk/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SDK } from '@rsdoctor/types';
import os from 'os';

export function getDataByPagination<T>(
data: T[],
Expand All @@ -18,3 +19,15 @@ export function getDataByPagination<T>(
},
};
}

export function getLocalIpAddress() {
const interfaces = os.networkInterfaces();
for (const iface of Object.values(interfaces)) {
for (const alias of iface as os.NetworkInterfaceInfo[]) {
if (alias.family === 'IPv4' && !alias.internal) {
return alias.address;
}
}
}
return '127.0.0.1'; // fallback to localhost if no external IP found
}
4 changes: 2 additions & 2 deletions packages/sdk/tests/server/apis/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it, expect, vi } from 'vitest';
import ip from 'ip';
import { Manifest, SDK } from '@rsdoctor/types';
import { Manifest as ManifestShared } from '@rsdoctor/utils/common';

import { cwd, setupSDK } from '../../utils';
import { getLocalIpAddress } from '@/sdk/server/utils';

vi.setConfig({ testTimeout: 50000 });

Expand All @@ -13,7 +13,7 @@ describe('test server/apis/project.ts', () => {
it(`test api: ${SDK.ServerAPI.API.Env}`, async () => {
const env = (await target.get(SDK.ServerAPI.API.Env)).toJSON();

expect(env.ip).toEqual(ip.address());
expect(env.ip).toEqual(getLocalIpAddress());
expect(env.port).toEqual(target.server.port);
});

Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading