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

chore: security fixes #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 20.x

# PreInstall
- name: add gulp, codecov
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ncu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.x"
node-version: "20.x"
- name: Update dependencies
run: |
npx -p npm-check-updates ncu -u
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 20.x

# PreInstall
- name: add gulp, codecov
Expand Down
20 changes: 0 additions & 20 deletions __mocks__/request.ts

This file was deleted.

27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "samsung-tv-control",
"version": "1.14.0",
"version": "1.15.0",
"description": "Remote your tv via JS!",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down Expand Up @@ -39,24 +39,23 @@
},
"homepage": "https://github.com/Toxblh/samsung-tv-remote#readme",
"dependencies": {
"axios": "^1.7.7",
"node-ssdp": "^4.0.1",
"request": "^2.88.2",
"type-coverage": "^2.18.0",
"type-coverage": "^2.29.1",
"wake_on_lan": "^1.0.0",
"ws": "^8.2.0"
"ws": "^8.18.0"
},
"devDependencies": {
"@types/jest": "^27.0.1",
"@types/node": "^16.7.2",
"@types/node-ssdp": "^4.0.1",
"@types/request": "^2.48.7",
"@types/wake_on_lan": "0.0.30",
"@types/ws": "^7.4.7",
"jest": "^27.0.6",
"ts-jest": "^27.0.5",
"@types/jest": "^29.5.13",
"@types/node": "^20.16.11",
"@types/node-ssdp": "^4.0.4",
"@types/wake_on_lan": "0.0.33",
"@types/ws": "^8.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"tslint": "^6.1.3",
"typedoc": "^0.21.6",
"typescript": "^4.3.5"
"typedoc": "^0.26.8",
"typescript": "^5.6.3"
},
"jest": {
"transform": {
Expand Down
9 changes: 7 additions & 2 deletions src/__tests__/samsung.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import axios from 'axios'
import Samsung from '../samsung'
import * as fs from 'fs'

jest.mock('fs')
jest.mock('axios')

describe('test config', () => {
it('empty ip', () => {
Expand Down Expand Up @@ -91,6 +93,7 @@ describe('Minimal config', () => {
})

it('should sendYouTubeLink', () => {
jest.spyOn(axios, 'post').mockImplementation(() => Promise.resolve({ data: { data: 'data' } }))
const spy = jest.spyOn(control, 'openYouTubeLink')
control.openYouTubeLink('https://www.youtube.com/watch?v=1111111')

Expand All @@ -117,7 +120,9 @@ describe('saveToken', () => {

it('should correct save with exeption while save', () => {
// @ts-ignore
jest.spyOn(fs, 'writeFileSync').mockRejectedValue('error')
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
throw new Error('error')
})
jest.spyOn(fs, 'accessSync')

// @ts-ignore
Expand All @@ -126,7 +131,7 @@ describe('saveToken', () => {
expect(fs.writeFileSync).toHaveBeenCalled()
})

it('should correct read with exeption while read', () => {
it('should correct read with exception while read', () => {
jest.spyOn(fs, 'readFileSync').mockImplementation(() => {
throw new Error('error')
})
Expand Down
31 changes: 16 additions & 15 deletions src/auto-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dgram from 'dgram'
import { Client, SsdpHeaders } from 'node-ssdp'
import * as request from 'request'
import axios from 'axios'

const SAMSUNG_TV_URN = 'urn:samsung.com:device'

Expand Down Expand Up @@ -74,23 +74,24 @@ class AutoSearch {
this.IPs.push(rinfo.address)

// TODO Add rotation Urls
request.get({ url: `http://${rinfo.address}:8001/api/v2/` }, (err: Error, res, body: string) => {
if (err || res.statusCode !== 200) {
return
}

const data: SamsungInfo = JSON.parse(body) as SamsungInfo

this.TVs.push({
ip: data.device.ip,
model: data.device.modelName,
name: data.device.name,
wifiMac: data.device.wifiMac,
axios.get(`http://${rinfo.address}:8001/api/v2/`)
.then(response => {
if (response.status !== 200) {
return;
}

const data: SamsungInfo = response.data as SamsungInfo;

this.TVs.push({
ip: data.device.ip,
model: data.device.modelName,
name: data.device.name,
wifiMac: data.device.wifiMac,
});
})
})
}

private stopSearch(resolve: (data: TV[]) => void) {
private stopSearch(resolve: (data: TV[]) => void, reject: (reason?: any) => void) {
this.client.stop()
resolve(this.TVs)
}
Expand Down
63 changes: 30 additions & 33 deletions src/samsung.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { exec } from 'child_process'
import * as fs from 'fs'
import * as net from 'net'
import * as path from 'path'
import * as request from 'request'
import * as wol from 'wake_on_lan'
import * as WebSocket from 'ws'
import axios from 'axios'
import { KEYS } from './keys'
import Logger from './logger'
import { Configuration, WSData, App, Command } from './types'
Expand Down Expand Up @@ -258,55 +258,52 @@ class Samsung {
this.LOGGER.log('videoId', { videoId }, 'openYouTubeLink')

return new Promise((resolve, reject) => {
request.post(
axios.post(
'http://' + this.IP + ':8080/ws/apps/YouTube',
videoId,
{
headers: {
'Content-Type': 'text/plain',
'Content-Length': Buffer.byteLength(videoId),
},
timeout: 10000,
body: videoId,
},
(err, response) => {
if (!err) {
this.LOGGER.log(
'Link sent',
{ status: response.statusCode, body: response.body, headers: response.headers },
'openYouTubeLink',
)
resolve('Link sent')
} else {
this.LOGGER.error('While send a link, somthing went wrong', { err }, 'openYouTubeLink')
reject(err)
}
},
}
)
.then(response => {
this.LOGGER.log(
'Link sent',
{ status: response.status, data: response.data, headers: response.headers },
'openYouTubeLink',
);
resolve('Link sent');
})
.catch(err => {
this.LOGGER.error('While sending a link, something went wrong', { err }, 'openYouTubeLink');
reject(err);
});
})
}

public isAvailable(): Promise<boolean> {
return new Promise((resolve, reject) => {
request.get(
{ url: `http://${this.IP}:8001${this.PORT === 55000 ? '/ms/1.0/' : '/api/v2/'}`, timeout: 3000 },
(err: Error, res: request.RequestResponse) => {
if (err) {
return reject(err)
}

if (!err && res.statusCode === 200) {
axios.get(`http://${this.IP}:8001${this.PORT === 55000 ? '/ms/1.0/' : '/api/v2/'}`, { timeout: 3000 })
.then(res => {
if (res.status === 200) {
this.LOGGER.log(
'TV is available',
{ request: res.request, body: res.body as string, code: res.statusCode },
{ request: res.request, data: res.data, code: res.status },
'isAvailable',
)
resolve(true)
);
resolve(true);
} else {
this.LOGGER.error('TV is not available', { err }, 'isAvailable')
resolve(false)
this.LOGGER.error('TV is not available', {}, 'isAvailable');
resolve(false);
}
},
)
})
.catch(err => {
this.LOGGER.error('TV is not available', { err }, 'isAvailable');
reject(err);
});
})
}

Expand Down Expand Up @@ -529,7 +526,7 @@ class Samsung {
console.log('Token saved!')
} catch (err) {
console.log('File error!')
this.LOGGER.error('catch fil esave', { err }, '_saveTokenToFile')
this.LOGGER.error('catch file save', { err }, '_saveTokenToFile')
}
}

Expand Down
Loading
Loading