Skip to content

Commit

Permalink
disable viewing history commands without auth #2620
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Aug 24, 2023
1 parent 693fe71 commit db0b3bb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.taobao.arthas.core.shell.term.impl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.taobao.arthas.common.ArthasConstants;
import com.taobao.arthas.core.shell.session.Session;

import io.termd.core.readline.Function;
import io.termd.core.readline.Readline;
import io.termd.core.readline.Readline.Interaction;

/**
* 拦截指定的 Function 的 apply 函数
*
* @author hengyunabc 2023-08-24
*
*/
public class FunctionInvocationHandler implements InvocationHandler {

private TermImpl termImpl;

private Function target;

public FunctionInvocationHandler(TermImpl termImpl, Function target) {
this.termImpl = termImpl;
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

String name = method.getName();

if (name.equals("apply")) {
Session session = termImpl.getSession();
if (session != null) {
boolean authenticated = session.get(ArthasConstants.SUBJECT_KEY) != null;
if (authenticated) {
return method.invoke(target, args);
} else {
Readline.Interaction interaction = (Interaction) args[0];
// 必要
interaction.resume();
return null;
}
}
}

return method.invoke(target, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.taobao.arthas.core.shell.term.impl;

import java.io.File;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.handlers.term.CloseHandlerWrapper;
import com.taobao.arthas.core.shell.handlers.term.DefaultTermStdinHandler;
import com.taobao.arthas.core.shell.handlers.term.EventHandler;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.handlers.term.RequestHandler;
import com.taobao.arthas.core.shell.handlers.term.SizeHandlerWrapper;
import com.taobao.arthas.core.shell.handlers.term.StdinHandlerWrapper;
Expand All @@ -13,17 +18,15 @@
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.FileUtils;

import io.termd.core.function.Consumer;
import io.termd.core.readline.Function;
import io.termd.core.readline.Keymap;
import io.termd.core.readline.Readline;
import io.termd.core.readline.functions.HistorySearchForward;
import io.termd.core.tty.TtyConnection;
import io.termd.core.util.Helper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
Expand All @@ -50,6 +53,18 @@ public TermImpl(Keymap keymap, TtyConnection conn) {
readline = new Readline(keymap);
readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE)));
for (Function function : readlineFunctions) {
/**
* 防止没有鉴权时,查看历史命令
*
* @see io.termd.core.readline.functions.HistorySearchForward
*/
if (function.name().contains("history")) {
FunctionInvocationHandler funcHandler = new FunctionInvocationHandler(this, function);
function = (Function) Proxy.newProxyInstance(this.getClass().getClassLoader(),
HistorySearchForward.class.getInterfaces(), funcHandler);

}

readline.addFunction(function);
}

Expand All @@ -64,6 +79,10 @@ public Term setSession(Session session) {
return this;
}

public Session getSession() {
return session;
}

@Override
public void readline(String prompt, Handler<String> lineHandler) {
if (conn.getStdinHandler() != echoHandler) {
Expand Down

0 comments on commit db0b3bb

Please sign in to comment.