Skip to content

Commit e3171dd

Browse files
committed
feat: add splitOnFirstEquals function
1 parent 6cab256 commit e3171dd

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/node/cli.ts

+23
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ export const optionDescriptions = (): string[] => {
240240
})
241241
}
242242

243+
export function splitOnFirstEquals(str: string): string[] {
244+
// we use regex instead of "=" to ensure we split at the first
245+
// "=" and return the following substring with it
246+
// important for the hashed-password which looks like this
247+
// $argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY
248+
// 2 means return two items
249+
// Source: https://stackoverflow.com/a/4607799/3015595
250+
const split = str.split(/=(.+)/, 2)
251+
252+
// It should always return two elements
253+
// because it's used in a place where
254+
// it expected two elements
255+
if (split.length === 1) {
256+
split.push("")
257+
}
258+
259+
return split
260+
}
261+
243262
export const parse = (
244263
argv: string[],
245264
opts?: {
@@ -270,6 +289,7 @@ export const parse = (
270289
let key: keyof Args | undefined
271290
let value: string | undefined
272291
if (arg.startsWith("--")) {
292+
// TODO fix this
273293
const split = arg.replace(/^--/, "").split("=", 2)
274294
key = split[0] as keyof Args
275295
value = split[1]
@@ -543,6 +563,7 @@ export function parseConfigFile(configFile: string, configPath: string): ConfigA
543563
const config = yaml.load(configFile, {
544564
filename: configPath,
545565
})
566+
console.log("what is this config", config)
546567
if (!config || typeof config === "string") {
547568
throw new Error(`invalid config: ${config}`)
548569
}
@@ -555,9 +576,11 @@ export function parseConfigFile(configFile: string, configPath: string): ConfigA
555576
}
556577
return `--${optName}=${opt}`
557578
})
579+
console.log("what are the configFileArgv", configFileArgv)
558580
const args = parse(configFileArgv, {
559581
configFile: configPath,
560582
})
583+
console.log(args, "args")
561584
return {
562585
...args,
563586
config: configPath,

src/node/routes/login.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ import { Router, Request } from "express"
22
import { promises as fs } from "fs"
33
import { RateLimiter as Limiter } from "limiter"
44
import * as path from "path"
5-
import safeCompare from "safe-compare"
65
import { rootPath } from "../constants"
76
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
8-
import {
9-
getPasswordMethod,
10-
handlePasswordValidation,
11-
hash,
12-
hashLegacy,
13-
humanPath,
14-
isHashLegacyMatch,
15-
isHashMatch,
16-
} from "../util"
7+
import { getPasswordMethod, handlePasswordValidation, humanPath } from "../util"
178

189
export enum Cookie {
1910
Key = "key",

test/unit/cli.test.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { promises as fs } from "fs"
33
import * as net from "net"
44
import * as os from "os"
55
import * as path from "path"
6-
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../../src/node/cli"
6+
import { Args, parse, setDefaults, shouldOpenInExistingInstance, splitOnFirstEquals } from "../../src/node/cli"
77
import { tmpdir } from "../../src/node/constants"
88
import { paths } from "../../src/node/util"
99

@@ -337,6 +337,18 @@ describe("parser", () => {
337337
"proxy-domain": ["coder.com", "coder.org"],
338338
})
339339
})
340+
it("should allow '=,$/' in strings", async () => {
341+
const args = parse([
342+
"--enable-proposed-api",
343+
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
344+
])
345+
expect(args).toEqual({
346+
_: [],
347+
"enable-proposed-api": [
348+
"$argon2i$v=19$m=4096,t=3,p=1$0qr/o+0t00hsbjfqcksfdq$ofcm4rl6o+b7oxpua4qlxubypbbpsf+8l531u7p9hyy",
349+
],
350+
})
351+
})
340352
})
341353

342354
describe("cli", () => {
@@ -411,3 +423,28 @@ describe("cli", () => {
411423
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
412424
})
413425
})
426+
427+
describe("splitOnFirstEquals", () => {
428+
it("should split on the first equals", () => {
429+
const testStr = "--enabled-proposed-api=test=value"
430+
const actual = splitOnFirstEquals(testStr)
431+
const expected = ["--enabled-proposed-api", "test=value"]
432+
expect(actual).toEqual(expect.arrayContaining(expected))
433+
})
434+
it("should split on first equals regardless of multiple equals signs", () => {
435+
const testStr =
436+
"--hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
437+
const actual = splitOnFirstEquals(testStr)
438+
const expected = [
439+
"--hashed-password",
440+
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
441+
]
442+
expect(actual).toEqual(expect.arrayContaining(expected))
443+
})
444+
it("should always return two elements", () => {
445+
const testStr = ""
446+
const actual = splitOnFirstEquals(testStr)
447+
const expected = ["", ""]
448+
expect(actual).toEqual(expect.arrayContaining(expected))
449+
})
450+
})

0 commit comments

Comments
 (0)