Skip to content

Commit

Permalink
test for windows, block executions on non-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Apr 17, 2024
1 parent 1b6fdf0 commit b98a5f3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { execSync } from 'node:child_process';
import Debug from 'debug';
import exitHook from 'exit-hook';
import { uncPathIsSafe, uncPathOptionsAreSafe, uncPathOptionsHaveCredentials } from './validators.js';
import { isWindows, uncPathIsSafe, uncPathOptionsAreSafe, uncPathOptionsHaveCredentials } from './validators.js';
const debug = Debug('windows-unc-path-connect');
export function connectToUncPath(uncPathOptions, connectOptions) {
if (!uncPathOptionsAreSafe(uncPathOptions)) {
if (!isWindows() || !uncPathOptionsAreSafe(uncPathOptions)) {
return false;
}
debug(`Connecting to share: ${uncPathOptions.uncPath}`);
Expand All @@ -29,7 +29,7 @@ export function connectToUncPath(uncPathOptions, connectOptions) {
}
}
export function disconnectUncPath(uncPath) {
if (!uncPathIsSafe(uncPath)) {
if (!isWindows() || !uncPathIsSafe(uncPath)) {
return false;
}
debug(`Disconnecting share: ${uncPath}`);
Expand Down
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import exitHook from 'exit-hook'

import type { ConnectOptions, UncPath, UncPathOptions } from './types.js'
import {
isWindows,
uncPathIsSafe,
uncPathOptionsAreSafe,
uncPathOptionsHaveCredentials
Expand All @@ -22,7 +23,7 @@ export function connectToUncPath(
uncPathOptions: UncPathOptions,
connectOptions?: Partial<ConnectOptions>
): boolean {
if (!uncPathOptionsAreSafe(uncPathOptions)) {
if (!isWindows() || !uncPathOptionsAreSafe(uncPathOptions)) {
return false
}

Expand Down Expand Up @@ -61,7 +62,7 @@ export function connectToUncPath(
* @returns {boolean} - True if the path was disconnected.
*/
export function disconnectUncPath(uncPath: UncPath): boolean {
if (!uncPathIsSafe(uncPath)) {
if (!isWindows() || !uncPathIsSafe(uncPath)) {
return false
}

Expand Down
1 change: 1 addition & 0 deletions validators.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type UncPath, type UncPathOptions, type UncPathOptionsWithCredentials } from './types.js';
export declare function isWindows(): boolean;
export declare function uncPathOptionsHaveCredentials(uncPathOptions: UncPathOptions): uncPathOptions is UncPathOptionsWithCredentials;
export declare function uncPathIsSafe(uncPath: string): uncPath is UncPath;
export declare function uncPathOptionsAreSafe(uncPathOptions: UncPathOptions): boolean;
3 changes: 3 additions & 0 deletions validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { uncPathPrefix } from './types.js';
function stringHasForbiddenCharacters(stringToCheck) {
return (stringToCheck ?? '').includes('"');
}
export function isWindows() {
return process.platform === 'win32';
}
export function uncPathOptionsHaveCredentials(uncPathOptions) {
return ((uncPathOptions.userName ?? '') !== '' &&
(uncPathOptions.password ?? '') !== '');
Expand Down
8 changes: 8 additions & 0 deletions validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ function stringHasForbiddenCharacters(stringToCheck?: string): boolean {
return (stringToCheck ?? '').includes('"')
}

/**
* Checks if the operating system is Windows.
* @returns {boolean} - True if the operating system is Windows.
*/
export function isWindows(): boolean {
return process.platform === 'win32'
}

/**
* Checks if the options include credentials.
* @param {UncPathOptions} uncPathOptions - UNC path options.
Expand Down

0 comments on commit b98a5f3

Please sign in to comment.