栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

exec git命令拒绝重定向到Go中的文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

exec git命令拒绝重定向到Go中的文件

Go的

cmd.Run()
行为类似于C
fork()
exec()
启动新程序的过程。这 并不是
隐式调用Shell,从安全性的角度来看,这是一件非常好的事情。不必要的外壳程序调用通常会导致命令注入漏洞。

如果您 想要 外壳程序可以添加的功能(此处为重定向和复合命令语法),但又想避免安全风险,请从代码中将数据带外传递:

cmdArgs = []string{  "-c",// tells interpreter that script is next argument  `outfile=$1; shift; "$@" >"$outfile" && cat "$outfile"`, // script to execute  "_",         // this is $0  workingDir+"/logs/"+repoName+".log",          // $1, becomes outfile  "git", "log", "--numstat", "--reverse",       // remaining args are in "$@"  fmt.Sprintf("%s..HEAD", "89c98f5ec48c8ac383ea9e27d792c3dc77fa6240"),  "--pretty=format:=%P %H %an %ae %ad %at %s %b"}cmd := exec.Command("sh", cmdArgs...)

上面的内容等效于以下shell脚本:

#!/bin/sh#      ^^- not /bin/bash; this only guarantees support for POSIX syntaxoutfile=$1      # assign first positional argument to variable "$outfile"shift# rename $2 to $1, $3 to $2, etcif "$@" >"$outfile"; then  # run remaining arguments as a single command, stdout to outfile  cat "$outfile"# if that succeeded, then cat our "$outfile" to stdoutfi

请注意,我从内部删除了文字引号

--pretty=
。这是因为当你在shell中运行命令,这些报价作为语法处理 的外壳
-不要在格式字符串中的空间分割的指令。这里,没有外壳程序将该字符串解析为代码。如果我们将引号保留下来,它们将成为格式字符串的文字部分。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471106.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号