Skip to content

Commit 63e7183

Browse files
committed
fix(remove-axios): Removes axios dependency with security issue
1 parent 7845131 commit 63e7183

7 files changed

Lines changed: 34 additions & 32 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
on:
22
push:
3-
branches: [main]
43
pull_request:
54
name: ci
65
jobs:
@@ -11,7 +10,7 @@ jobs:
1110
node: [18, 20]
1211
include:
1312
# use latest npm by default
14-
- npm-version: latest
13+
- npm-version: 10.9.2
1514
steps:
1615
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
1716
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
@@ -21,4 +20,4 @@ jobs:
2120
- name: Upgrade to latest npm to support lockfile v2
2221
run: npm install -g npm@${{ matrix.npm-version }}
2322
- run: npm install && npm install
24-
- run: npm run test
23+
- run: npm run test

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/synthetics-sdk-api/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
"error-stack-parser": "2.1.4",
4444
"google-auth-library": "9.0.0",
4545
"ts-proto": "1.148.1",
46-
"winston": "3.10.0",
47-
"axios": "1.6.7"
46+
"winston": "3.10.0"
4847
},
4948
"author": "Google Inc.",
5049
"license": "Apache-2.0"

packages/synthetics-sdk-api/src/cloud_region_resolver.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import axios, { AxiosResponse } from 'axios';
16-
1715
/**
18-
*
1916
* @public
2017
*
2118
* Retrieves the region in which the current Google Cloud Function (v2) is
@@ -24,24 +21,28 @@ import axios, { AxiosResponse } from 'axios';
2421
export async function getExecutionRegion(): Promise<string | null> {
2522
const metadataServerUrl =
2623
'http://metadata.google.internal/computeMetadata/v1/instance/region';
27-
const headers = { 'Metadata-Flavor': 'Google' };
24+
const options = {
25+
headers: {
26+
'Metadata-Flavor': 'Google',
27+
},
28+
};
2829

2930
try {
30-
const response: AxiosResponse = await axios.get(metadataServerUrl, {
31-
headers,
32-
});
31+
const response = await fetch(metadataServerUrl, options);
32+
33+
if (!response.ok) {
34+
throw new Error(`Request failed with status ${response.status}`);
35+
}
36+
37+
const body = await response.text();
3338

3439
// Extract region from the response (e.g., 'us-east1')
35-
const regions = response.data.split('/');
40+
const regions = body.split('/');
3641
const region = regions[regions.length - 1];
3742

3843
return region;
3944
} catch (error) {
40-
if (axios.isAxiosError(error)) {
41-
console.error('Error fetching region from metadata server:', error);
42-
} else {
43-
console.error('Unexpected error:', error);
44-
}
45+
console.error('Error fetching region from metadata server:', error);
4546
}
4647

4748
return null;

packages/synthetics-sdk-api/test/unit/cloud_region_resolver.spec.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,38 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import axios from 'axios';
1615
import * as sinon from 'sinon';
1716
import { expect } from 'chai';
1817
import { getExecutionRegion } from '../../src/cloud_region_resolver';
1918

20-
describe('getExecutionRegion', () => {
21-
let axiosGetStub: sinon.SinonStub;
19+
describe('getExecutionRegion (with fetch)', () => {
20+
let fetchStub: sinon.SinonStub;
2221

2322
beforeEach(() => {
24-
axiosGetStub = sinon.stub(axios, 'get');
23+
fetchStub = sinon.stub(global, 'fetch');
2524
});
2625

2726
afterEach(() => {
28-
axiosGetStub.restore();
27+
fetchStub.restore();
2928
});
3029

3130
it('should retrieve the region from the metadata server', async () => {
32-
axiosGetStub.resolves({ data: 'projects/123456789/regions/us-east1' });
31+
const mockResponse = {
32+
ok: true,
33+
text: () => Promise.resolve('projects/123456789/regions/us-east1'),
34+
};
35+
fetchStub.resolves(mockResponse as any);
3336

3437
const region = await getExecutionRegion();
3538
expect(region).to.equal('us-east1');
36-
expect(axiosGetStub.calledOnce).to.be.true;
39+
expect(fetchStub.calledOnce).to.be.true;
3740
});
3841

3942
it('should handle errors from the metadata server', async () => {
40-
axiosGetStub.rejects(new Error('Metadata server error'));
43+
fetchStub.rejects(new Error('Network error'));
4144

4245
const region = await getExecutionRegion();
46+
4347
expect(region).to.be.null;
4448
});
4549
});

packages/synthetics-sdk-api/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"composite": true,
88
"resolveJsonModule": true,
99
"esModuleInterop": true,
10-
"module": "commonjs"
10+
"module": "commonjs",
11+
"lib": ["es2020", "dom"]
1112
},
1213
"include": [
1314
"src/**/*",

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"extends": "./node_modules/gts/tsconfig-google.json"
3-
}
3+
}

0 commit comments

Comments
 (0)